Add daily weekly monthly and yearly view for food
This commit is contained in:
124
foodservice.go
124
foodservice.go
@@ -36,7 +36,11 @@ func (s *FoodService) GetRecent() ([]Food, error) {
|
||||
return res, fmt.Errorf("cannot get recent food, db is nil or is not ready")
|
||||
}
|
||||
|
||||
row, _ := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodView WHERE date > datetime('now', '-%s days')", foodColumns, Settings.FoodDaysLookback))
|
||||
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodView WHERE date > datetime('now', '-%d days')", foodColumns, Settings.FoodDaysLookback))
|
||||
if err != nil {
|
||||
log.Printf("error getting daily food: %v", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
var rows []Food
|
||||
for row.Next() {
|
||||
@@ -100,5 +104,123 @@ func (s *FoodService) GetByRowid(rowid int64) (Food, error) {
|
||||
return res, fmt.Errorf("error scanning row: %v", err)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// I could probably refactor this to be less of a disaster...
|
||||
// But I think it'll work for now
|
||||
func (s *FoodService) GetDaily() ([]AggregatedFood, error) {
|
||||
var res []AggregatedFood
|
||||
if s.db == nil || !s.db.Ready {
|
||||
return res, fmt.Errorf("cannot get daily food, db is nil or is not ready")
|
||||
}
|
||||
|
||||
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodDaily LIMIT %d", foodAggregatedColumns, Settings.FoodDailyLookback))
|
||||
if err != nil {
|
||||
log.Printf("error getting daily food: %v", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
var rows []AggregatedFood
|
||||
for row.Next() {
|
||||
var food AggregatedFood
|
||||
err := row.Scan(&food.period, &food.amount, &food.avgPer100, &food.energy)
|
||||
if err != nil {
|
||||
log.Printf("error scanning row: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
rows = append(rows, food)
|
||||
}
|
||||
|
||||
log.Printf("%++v", rows)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *FoodService) GetWeekly() ([]AggregatedFood, error) {
|
||||
var res []AggregatedFood
|
||||
if s.db == nil || !s.db.Ready {
|
||||
return res, fmt.Errorf("cannot get weekly food, db is nil or is not ready")
|
||||
}
|
||||
|
||||
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodWeekly LIMIT %d", foodAggregatedColumns, Settings.FoodWeeklyLookback))
|
||||
if err != nil {
|
||||
log.Printf("error getting weekly food: %v", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
var rows []AggregatedFood
|
||||
for row.Next() {
|
||||
var food AggregatedFood
|
||||
err := row.Scan(&food.period, &food.amount, &food.avgPer100, &food.energy)
|
||||
if err != nil {
|
||||
log.Printf("error scanning row: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
rows = append(rows, food)
|
||||
}
|
||||
|
||||
log.Printf("%++v", rows)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *FoodService) GetMonthly() ([]AggregatedFood, error) {
|
||||
var res []AggregatedFood
|
||||
if s.db == nil || !s.db.Ready {
|
||||
return res, fmt.Errorf("cannot get monthly food, db is nil or is not ready")
|
||||
}
|
||||
|
||||
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodMonthly LIMIT %d", foodAggregatedColumns, Settings.FoodMonthlyLookback))
|
||||
if err != nil {
|
||||
log.Printf("error getting monthly food: %v", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
var rows []AggregatedFood
|
||||
for row.Next() {
|
||||
var food AggregatedFood
|
||||
err := row.Scan(&food.period, &food.amount, &food.avgPer100, &food.energy)
|
||||
if err != nil {
|
||||
log.Printf("error scanning row: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
rows = append(rows, food)
|
||||
}
|
||||
|
||||
log.Printf("%++v", rows)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *FoodService) GetYearly() ([]AggregatedFood, error) {
|
||||
var res []AggregatedFood
|
||||
if s.db == nil || !s.db.Ready {
|
||||
return res, fmt.Errorf("cannot get yearly food, db is nil or is not ready")
|
||||
}
|
||||
|
||||
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodYearly LIMIT %d", foodAggregatedColumns, Settings.FoodYearlyLookback))
|
||||
if err != nil {
|
||||
log.Printf("error getting daily yearly: %v", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
var rows []AggregatedFood
|
||||
for row.Next() {
|
||||
var food AggregatedFood
|
||||
err := row.Scan(&food.period, &food.amount, &food.avgPer100, &food.energy)
|
||||
if err != nil {
|
||||
log.Printf("error scanning row: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
rows = append(rows, food)
|
||||
}
|
||||
|
||||
log.Printf("%++v", rows)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
Reference in New Issue
Block a user