Rework food service to use new search query

This commit is contained in:
2024-08-13 17:21:08 +02:00
parent 1983c6c932
commit a6626761e7

View File

@@ -62,7 +62,18 @@ func (s *FoodService) GetLastPer100(name string) (Food, error) {
return res, 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(fmt.Sprintf("SELECT %s FROM food WHERE food like ? ORDER BY rowid DESC LIMIT 1", foodColumns), name+"%") row := s.db.readConn.QueryRow(fmt.Sprintf(`
with search_results as (
select word, rank
from foodfix
where word MATCH ?
limit 1
)
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) err := row.Scan(&res.Rowid, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy)
if err != nil { if err != nil {
return res, fmt.Errorf("error scanning row: %v", err) return res, fmt.Errorf("error scanning row: %v", err)