diff --git a/app.go b/app.go index c280456..757fe70 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) WailsFood1 { +func (a *App) GetLastPer100(name string) WailsFoodSearch { data, err := foodService.GetLastPer100(name) if err != nil { - return WailsFood1{Success: false, Error: err.Error()} + return WailsFoodSearch{Success: false, Error: err.Error()} } - return WailsFood1{Data: data, Success: true} + return WailsFoodSearch{Data: data, Success: true} } func (a *App) GetDailyFood() WailsAggregateFood { diff --git a/food.ddl b/food.ddl index 1352561..24a6e27 100644 --- a/food.ddl +++ b/food.ddl @@ -62,11 +62,12 @@ create table food( ); create virtual table foodfix using spellfix1; --- insert into foodfix (word, rank) --- select food as word, --- count(*) as rank --- from food --- group by food; +insert into foodfix (word, rank) +select food as word, + count(*) as rank +from food +group by food; + drop trigger if exists food_foodfix_insert; create trigger food_foodfix_insert AFTER insert on food for EACH row @@ -125,15 +126,15 @@ end; -- Spellfix search example with search_results as ( - select word, rank + select word, score, f.rowid, f.* from foodfix - where word MATCH 'B' - limit 1 + inner join food f on f.food == word + where word match 'B' ) -select f.*, s.rank -from search_results s - inner join food f on s.word = f.food -order by f.date desc; +select rowid, food, score, date, description, amount, per100, energy +from search_results +group by food +order by score asc, date desc create index dailyIdx on food(strftime('%Y-%m-%d', date)); create index weeklyIdx on food(strftime('%Y-%W', date)); diff --git a/foodservice.go b/foodservice.go index dd78a51..ab982c1 100644 --- a/foodservice.go +++ b/foodservice.go @@ -19,6 +19,10 @@ type ( Per100 float32 `json:"per100"` Energy float32 `json:"energy"` } + FoodSearch struct { + Food + Score int64 `json:"score"` + } AggregatedFood struct { Period string `json:"period"` Amount float32 `json:"amount"` @@ -56,27 +60,44 @@ func (s *FoodService) GetRecent() ([]Food, error) { return res, nil } -func (s *FoodService) GetLastPer100(name string) (Food, error) { - res := Food{} +func (s *FoodService) GetLastPer100(name string) ([]FoodSearch, error) { + res := []FoodSearch{} if s.db == nil || !s.db.Ready { return res, fmt.Errorf("cannot get last per100, db is nil or is not ready") } - row := s.db.readConn.QueryRow(fmt.Sprintf(` + query := fmt.Sprintf(` with search_results as ( - select word, rank - from foodfix - where word MATCH ? - limit 1 + select word, score, f.rowid, f.* + from foodfix + inner join food f on f.food == word + where word MATCH ? ) -select %s -from search_results s - inner join food f on s.word = f.food -order by f.date desc; - `, foodColumns), name) - err := row.Scan(&res.Rowid, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy) +select %s, score +from search_results +group by food +order by score asc, date desc +limit %d + `, foodColumns, Settings.SearchLimit) + // log.Printf("%#v", query) + rows, err := s.db.readConn.Query(query, name) if err != nil { - return res, fmt.Errorf("error scanning row: %v", err) + log.Printf("error getting last per100: %v", err) + return res, err + } + + for rows.Next() { + var f FoodSearch + err := rows.Scan(&f.Rowid, &f.Date, &f.Food.Food, &f.Descripton, &f.Amount, &f.Per100, &f.Energy, &f.Score) + if err != nil { + log.Printf("error scanning row: %v", err) + continue + } + res = append(res, f) + } + + if len(res) == 0 { + return nil, fmt.Errorf("no results found for %s", name) } return res, nil diff --git a/frontend/src/lib/components/Energy/EmptyFoodComp.svelte b/frontend/src/lib/components/Energy/EmptyFoodComp.svelte index 73b85df..d5fa9b8 100644 --- a/frontend/src/lib/components/Energy/EmptyFoodComp.svelte +++ b/frontend/src/lib/components/Energy/EmptyFoodComp.svelte @@ -55,7 +55,8 @@ if (!per100Edited) GetLastPer100(name.trim()).then((res) => { if (res.success) { - per100 = res.data.toString(); + console.log(res.data[0].food); + per100 = res.data[0].per100.toString(); } }); } diff --git a/settingsservice.go b/settingsservice.go index 36a2727..992e1b3 100644 --- a/settingsservice.go +++ b/settingsservice.go @@ -28,8 +28,9 @@ type settings struct { WeightMonthlyLookback int `default:"4" json:"weightMonthlyLookback"` WeightYearlyLookback int `default:"2" json:"weightYearlyLookback"` - Target int `default:"2000" json:"target"` - Limit int `default:"2500" json:"limit"` + Target int `default:"2000" json:"target"` + Limit int `default:"2500" json:"limit"` + SearchLimit int `default:"5" json:"searchLimit"` } var Settings settings diff --git a/wailstypes.go b/wailstypes.go index b375476..7c08149 100644 --- a/wailstypes.go +++ b/wailstypes.go @@ -28,6 +28,11 @@ type ( Success bool `json:"success"` Error string `json:"error,omitempty"` } + WailsFoodSearch struct { + Data []FoodSearch `json:"data"` + Success bool `json:"success"` + Error string `json:"error,omitempty"` + } WailsWeight struct { Data []Weight `json:"data"`