Fix issue with food creation
This commit is contained in:
@@ -21,17 +21,22 @@
|
|||||||
item.food = name.trim()
|
item.food = name.trim()
|
||||||
item.amount = parseInt(amount.trim())
|
item.amount = parseInt(amount.trim())
|
||||||
item.description = description.trim()
|
item.description = description.trim()
|
||||||
const [dbFood, err]: [Food, Err] = await FoodService.Create(item)
|
const [dbFood, err]: [Food[], Err] = await FoodService.Create(item)
|
||||||
name = ''
|
name = ''
|
||||||
amount = ''
|
amount = ''
|
||||||
description = ''
|
description = ''
|
||||||
|
|
||||||
|
if (dbFood.length == 0) {
|
||||||
|
toast.error('Creating food returned 0 rows')
|
||||||
|
return
|
||||||
|
}
|
||||||
if (err) {
|
if (err) {
|
||||||
toast.error(err)
|
toast.error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
foodStore.update((food) => {
|
foodStore.update((food) => {
|
||||||
food.push(dbFood)
|
// @ts-ignore
|
||||||
|
food.unshift(dbFood[0])
|
||||||
return food
|
return food
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,12 +31,12 @@ order by date DESC;
|
|||||||
`, [date])
|
`, [date])
|
||||||
},
|
},
|
||||||
// TODO: Rework this to use Err in Go style
|
// TODO: Rework this to use Err in Go style
|
||||||
async Create(food: Food): Promise<[Food, Err]> {
|
async Create(food: Food): Promise<[Food[], Err]> {
|
||||||
if (!food.food) return [food, 'food.food is required']
|
if (!food.food) return [[food], 'food.food is required']
|
||||||
if (!food.amount) throw [food, 'food.amount 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 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]
|
return [row, null]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user