From 5ee56a98d0e40b2a21421b3b83c6bee0f72d29b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Wed, 12 Jun 2024 00:56:52 +0200 Subject: [PATCH] Implement food creation, such as it is --- src-tauri/tauri.conf.json | 4 +- src/App.svelte | 29 +++------- src/lib/components/EmptyFoodComp.svelte | 61 +++++++++++++++++++++ src/lib/components/FoodComp.svelte | 31 +++++++++++ src/lib/database/food.ts | 36 +++++++++++-- src/lib/ipc.ts | 18 ++++--- src/lib/router/routes/Home.svelte | 70 ++++++++++++++++++++----- src/lib/types.ts | 2 + 8 files changed, 201 insertions(+), 50 deletions(-) create mode 100644 src/lib/components/EmptyFoodComp.svelte create mode 100644 src/lib/components/FoodComp.svelte diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 1794870..2823462 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -64,8 +64,8 @@ "windows": [ { "title": "Svelte-Tauri", - "width": 900, - "height": 600, + "width": 1920, + "height": 1080, "fullscreen": false, "transparent": true, "decorations": false diff --git a/src/App.svelte b/src/App.svelte index aafff2b..463e405 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,29 +1,16 @@ -
-
-
-	    {#each food as f}
-	      {f.amount}
-	    {/each}
-    
-
- - - - -
+ diff --git a/src/lib/components/EmptyFoodComp.svelte b/src/lib/components/EmptyFoodComp.svelte new file mode 100644 index 0000000..ce572a0 --- /dev/null +++ b/src/lib/components/EmptyFoodComp.svelte @@ -0,0 +1,61 @@ + + + diff --git a/src/lib/components/FoodComp.svelte b/src/lib/components/FoodComp.svelte new file mode 100644 index 0000000..b95de89 --- /dev/null +++ b/src/lib/components/FoodComp.svelte @@ -0,0 +1,31 @@ + + + diff --git a/src/lib/database/food.ts b/src/lib/database/food.ts index 3b60ce7..b4d524c 100644 --- a/src/lib/database/food.ts +++ b/src/lib/database/food.ts @@ -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('SELECT rowid, food, amount, per100, energy FROM food ORDER BY date DESC') + return await db.select(` +select ${columns.join(', ')} +from food +order by date desc + `) }, - async SetAll(data: Food[]) { + async GetAllForDate(date: Date) { + return await db.select(` +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(`select ${columns.join(', ')} from food where rowid = $1`, [res.lastInsertId]) + + return [row, null] } } diff --git a/src/lib/ipc.ts b/src/lib/ipc.ts index c6dbde9..3796316 100644 --- a/src/lib/ipc.ts +++ b/src/lib/ipc.ts @@ -2,26 +2,28 @@ // This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually. declare global { - interface Window { - __TAURI_INVOKE__(cmd: string, args?: Record): Promise; - } + interface Window { + __TAURI_INVOKE__(cmd: string, args?: Record): Promise; + } } // Function avoids 'window not defined' in SSR -const invoke = () => window.__TAURI_INVOKE__ +const invoke = () => window.__TAURI_INVOKE__; export function helloTauri() { - return invoke()('hello_tauri') + return invoke()("hello_tauri") } export function hash256sum(hashInput: string) { - return invoke()('hash256sum', { hashInput }) + return invoke()("hash256sum", { hashInput }) } export function storeSetKey(key: string, value: string) { - return invoke()('store_set_key', { key, value }) + return invoke()("store_set_key", { key,value }) } export function storeReadKey(key: string) { - return invoke()('store_read_key', { key }) + return invoke()("store_read_key", { key }) } + + diff --git a/src/lib/router/routes/Home.svelte b/src/lib/router/routes/Home.svelte index b4fe411..6abb6d8 100644 --- a/src/lib/router/routes/Home.svelte +++ b/src/lib/router/routes/Home.svelte @@ -1,14 +1,56 @@ -
-

Welcome

-

- This is a   - - svelte logo - -  -  - - svelte logo - -   Template -

-
+ + + diff --git a/src/lib/types.ts b/src/lib/types.ts index d4929b5..8b47513 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,2 +1,4 @@ export const themes = ['dark', 'light'] as const export type Theme = (typeof themes)[number] + +export type Err = string | null