Rework lastper100 to return a food instead of float32

This commit is contained in:
2024-08-13 15:31:50 +02:00
parent abb25c1357
commit 6bfd5cc26a
4 changed files with 11 additions and 27 deletions

View File

@@ -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) {