Implement food creation, such as it is
This commit is contained in:
@@ -1,18 +1,44 @@
|
||||
import { db } from '$lib/database'
|
||||
import type { Err } from '$lib/types'
|
||||
|
||||
export type Food = {
|
||||
rowid: number,
|
||||
rowid?: number,
|
||||
date?: Date,
|
||||
food: string,
|
||||
description?: string,
|
||||
amount: number,
|
||||
per100: number,
|
||||
energy: number,
|
||||
per100?: number,
|
||||
energy?: number,
|
||||
}
|
||||
|
||||
const columns = ['rowid', 'date', 'food', 'description', 'amount', 'per100', 'energy']
|
||||
|
||||
const FoodService = {
|
||||
async GetAll() {
|
||||
return await db.select<Food[]>('SELECT rowid, food, amount, per100, energy FROM food ORDER BY date DESC')
|
||||
return await db.select<Food[]>(`
|
||||
select ${columns.join(', ')}
|
||||
from food
|
||||
order by date desc
|
||||
`)
|
||||
},
|
||||
async SetAll(data: Food[]) {
|
||||
async GetAllForDate(date: Date) {
|
||||
return await db.select<Food[]>(`
|
||||
select ${columns.join(', ')}
|
||||
from food
|
||||
where strftime('%Y-%m-%d', date) = strftime('%Y-%m-%d', '2024-06-12')
|
||||
or strftime('%Y-%m-%d', date) = strftime('%Y-%m-%d', date('2024-06-12', '-1 day'))
|
||||
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']
|
||||
|
||||
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])
|
||||
|
||||
return [row, null]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user