Compare commits
3 Commits
bee45ebc59
...
531afabf61
Author | SHA1 | Date | |
---|---|---|---|
531afabf61 | |||
a05a4eb9e2 | |||
1831ed12c5 |
@@ -34,21 +34,20 @@ type (
|
||||
const foodColumns = "id, date, food, description, amount, per100, energy"
|
||||
const foodAggregatedColumns = "period, amount, avgPer100, energy"
|
||||
|
||||
func (s *FoodService) GetRecent() ([]Food, error) {
|
||||
var res []Food = []Food{}
|
||||
func (s *FoodService) GetRecent() (res []Food, err error) {
|
||||
log.Printf("Getting recent food")
|
||||
if s.db == nil || !s.db.Ready {
|
||||
return res, fmt.Errorf("cannot get recent food, db is nil or is not ready")
|
||||
}
|
||||
|
||||
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodView WHERE date > datetime('now', '-%d days') order by date desc;", foodColumns, Settings.FoodDaysLookback))
|
||||
if err != nil {
|
||||
log.Printf("error getting daily food: %v", err)
|
||||
return res, err
|
||||
return res, fmt.Errorf("error getting recent food: %w", err)
|
||||
}
|
||||
|
||||
for row.Next() {
|
||||
var food Food
|
||||
err := row.Scan(&food.Id, &food.Date, &food.Food, &food.Descripton, &food.Amount, &food.Per100, &food.Energy)
|
||||
err = row.Scan(&food.Id, &food.Date, &food.Food, &food.Descripton, &food.Amount, &food.Per100, &food.Energy)
|
||||
if err != nil {
|
||||
log.Printf("error scanning row: %v", err)
|
||||
continue
|
||||
@@ -57,11 +56,12 @@ func (s *FoodService) GetRecent() ([]Food, error) {
|
||||
res = append(res, food)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
log.Printf("Got %d recent foods", len(res))
|
||||
return
|
||||
}
|
||||
|
||||
func (s *FoodService) GetLastPer100(name string) ([]FoodSearch, error) {
|
||||
res := []FoodSearch{}
|
||||
func (s *FoodService) GetLastPer100(name string) (res []FoodSearch, err error) {
|
||||
log.Printf("Getting last per100 for %s", name)
|
||||
if s.db == nil || !s.db.Ready {
|
||||
return res, fmt.Errorf("cannot get last per100, db is nil or is not ready")
|
||||
}
|
||||
@@ -83,13 +83,12 @@ limit %d
|
||||
// log.Printf("%#v", query)
|
||||
rows, err := s.db.readConn.Query(query, name)
|
||||
if err != nil {
|
||||
log.Printf("error getting last per100: %v", err)
|
||||
return res, err
|
||||
return res, fmt.Errorf("error getting last per100: %w", err)
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var f FoodSearch
|
||||
err := rows.Scan(&f.Id, &f.Date, &f.Food.Food, &f.Descripton, &f.Amount, &f.Per100, &f.Energy, &f.Score)
|
||||
err = rows.Scan(&f.Id, &f.Date, &f.Food.Food, &f.Descripton, &f.Amount, &f.Per100, &f.Energy, &f.Score)
|
||||
if err != nil {
|
||||
log.Printf("error scanning row: %v", err)
|
||||
continue
|
||||
@@ -101,10 +100,12 @@ limit %d
|
||||
return nil, fmt.Errorf("no results found for %s", name)
|
||||
}
|
||||
|
||||
log.Printf("Got %d last per100 foods for %s", len(res), name)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *FoodService) Create(food Food) (Food, error) {
|
||||
func (s *FoodService) Create(food Food) (res Food, err error) {
|
||||
log.Printf("Creating food %v", food)
|
||||
if s.db == nil || !s.db.Ready {
|
||||
return food, fmt.Errorf("cannot create food, db is nil or is not ready")
|
||||
}
|
||||
@@ -115,29 +116,30 @@ func (s *FoodService) Create(food Food) (Food, error) {
|
||||
return food, fmt.Errorf("cannot create food, amount is less than or equal to 0")
|
||||
}
|
||||
|
||||
var res sql.Result
|
||||
var err error
|
||||
var dbres sql.Result
|
||||
if food.Per100 > 0 {
|
||||
res, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount, per100) VALUES (?, ?, ?, ?)", food.Food, food.Descripton, food.Amount, food.Per100)
|
||||
dbres, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount, per100) VALUES (?, ?, ?, ?)", food.Food, food.Descripton, food.Amount, food.Per100)
|
||||
if err != nil {
|
||||
return food, fmt.Errorf("error inserting food: %v", err)
|
||||
return food, fmt.Errorf("error inserting food: %w", err)
|
||||
}
|
||||
} else {
|
||||
res, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount) VALUES (?, ?, ?)", food.Food, food.Descripton, food.Amount)
|
||||
dbres, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount) VALUES (?, ?, ?)", food.Food, food.Descripton, food.Amount)
|
||||
if err != nil {
|
||||
return food, fmt.Errorf("error inserting food: %v", err)
|
||||
return food, fmt.Errorf("error inserting food: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
id, err := res.LastInsertId()
|
||||
id, err := dbres.LastInsertId()
|
||||
if err != nil {
|
||||
return food, fmt.Errorf("error getting last insert id: %v", err)
|
||||
return food, fmt.Errorf("error getting last insert id: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("Created food %s with id %d", food.Food, id)
|
||||
return s.GetById(id)
|
||||
}
|
||||
|
||||
func (s *FoodService) Update(food Food) (Food, error) {
|
||||
func (s *FoodService) Update(food Food) (res Food, err error) {
|
||||
log.Printf("Updating food %s with id %d", food.Food, food.Id)
|
||||
if s.db == nil || !s.db.Ready {
|
||||
return food, fmt.Errorf("cannot update food, db is nil or is not ready")
|
||||
}
|
||||
@@ -154,45 +156,46 @@ func (s *FoodService) Update(food Food) (Food, error) {
|
||||
if food.Per100 > 0 {
|
||||
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ?, per100 = ? WHERE Id = ?", food.Food, food.Descripton, food.Amount, food.Per100, food.Id)
|
||||
if err != nil {
|
||||
return food, fmt.Errorf("error updating food: %v", err)
|
||||
return food, fmt.Errorf("error updating food: %w", err)
|
||||
}
|
||||
} else {
|
||||
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ? WHERE Id = ?", food.Food, food.Descripton, food.Amount, food.Id)
|
||||
if err != nil {
|
||||
return food, fmt.Errorf("error updating food: %v", err)
|
||||
return food, fmt.Errorf("error updating food: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Updated food %s with id %d", food.Food, food.Id)
|
||||
return s.GetById(food.Id)
|
||||
}
|
||||
|
||||
func (s *FoodService) GetById(id int64) (Food, error) {
|
||||
var res Food
|
||||
func (s *FoodService) GetById(id int64) (res Food, err error) {
|
||||
log.Printf("Getting food by id %d", id)
|
||||
if s.db == nil || !s.db.Ready {
|
||||
return res, fmt.Errorf("cannot get food by id, db is nil or is not ready")
|
||||
}
|
||||
|
||||
row := s.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from foodView WHERE id = ?", foodColumns), id)
|
||||
err := row.Scan(&res.Id, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy)
|
||||
err = row.Scan(&res.Id, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("error scanning row: %v", err)
|
||||
return res, fmt.Errorf("error scanning row: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("Got food %s with id %d", res.Food, res.Id)
|
||||
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) {
|
||||
res := []AggregatedFood{}
|
||||
func (s *FoodService) GetDaily() (res []AggregatedFood, err error) {
|
||||
log.Printf("Getting daily food")
|
||||
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
|
||||
return res, fmt.Errorf("error getting daily food: %w", err)
|
||||
}
|
||||
|
||||
for row.Next() {
|
||||
@@ -206,24 +209,24 @@ func (s *FoodService) GetDaily() ([]AggregatedFood, error) {
|
||||
res = append(res, food)
|
||||
}
|
||||
|
||||
log.Printf("Got %d daily foods", len(res))
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *FoodService) GetWeekly() ([]AggregatedFood, error) {
|
||||
res := []AggregatedFood{}
|
||||
func (s *FoodService) GetWeekly() (res []AggregatedFood, err error) {
|
||||
log.Printf("Getting weekly food")
|
||||
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
|
||||
return res, fmt.Errorf("error getting weekly food: %w", err)
|
||||
}
|
||||
|
||||
for row.Next() {
|
||||
var food AggregatedFood
|
||||
err := row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
|
||||
err = row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
|
||||
if err != nil {
|
||||
log.Printf("error scanning row: %v", err)
|
||||
continue
|
||||
@@ -232,24 +235,24 @@ func (s *FoodService) GetWeekly() ([]AggregatedFood, error) {
|
||||
res = append(res, food)
|
||||
}
|
||||
|
||||
log.Printf("Got %d weekly foods", len(res))
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *FoodService) GetMonthly() ([]AggregatedFood, error) {
|
||||
res := []AggregatedFood{}
|
||||
func (s *FoodService) GetMonthly() (res []AggregatedFood, err error) {
|
||||
log.Printf("Getting monthly food")
|
||||
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
|
||||
return res, fmt.Errorf("error getting monthly food: %w", err)
|
||||
}
|
||||
|
||||
for row.Next() {
|
||||
var food AggregatedFood
|
||||
err := row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
|
||||
err = row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
|
||||
if err != nil {
|
||||
log.Printf("error scanning row: %v", err)
|
||||
continue
|
||||
@@ -258,24 +261,24 @@ func (s *FoodService) GetMonthly() ([]AggregatedFood, error) {
|
||||
res = append(res, food)
|
||||
}
|
||||
|
||||
log.Printf("Got %d monthly foods", len(res))
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *FoodService) GetYearly() ([]AggregatedFood, error) {
|
||||
res := []AggregatedFood{}
|
||||
func (s *FoodService) GetYearly() (res []AggregatedFood, err error) {
|
||||
log.Printf("Getting yearly food")
|
||||
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 yearly food: %v", err)
|
||||
return res, err
|
||||
return res, fmt.Errorf("error getting yearly food: %w", err)
|
||||
}
|
||||
|
||||
for row.Next() {
|
||||
var food AggregatedFood
|
||||
err := row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
|
||||
err = row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
|
||||
if err != nil {
|
||||
log.Printf("error scanning row: %v", err)
|
||||
continue
|
||||
@@ -284,5 +287,6 @@ func (s *FoodService) GetYearly() ([]AggregatedFood, error) {
|
||||
res = append(res, food)
|
||||
}
|
||||
|
||||
log.Printf("Got %d yearly foods", len(res))
|
||||
return res, nil
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@@ -6,7 +6,7 @@ toolchain go1.22.4
|
||||
|
||||
require (
|
||||
github.com/mattn/go-sqlite3 v1.14.22
|
||||
github.com/wailsapp/wails/v2 v2.9.1
|
||||
github.com/wailsapp/wails/v2 v2.9.2
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -30,7 +30,7 @@ require (
|
||||
github.com/tkrajina/go-reflector v0.5.6 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
github.com/wailsapp/go-webview2 v1.0.10 // indirect
|
||||
github.com/wailsapp/go-webview2 v1.0.16 // indirect
|
||||
github.com/wailsapp/mimetype v1.4.1 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
|
||||
|
8
go.sum
8
go.sum
@@ -59,12 +59,12 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
|
||||
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
github.com/wailsapp/go-webview2 v1.0.10 h1:PP5Hug6pnQEAhfRzLCoOh2jJaPdrqeRgJKZhyYyDV/w=
|
||||
github.com/wailsapp/go-webview2 v1.0.10/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo=
|
||||
github.com/wailsapp/go-webview2 v1.0.16 h1:wffnvnkkLvhRex/aOrA3R7FP7rkvOqL/bir1br7BekU=
|
||||
github.com/wailsapp/go-webview2 v1.0.16/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo=
|
||||
github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs=
|
||||
github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o=
|
||||
github.com/wailsapp/wails/v2 v2.9.1 h1:irsXnoQrCpeKzKTYZ2SUVlRRyeMR6I0vCO9Q1cvlEdc=
|
||||
github.com/wailsapp/wails/v2 v2.9.1/go.mod h1:7maJV2h+Egl11Ak8QZN/jlGLj2wg05bsQS+ywJPT0gI=
|
||||
github.com/wailsapp/wails/v2 v2.9.2 h1:Xb5YRTos1w5N7DTMyYegWaGukCP2fIaX9WF21kPPF2k=
|
||||
github.com/wailsapp/wails/v2 v2.9.2/go.mod h1:uehvlCwJSFcBq7rMCGfk4rxca67QQGsbg5Nm4m9UnBs=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
|
||||
|
189
migratev2.sql
Normal file
189
migratev2.sql
Normal file
@@ -0,0 +1,189 @@
|
||||
drop index weightDateIdx;
|
||||
drop index weightDailyIdx;
|
||||
drop index weightWeeklyIdx;
|
||||
drop index weightMonthlyIdx;
|
||||
drop index weightYearlyIdx;
|
||||
|
||||
create table weight2 (
|
||||
id integer primary key,
|
||||
date datetime default (datetime('now')),
|
||||
weight real not null
|
||||
);
|
||||
insert into weight2 select * from weight;
|
||||
drop table weight;
|
||||
|
||||
drop view if exists weightView;
|
||||
drop view if exists weightDaily;
|
||||
drop view if exists weightWeekly;
|
||||
drop view if exists weightMonthly;
|
||||
drop view if exists weightYearly;
|
||||
|
||||
alter table weight2 rename to weight;
|
||||
|
||||
create index weightDateIdx on weight(date);
|
||||
create index weightDailyIdx on weight(strftime('%Y-%m-%d', date));
|
||||
create index weightWeeklyIdx on weight(strftime('%Y-%W', date));
|
||||
create index weightMonthlyIdx on weight(strftime('%Y-%m', date));
|
||||
create index weightYearlyIdx on weight(strftime('%Y', date));
|
||||
|
||||
create view weightDaily as
|
||||
select strftime('%Y-%m-%d', date) as period,
|
||||
round(avg(weight), 2) as amount
|
||||
from weight
|
||||
group by strftime('%Y-%m-%d', date)
|
||||
order by date desc;
|
||||
|
||||
create view weightWeekly as
|
||||
select strftime('%Y-%W', date) as period,
|
||||
round(avg(weight), 2) as amount
|
||||
from weight
|
||||
group by strftime('%Y-%W', date)
|
||||
order by date desc;
|
||||
|
||||
create view weightMonthly as
|
||||
select strftime('%Y-%m', date) as period,
|
||||
round(avg(weight), 2) as amount
|
||||
from weight
|
||||
group by strftime('%Y-%m', date)
|
||||
order by date desc;
|
||||
|
||||
create view weightYearly as
|
||||
select strftime('%Y', date) as period,
|
||||
round(avg(weight), 2) as amount
|
||||
from weight
|
||||
group by strftime('%Y', date)
|
||||
order by date desc;
|
||||
|
||||
|
||||
create table food2 (
|
||||
id integer primary key,
|
||||
date datetime default (datetime('now')),
|
||||
food varchar not null,
|
||||
description varchar,
|
||||
amount real not null,
|
||||
per100 real default 0,
|
||||
energy generated always as (coalesce(amount, 0) * coalesce(per100, 0) / 100) stored
|
||||
);
|
||||
|
||||
drop index dailyIdx;
|
||||
drop index weeklyIdx;
|
||||
drop index monthlyIdx;
|
||||
drop index yearlyIdx;
|
||||
drop index dateIdx;
|
||||
drop index foodIdx;
|
||||
|
||||
drop view if exists foodView;
|
||||
drop view if exists foodDaily;
|
||||
drop view if exists foodWeekly;
|
||||
drop view if exists foodMonthly;
|
||||
drop view if exists foodYearly;
|
||||
drop view if exists foodRecent;
|
||||
|
||||
insert into food2(date, food, description, amount) select date, food, description, amount from food;
|
||||
drop table food;
|
||||
alter table food2 rename to food;
|
||||
|
||||
create index dailyIdx on food(strftime('%Y-%m-%d', date));
|
||||
create index weeklyIdx on food(strftime('%Y-%W', date));
|
||||
create index monthlyIdx on food(strftime('%Y-%m', date));
|
||||
create index yearlyIdx on food(strftime('%Y', date));
|
||||
create index dateIdx on food(date);
|
||||
create index foodIdx on food(food);
|
||||
|
||||
create view foodView as
|
||||
select id,
|
||||
date,
|
||||
food,
|
||||
description,
|
||||
round(amount, 2) as amount,
|
||||
round(per100, 2) as per100,
|
||||
round(energy, 2) as energy
|
||||
from food;
|
||||
|
||||
create view foodDaily as
|
||||
select strftime('%Y-%m-%d', date) as period,
|
||||
round(sum(amount), 2) as amount,
|
||||
round(avg(per100), 2) as avgPer100,
|
||||
round(sum(energy), 2) as energy
|
||||
from food
|
||||
group by strftime('%Y-%m-%d', date)
|
||||
order by date desc;
|
||||
|
||||
create view foodWeekly as
|
||||
select strftime('%Y-%W', date) as period,
|
||||
round(sum(amount), 2) as amount,
|
||||
round(avg(per100), 2) as avgPer100,
|
||||
round(sum(energy), 2) as energy
|
||||
from food
|
||||
group by strftime('%Y-%W', date)
|
||||
order by date desc;
|
||||
|
||||
create view foodMonthly as
|
||||
select strftime('%Y-%m', date) as period,
|
||||
round(sum(amount), 2) as amount,
|
||||
round(avg(per100), 2) as avgPer100,
|
||||
round(sum(energy), 2) as energy
|
||||
from food
|
||||
group by strftime('%Y-%m', date)
|
||||
order by date desc;
|
||||
|
||||
create view foodYearly as
|
||||
select strftime('%Y', date) as period,
|
||||
round(sum(amount), 2) as amount,
|
||||
round(avg(per100), 2) as avgPer100,
|
||||
round(sum(energy), 2) as energy
|
||||
from food
|
||||
group by strftime('%Y', date)
|
||||
order by date desc;
|
||||
|
||||
create view foodRecent as
|
||||
select *
|
||||
from food
|
||||
order by date desc
|
||||
limit 10;
|
||||
|
||||
drop trigger if exists food_foodfix_insert;
|
||||
create trigger food_foodfix_insert AFTER
|
||||
insert on food for EACH row
|
||||
begin
|
||||
update foodfix
|
||||
set rank = rank + 1
|
||||
where word = new.food;
|
||||
insert into foodfix (word, rank)
|
||||
select new.food,
|
||||
1
|
||||
where not exists (
|
||||
select 1
|
||||
from foodfix
|
||||
where word = new.food
|
||||
);
|
||||
end;
|
||||
|
||||
drop trigger if exists food_foodfix_delete;
|
||||
create trigger food_foodfix_delete AFTER
|
||||
delete on food for EACH row
|
||||
begin
|
||||
update foodfix
|
||||
set rank = rank - 1
|
||||
where word = old.food;
|
||||
|
||||
delete from foodfix
|
||||
where word = old.food
|
||||
and rank <= 0;
|
||||
end;
|
||||
|
||||
drop trigger if exists food_foodfix_update;
|
||||
create trigger food_foodfix_update AFTER
|
||||
update on food for EACH row
|
||||
begin
|
||||
update foodfix
|
||||
set rank = rank - 1
|
||||
where word = old.food;
|
||||
end;
|
||||
|
||||
delete from foodfix;
|
||||
insert into foodfix (word, rank)
|
||||
select food as word,
|
||||
count(*) as rank
|
||||
from food
|
||||
group by food;
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user