Fix issue with food creation

This commit is contained in:
2024-06-12 09:45:41 +02:00
parent b4d8543a5f
commit 5a791dcb3d
2 changed files with 11 additions and 6 deletions

View File

@@ -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
})
}

View File

@@ -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<Food>(`select ${columns.join(', ')} from food where rowid = $1`, [res.lastInsertId])
const row = await db.select<Food[]>(`select ${columns.join(', ')} from food where rowid = $1`, [res.lastInsertId])
return [row, null]
}