From a05a4eb9e2573b290ac996d0ae76ed16591767ad Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 5 Jan 2025 17:55:13 +0100 Subject: [PATCH] Refactor weight service --- weightservice.go | 81 ++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 44 deletions(-) diff --git a/weightservice.go b/weightservice.go index 0ab449a..6bf7444 100644 --- a/weightservice.go +++ b/weightservice.go @@ -10,7 +10,7 @@ type ( db *DB } Weight struct { - Id int64 `json:"id"` + Id int64 `json:"id"` Date string `json:"date"` Weight float32 `json:"weight"` } @@ -23,21 +23,20 @@ type ( const weightcolumns = "id, date, weight" const weightAggregatedColumns = "period, amount" -func (w *WeightService) GetRecent() ([]Weight, error) { - res := []Weight{} +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 { - log.Printf("error getting daily weight: %v", err) - return res, err + 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) + err = row.Scan(&weight.Id, &weight.Date, &weight.Weight) if err != nil { log.Printf("error scanning row: %v", err) continue @@ -45,11 +44,12 @@ func (w *WeightService) GetRecent() ([]Weight, error) { res = append(res, weight) } - - return res, nil + log.Printf("Got %d recent weights", len(res)) + return } -func (w *WeightService) Create(weight Weight) (Weight, error) { +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") } @@ -57,46 +57,44 @@ func (w *WeightService) Create(weight Weight) (Weight, error) { return weight, fmt.Errorf("cannot create weight, weight is empty") } - res, err := w.db.writeConn.Exec("INSERT INTO weight (weight) VALUES (?)", weight.Weight) + dbres, err := w.db.writeConn.Exec("INSERT INTO weight (weight) VALUES (?)", weight.Weight) if err != nil { - return weight, fmt.Errorf("error inserting weight: %v", err) + return weight, fmt.Errorf("error inserting weight: %w", err) } - id, err := res.LastInsertId() + id, err := dbres.LastInsertId() if err != nil { - return weight, fmt.Errorf("error getting last insert id: %v", err) + return weight, fmt.Errorf("error getting last insert id: %w", err) } return w.GetById(id) } -func (w *WeightService) GetById(id int64) (Weight, error) { - var res Weight +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) + err = row.Scan(&res.Id, &res.Date, &res.Weight) if err != nil { - return res, fmt.Errorf("error scanning row: %v", err) + return res, fmt.Errorf("error scanning row: %w", err) } - - return res, nil + return } // I could probably refactor this to be less of a disaster... // But I think it'll work for now -func (w *WeightService) GetDaily() ([]AggregatedWeight, error) { - res := []AggregatedWeight{} +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 { - log.Printf("error getting daily weight: %v", err) - return res, err + return res, fmt.Errorf("error getting daily weight: %w", err) } for row.Next() { @@ -109,25 +107,23 @@ func (w *WeightService) GetDaily() ([]AggregatedWeight, error) { res = append(res, weight) } - - return res, nil + log.Printf("Got %d daily weights", len(res)) + return } -func (w *WeightService) GetWeekly() ([]AggregatedWeight, error) { - res := []AggregatedWeight{} +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 { - log.Printf("error getting weekly weight: %v", err) - return res, err + return res, fmt.Errorf("error getting weekly weight: %w", err) } for row.Next() { var weight AggregatedWeight - err := row.Scan(&weight.Period, &weight.Amount) + err = row.Scan(&weight.Period, &weight.Amount) if err != nil { log.Printf("error scanning row: %v", err) continue @@ -135,20 +131,18 @@ func (w *WeightService) GetWeekly() ([]AggregatedWeight, error) { res = append(res, weight) } - - return res, nil + log.Printf("Got %d weekly weights", len(res)) + return } -func (w *WeightService) GetMonthly() ([]AggregatedWeight, error) { - res := []AggregatedWeight{} +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 { - log.Printf("error getting monthly weight: %v", err) - return res, err + return res, fmt.Errorf("error getting monthly weight: %w", err) } for row.Next() { @@ -161,25 +155,23 @@ func (w *WeightService) GetMonthly() ([]AggregatedWeight, error) { res = append(res, weight) } - - return res, nil + log.Printf("Got %d monthly weights", len(res)) + return } -func (w *WeightService) GetYearly() ([]AggregatedWeight, error) { - res := []AggregatedWeight{} +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 { - log.Printf("error getting yearly weight: %v", err) - return res, err + return res, fmt.Errorf("error getting yearly weight: %w", err) } for row.Next() { var weight AggregatedWeight - err := row.Scan(&weight.Period, &weight.Amount) + err = row.Scan(&weight.Period, &weight.Amount) if err != nil { log.Printf("error scanning row: %v", err) continue @@ -188,5 +180,6 @@ func (w *WeightService) GetYearly() ([]AggregatedWeight, error) { res = append(res, weight) } - return res, nil + log.Printf("Got %d yearly weights", len(res)) + return }