From 6bfd5cc26a6f73fd21a831924c9beab75dd9dae7 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 13 Aug 2024 15:31:50 +0200 Subject: [PATCH] Rework lastper100 to return a food instead of float32 --- app.go | 6 +++--- foodservice.go | 14 +++++++------- frontend/wailsjs/go/main/App.d.ts | 2 +- frontend/wailsjs/go/models.ts | 16 ---------------- 4 files changed, 11 insertions(+), 27 deletions(-) diff --git a/app.go b/app.go index abbf3e1..c280456 100644 --- a/app.go +++ b/app.go @@ -44,12 +44,12 @@ func (a *App) UpdateFood(food Food) WailsFood1 { return WailsFood1{Data: data, Success: true} } -func (a *App) GetLastPer100(name string) WailsPer100 { +func (a *App) GetLastPer100(name string) WailsFood1 { data, err := foodService.GetLastPer100(name) if err != nil { - return WailsPer100{Success: false, Error: err.Error()} + return WailsFood1{Success: false, Error: err.Error()} } - return WailsPer100{Data: data, Success: true} + return WailsFood1{Data: data, Success: true} } func (a *App) GetDailyFood() WailsAggregateFood { diff --git a/foodservice.go b/foodservice.go index a235556..2b03fbf 100644 --- a/foodservice.go +++ b/foodservice.go @@ -56,19 +56,19 @@ func (s *FoodService) GetRecent() ([]Food, error) { return res, nil } -func (s *FoodService) GetLastPer100(name string) (float32, error) { +func (s *FoodService) GetLastPer100(name string) (Food, error) { + res := Food{} if s.db == nil || !s.db.Ready { - return 0, fmt.Errorf("cannot get last per100, db is nil or is not ready") + return res, fmt.Errorf("cannot get last per100, db is nil or is not ready") } - row := s.db.readConn.QueryRow("SELECT per100 FROM food WHERE food like ? ORDER BY rowid DESC LIMIT 1", name+"%") - var per100 float32 - err := row.Scan(&per100) + row := s.db.readConn.QueryRow(fmt.Sprintf("SELECT %s FROM food WHERE food like ? ORDER BY rowid DESC LIMIT 1", foodColumns), name+"%") + err := row.Scan(&res.Rowid, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy) if err != nil { - return 0, fmt.Errorf("error scanning row: %v", err) + return res, fmt.Errorf("error scanning row: %v", err) } - return per100, nil + return res, nil } func (s *FoodService) Create(food Food) (Food, error) { diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 1b47a0c..407ee01 100644 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -14,7 +14,7 @@ export function GetDailyWeight():Promise; export function GetFood():Promise; -export function GetLastPer100(arg1:string):Promise; +export function GetLastPer100(arg1:string):Promise; export function GetMonthlyFood():Promise; diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts index 7b11af4..2b2eb15 100644 --- a/frontend/wailsjs/go/models.ts +++ b/frontend/wailsjs/go/models.ts @@ -206,22 +206,6 @@ export namespace main { this.error = source["error"]; } } - export class WailsPer100 { - data: number; - success: boolean; - error?: string; - - static createFrom(source: any = {}) { - return new WailsPer100(source); - } - - constructor(source: any = {}) { - if ('string' === typeof source) source = JSON.parse(source); - this.data = source["data"]; - this.success = source["success"]; - this.error = source["error"]; - } - } export class Weight { rowid: number; date: string;