Refactor weight service
This commit is contained in:
@@ -10,7 +10,7 @@ type (
|
|||||||
db *DB
|
db *DB
|
||||||
}
|
}
|
||||||
Weight struct {
|
Weight struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
Weight float32 `json:"weight"`
|
Weight float32 `json:"weight"`
|
||||||
}
|
}
|
||||||
@@ -23,21 +23,20 @@ type (
|
|||||||
const weightcolumns = "id, date, weight"
|
const weightcolumns = "id, date, weight"
|
||||||
const weightAggregatedColumns = "period, amount"
|
const weightAggregatedColumns = "period, amount"
|
||||||
|
|
||||||
func (w *WeightService) GetRecent() ([]Weight, error) {
|
func (w *WeightService) GetRecent() (res []Weight, err error) {
|
||||||
res := []Weight{}
|
log.Printf("Getting recent weight")
|
||||||
if w.db == nil || !w.db.Ready {
|
if w.db == nil || !w.db.Ready {
|
||||||
return res, fmt.Errorf("cannot get recent weight, db is nil or is not 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))
|
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 {
|
if err != nil {
|
||||||
log.Printf("error getting daily weight: %v", err)
|
return res, fmt.Errorf("error getting recent weight: %w", err)
|
||||||
return res, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var weight Weight
|
var weight Weight
|
||||||
err := row.Scan(&weight.Id, &weight.Date, &weight.Weight)
|
err = row.Scan(&weight.Id, &weight.Date, &weight.Weight)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error scanning row: %v", err)
|
log.Printf("error scanning row: %v", err)
|
||||||
continue
|
continue
|
||||||
@@ -45,11 +44,12 @@ func (w *WeightService) GetRecent() ([]Weight, error) {
|
|||||||
|
|
||||||
res = append(res, weight)
|
res = append(res, weight)
|
||||||
}
|
}
|
||||||
|
log.Printf("Got %d recent weights", len(res))
|
||||||
return res, nil
|
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 {
|
if w.db == nil || !w.db.Ready {
|
||||||
return weight, fmt.Errorf("cannot create weight, db is nil or is not 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")
|
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 {
|
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 {
|
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)
|
return w.GetById(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WeightService) GetById(id int64) (Weight, error) {
|
func (w *WeightService) GetById(id int64) (res Weight, err error) {
|
||||||
var res Weight
|
log.Printf("Getting weight by id: %d", id)
|
||||||
if w.db == nil || !w.db.Ready {
|
if w.db == nil || !w.db.Ready {
|
||||||
return res, fmt.Errorf("cannot get weight by id, db is nil or is not 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)
|
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 {
|
if err != nil {
|
||||||
return res, fmt.Errorf("error scanning row: %v", err)
|
return res, fmt.Errorf("error scanning row: %w", err)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// I could probably refactor this to be less of a disaster...
|
// I could probably refactor this to be less of a disaster...
|
||||||
// But I think it'll work for now
|
// But I think it'll work for now
|
||||||
func (w *WeightService) GetDaily() ([]AggregatedWeight, error) {
|
func (w *WeightService) GetDaily() (res []AggregatedWeight, err error) {
|
||||||
res := []AggregatedWeight{}
|
log.Printf("Getting daily weight")
|
||||||
if w.db == nil || !w.db.Ready {
|
if w.db == nil || !w.db.Ready {
|
||||||
return res, fmt.Errorf("cannot get daily weight, db is nil or is not 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))
|
row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightDaily LIMIT %d", weightAggregatedColumns, Settings.WeightDailyLookback))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error getting daily weight: %v", err)
|
return res, fmt.Errorf("error getting daily weight: %w", err)
|
||||||
return res, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
@@ -109,25 +107,23 @@ func (w *WeightService) GetDaily() ([]AggregatedWeight, error) {
|
|||||||
|
|
||||||
res = append(res, weight)
|
res = append(res, weight)
|
||||||
}
|
}
|
||||||
|
log.Printf("Got %d daily weights", len(res))
|
||||||
return res, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WeightService) GetWeekly() ([]AggregatedWeight, error) {
|
func (w *WeightService) GetWeekly() (res []AggregatedWeight, err error) {
|
||||||
res := []AggregatedWeight{}
|
|
||||||
if w.db == nil || !w.db.Ready {
|
if w.db == nil || !w.db.Ready {
|
||||||
return res, fmt.Errorf("cannot get weekly weight, db is nil or is not 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))
|
row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightWeekly LIMIT %d", weightAggregatedColumns, Settings.WeightWeeklyLookback))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error getting weekly weight: %v", err)
|
return res, fmt.Errorf("error getting weekly weight: %w", err)
|
||||||
return res, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var weight AggregatedWeight
|
var weight AggregatedWeight
|
||||||
err := row.Scan(&weight.Period, &weight.Amount)
|
err = row.Scan(&weight.Period, &weight.Amount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error scanning row: %v", err)
|
log.Printf("error scanning row: %v", err)
|
||||||
continue
|
continue
|
||||||
@@ -135,20 +131,18 @@ func (w *WeightService) GetWeekly() ([]AggregatedWeight, error) {
|
|||||||
|
|
||||||
res = append(res, weight)
|
res = append(res, weight)
|
||||||
}
|
}
|
||||||
|
log.Printf("Got %d weekly weights", len(res))
|
||||||
return res, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WeightService) GetMonthly() ([]AggregatedWeight, error) {
|
func (w *WeightService) GetMonthly() (res []AggregatedWeight, err error) {
|
||||||
res := []AggregatedWeight{}
|
|
||||||
if w.db == nil || !w.db.Ready {
|
if w.db == nil || !w.db.Ready {
|
||||||
return res, fmt.Errorf("cannot get monthly weight, db is nil or is not 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))
|
row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightMonthly LIMIT %d", weightAggregatedColumns, Settings.WeightMonthlyLookback))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error getting monthly weight: %v", err)
|
return res, fmt.Errorf("error getting monthly weight: %w", err)
|
||||||
return res, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
@@ -161,25 +155,23 @@ func (w *WeightService) GetMonthly() ([]AggregatedWeight, error) {
|
|||||||
|
|
||||||
res = append(res, weight)
|
res = append(res, weight)
|
||||||
}
|
}
|
||||||
|
log.Printf("Got %d monthly weights", len(res))
|
||||||
return res, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WeightService) GetYearly() ([]AggregatedWeight, error) {
|
func (w *WeightService) GetYearly() (res []AggregatedWeight, err error) {
|
||||||
res := []AggregatedWeight{}
|
|
||||||
if w.db == nil || !w.db.Ready {
|
if w.db == nil || !w.db.Ready {
|
||||||
return res, fmt.Errorf("cannot get yearly weight, db is nil or is not 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))
|
row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightYearly LIMIT %d", weightAggregatedColumns, Settings.WeightYearlyLookback))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error getting yearly weight: %v", err)
|
return res, fmt.Errorf("error getting yearly weight: %w", err)
|
||||||
return res, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var weight AggregatedWeight
|
var weight AggregatedWeight
|
||||||
err := row.Scan(&weight.Period, &weight.Amount)
|
err = row.Scan(&weight.Period, &weight.Amount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error scanning row: %v", err)
|
log.Printf("error scanning row: %v", err)
|
||||||
continue
|
continue
|
||||||
@@ -188,5 +180,6 @@ func (w *WeightService) GetYearly() ([]AggregatedWeight, error) {
|
|||||||
res = append(res, weight)
|
res = append(res, weight)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
log.Printf("Got %d yearly weights", len(res))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user