Refactor & add aggregated entries

This commit is contained in:
2024-06-12 10:06:54 +02:00
parent a7d76a1f14
commit 5b28ea62b1
7 changed files with 129 additions and 54 deletions

View File

@@ -12,6 +12,7 @@ export type Food = {
}
const columns = ['rowid', 'date', 'food', 'description', 'amount', 'per100', 'energy']
const aggColumns = ['date', 'amount', 'avgPer100', 'energy']
const FoodService = {
async GetAll() {
@@ -30,7 +31,6 @@ 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']
@@ -39,7 +39,23 @@ order by date DESC;
const row = await db.select<Food[]>(`select ${columns.join(', ')} from food where rowid = $1`, [res.lastInsertId])
return [row, null]
}
},
async GetDaily(): Promise<[Food[], Err]> {
const rows = await db.select<Food[]>(`select ${aggColumns.join(', ')} from daily limit 100`)
return [rows, null]
},
async GetWeekly(): Promise<[Food[], Err]> {
const rows = await db.select<Food[]>(`select ${aggColumns.join(', ')} from weekly limit 100`)
return [rows, null]
},
async GetMonthly(): Promise<[Food[], Err]> {
const rows = await db.select<Food[]>(`select ${aggColumns.join(', ')} from monthly limit 100`)
return [rows, null]
},
async GetYearly(): Promise<[Food[], Err]> {
const rows = await db.select<Food[]>(`select ${aggColumns.join(', ')} from yearly limit 100`)
return [rows, null]
},
}
export { FoodService }