Rework search to return a list
Like a proper search would!"
This commit is contained in:
6
app.go
6
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 {
|
||||
|
25
food.ddl
25
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));
|
||||
|
@@ -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
|
||||
|
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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"`
|
||||
|
Reference in New Issue
Block a user