package main import ( "fmt" "log" ) type ( WeightService struct { db *DB } Weight struct { Id int64 `json:"id"` Date string `json:"date"` Weight float32 `json:"weight"` } AggregatedWeight struct { Period string `json:"period"` Amount float32 `json:"amount"` } ) const weightcolumns = "id, date, weight" const weightAggregatedColumns = "period, amount" func (w *WeightService) GetRecent() (res []Weight, err error) { log.Printf("Getting recent weight") if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get recent weight, db is nil or is not ready") } row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightView WHERE date > datetime('now', '-%d days') order by date desc;", weightcolumns, Settings.WeightDaysLookback)) if err != nil { return res, fmt.Errorf("error getting recent weight: %w", err) } for row.Next() { var weight Weight err = row.Scan(&weight.Id, &weight.Date, &weight.Weight) if err != nil { log.Printf("error scanning row: %v", err) continue } res = append(res, weight) } log.Printf("Got %d recent weights", len(res)) return } func (w *WeightService) Create(weight Weight) (res Weight, err error) { log.Printf("Creating weight: %v", weight) if w.db == nil || !w.db.Ready { return weight, fmt.Errorf("cannot create weight, db is nil or is not ready") } if weight.Weight <= 0 { return weight, fmt.Errorf("cannot create weight, weight is empty") } dbres, err := w.db.writeConn.Exec("INSERT INTO weight (weight) VALUES (?)", weight.Weight) if err != nil { return weight, fmt.Errorf("error inserting weight: %w", err) } id, err := dbres.LastInsertId() if err != nil { return weight, fmt.Errorf("error getting last insert id: %w", err) } return w.GetById(id) } func (w *WeightService) GetById(id int64) (res Weight, err error) { log.Printf("Getting weight by id: %d", id) if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get weight by id, db is nil or is not ready") } row := w.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from weightView WHERE id = ?", weightcolumns), id) err = row.Scan(&res.Id, &res.Date, &res.Weight) if err != nil { return res, fmt.Errorf("error scanning row: %w", err) } return } // I could probably refactor this to be less of a disaster... // But I think it'll work for now func (w *WeightService) GetDaily() (res []AggregatedWeight, err error) { log.Printf("Getting daily weight") if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get daily weight, db is nil or is not ready") } row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightDaily LIMIT %d", weightAggregatedColumns, Settings.WeightDailyLookback)) if err != nil { return res, fmt.Errorf("error getting daily weight: %w", err) } for row.Next() { var weight AggregatedWeight err := row.Scan(&weight.Period, &weight.Amount) if err != nil { log.Printf("error scanning row: %v", err) continue } res = append(res, weight) } log.Printf("Got %d daily weights", len(res)) return } func (w *WeightService) GetWeekly() (res []AggregatedWeight, err error) { if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get weekly weight, db is nil or is not ready") } row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightWeekly LIMIT %d", weightAggregatedColumns, Settings.WeightWeeklyLookback)) if err != nil { return res, fmt.Errorf("error getting weekly weight: %w", err) } for row.Next() { var weight AggregatedWeight err = row.Scan(&weight.Period, &weight.Amount) if err != nil { log.Printf("error scanning row: %v", err) continue } res = append(res, weight) } log.Printf("Got %d weekly weights", len(res)) return } func (w *WeightService) GetMonthly() (res []AggregatedWeight, err error) { if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get monthly weight, db is nil or is not ready") } row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightMonthly LIMIT %d", weightAggregatedColumns, Settings.WeightMonthlyLookback)) if err != nil { return res, fmt.Errorf("error getting monthly weight: %w", err) } for row.Next() { var weight AggregatedWeight err := row.Scan(&weight.Period, &weight.Amount) if err != nil { log.Printf("error scanning row: %v", err) continue } res = append(res, weight) } log.Printf("Got %d monthly weights", len(res)) return } func (w *WeightService) GetYearly() (res []AggregatedWeight, err error) { if w.db == nil || !w.db.Ready { return res, fmt.Errorf("cannot get yearly weight, db is nil or is not ready") } row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightYearly LIMIT %d", weightAggregatedColumns, Settings.WeightYearlyLookback)) if err != nil { return res, fmt.Errorf("error getting yearly weight: %w", err) } for row.Next() { var weight AggregatedWeight err = row.Scan(&weight.Period, &weight.Amount) if err != nil { log.Printf("error scanning row: %v", err) continue } res = append(res, weight) } log.Printf("Got %d yearly weights", len(res)) return }