Implement creating food

This commit is contained in:
2024-08-09 21:20:02 +02:00
parent db34fe039e
commit d9ae2e1ab3
10 changed files with 171 additions and 84 deletions

View File

@@ -56,6 +56,21 @@ func (s *FoodService) GetRecent() ([]Food, error) {
return res, nil
}
func (s *FoodService) GetLastPer100(name string) (float32, error) {
if s.db == nil || !s.db.Ready {
return 0, 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)
if err != nil {
return 0, fmt.Errorf("error scanning row: %v", err)
}
return per100, nil
}
func (s *FoodService) Create(food Food) (Food, error) {
if s.db == nil || !s.db.Ready {
return food, fmt.Errorf("cannot create food, db is nil or is not ready")