diff --git a/src/lib/components/EmptyFoodComp.svelte b/src/lib/components/EmptyFoodComp.svelte index a15d7fb..e405c67 100644 --- a/src/lib/components/EmptyFoodComp.svelte +++ b/src/lib/components/EmptyFoodComp.svelte @@ -21,17 +21,22 @@ item.food = name.trim() item.amount = parseInt(amount.trim()) item.description = description.trim() - const [dbFood, err]: [Food, Err] = await FoodService.Create(item) + const [dbFood, err]: [Food[], Err] = await FoodService.Create(item) name = '' amount = '' description = '' + if (dbFood.length == 0) { + toast.error('Creating food returned 0 rows') + return + } if (err) { toast.error(err) return } foodStore.update((food) => { - food.push(dbFood) + // @ts-ignore + food.unshift(dbFood[0]) return food }) } diff --git a/src/lib/database/food.ts b/src/lib/database/food.ts index b4d524c..9872699 100644 --- a/src/lib/database/food.ts +++ b/src/lib/database/food.ts @@ -31,12 +31,12 @@ order by date DESC; `, [date]) }, // TODO: Rework this to use Err in Go style - async Create(food: Food): Promise<[Food, Err]> { - if (!food.food) return [food, 'food.food is required'] - if (!food.amount) throw [food, 'food.amount is required'] + async Create(food: Food): Promise<[Food[], Err]> { + if (!food.food) return [[food], 'food.food is required'] + if (!food.amount) throw [[food], 'food.amount is required'] const res = await db.execute(`insert into food (food, description, amount) values ($1, $2, $3)`, [food.food, food.description, food.amount]) - const row = await db.select(`select ${columns.join(', ')} from food where rowid = $1`, [res.lastInsertId]) + const row = await db.select(`select ${columns.join(', ')} from food where rowid = $1`, [res.lastInsertId]) return [row, null] }