Actually export fields of food
IDIOT
This commit is contained in:
@@ -11,19 +11,19 @@ type (
|
|||||||
db *DB
|
db *DB
|
||||||
}
|
}
|
||||||
Food struct {
|
Food struct {
|
||||||
rowid int64 `json:"rowid"`
|
Rowid int64 `json:"rowid"`
|
||||||
date string `json:"date"`
|
Date string `json:"date"`
|
||||||
food string `json:"food"`
|
Food string `json:"food"`
|
||||||
descripton string `json:"description"`
|
Descripton string `json:"description"`
|
||||||
amount float32 `json:"amount"`
|
Amount float32 `json:"amount"`
|
||||||
per100 float32 `json:"per100"`
|
Per100 float32 `json:"per100"`
|
||||||
energy float32 `json:"energy"`
|
Energy float32 `json:"energy"`
|
||||||
}
|
}
|
||||||
AggregatedFood struct {
|
AggregatedFood struct {
|
||||||
period string `json:"period"`
|
Period string `json:"period"`
|
||||||
amount float32 `json:"amount"`
|
Amount float32 `json:"amount"`
|
||||||
avgPer100 float32 `json:"avgPer100"`
|
AvgPer100 float32 `json:"avgPer100"`
|
||||||
energy float32 `json:"energy"`
|
Energy float32 `json:"energy"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ func (s *FoodService) GetRecent() ([]Food, error) {
|
|||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var food Food
|
var food Food
|
||||||
err := row.Scan(&food.rowid, &food.date, &food.food, &food.descripton, &food.amount, &food.per100, &food.energy)
|
err := row.Scan(&food.Rowid, &food.Date, &food.Food, &food.Descripton, &food.Amount, &food.Per100, &food.Energy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error scanning row: %v", err)
|
log.Printf("error scanning row: %v", err)
|
||||||
continue
|
continue
|
||||||
@@ -60,22 +60,22 @@ func (s *FoodService) Create(food Food) (Food, error) {
|
|||||||
if s.db == nil || !s.db.Ready {
|
if s.db == nil || !s.db.Ready {
|
||||||
return food, fmt.Errorf("cannot create food, db is nil or is not ready")
|
return food, fmt.Errorf("cannot create food, db is nil or is not ready")
|
||||||
}
|
}
|
||||||
if food.food == "" {
|
if food.Food == "" {
|
||||||
return food, fmt.Errorf("cannot create food, food is empty")
|
return food, fmt.Errorf("cannot create food, food is empty")
|
||||||
}
|
}
|
||||||
if food.amount <= 0 {
|
if food.Amount <= 0 {
|
||||||
return food, fmt.Errorf("cannot create food, amount is less than or equal to 0")
|
return food, fmt.Errorf("cannot create food, amount is less than or equal to 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
var res sql.Result
|
var res sql.Result
|
||||||
var err error
|
var err error
|
||||||
if food.per100 > 0 {
|
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)
|
res, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount, per100) VALUES (?, ?, ?, ?)", food.Food, food.Descripton, food.Amount, food.Per100)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return food, fmt.Errorf("error inserting food: %v", err)
|
return food, fmt.Errorf("error inserting food: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount) VALUES (?, ?, ?)", food.food, food.descripton, food.amount)
|
res, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount) VALUES (?, ?, ?)", food.Food, food.Descripton, food.Amount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return food, fmt.Errorf("error inserting food: %v", err)
|
return food, fmt.Errorf("error inserting food: %v", err)
|
||||||
}
|
}
|
||||||
@@ -93,29 +93,29 @@ func (s *FoodService) Update(food Food) (Food, error) {
|
|||||||
if s.db == nil || !s.db.Ready {
|
if s.db == nil || !s.db.Ready {
|
||||||
return food, fmt.Errorf("cannot update food, db is nil or is not ready")
|
return food, fmt.Errorf("cannot update food, db is nil or is not ready")
|
||||||
}
|
}
|
||||||
if food.rowid <= 0 {
|
if food.Rowid <= 0 {
|
||||||
return food, fmt.Errorf("cannot update food, rowid is less than or equal to 0")
|
return food, fmt.Errorf("cannot update food, rowid is less than or equal to 0")
|
||||||
}
|
}
|
||||||
if food.food == "" {
|
if food.Food == "" {
|
||||||
return food, fmt.Errorf("cannot update food, food is empty")
|
return food, fmt.Errorf("cannot update food, food is empty")
|
||||||
}
|
}
|
||||||
if food.amount <= 0 {
|
if food.Amount <= 0 {
|
||||||
return food, fmt.Errorf("cannot update food, amount is less than or equal to 0")
|
return food, fmt.Errorf("cannot update food, amount is less than or equal to 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
if food.per100 > 0 {
|
if food.Per100 > 0 {
|
||||||
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ?, per100 = ? WHERE rowid = ?", food.food, food.descripton, food.amount, food.per100, food.rowid)
|
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ?, per100 = ? WHERE rowid = ?", food.Food, food.Descripton, food.Amount, food.Per100, food.Rowid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return food, fmt.Errorf("error updating food: %v", err)
|
return food, fmt.Errorf("error updating food: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ? WHERE rowid = ?", food.food, food.descripton, food.amount, food.rowid)
|
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ? WHERE rowid = ?", food.Food, food.Descripton, food.Amount, food.Rowid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return food, fmt.Errorf("error updating food: %v", err)
|
return food, fmt.Errorf("error updating food: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.GetByRowid(food.rowid)
|
return s.GetByRowid(food.Rowid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FoodService) GetByRowid(rowid int64) (Food, error) {
|
func (s *FoodService) GetByRowid(rowid int64) (Food, error) {
|
||||||
@@ -125,7 +125,7 @@ func (s *FoodService) GetByRowid(rowid int64) (Food, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
row := s.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from foodView WHERE rowid = ?", foodColumns), rowid)
|
row := s.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from foodView WHERE rowid = ?", foodColumns), rowid)
|
||||||
err := row.Scan(&res.rowid, &res.date, &res.food, &res.descripton, &res.amount, &res.per100, &res.energy)
|
err := row.Scan(&res.Rowid, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, fmt.Errorf("error scanning row: %v", err)
|
return res, fmt.Errorf("error scanning row: %v", err)
|
||||||
}
|
}
|
||||||
@@ -149,7 +149,7 @@ func (s *FoodService) GetDaily() ([]AggregatedFood, error) {
|
|||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var food AggregatedFood
|
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 {
|
if err != nil {
|
||||||
log.Printf("error scanning row: %v", err)
|
log.Printf("error scanning row: %v", err)
|
||||||
continue
|
continue
|
||||||
@@ -175,7 +175,7 @@ func (s *FoodService) GetWeekly() ([]AggregatedFood, error) {
|
|||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var food AggregatedFood
|
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 {
|
if err != nil {
|
||||||
log.Printf("error scanning row: %v", err)
|
log.Printf("error scanning row: %v", err)
|
||||||
continue
|
continue
|
||||||
@@ -201,7 +201,7 @@ func (s *FoodService) GetMonthly() ([]AggregatedFood, error) {
|
|||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var food AggregatedFood
|
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 {
|
if err != nil {
|
||||||
log.Printf("error scanning row: %v", err)
|
log.Printf("error scanning row: %v", err)
|
||||||
continue
|
continue
|
||||||
@@ -227,7 +227,7 @@ func (s *FoodService) GetYearly() ([]AggregatedFood, error) {
|
|||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var food AggregatedFood
|
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 {
|
if err != nil {
|
||||||
log.Printf("error scanning row: %v", err)
|
log.Printf("error scanning row: %v", err)
|
||||||
continue
|
continue
|
||||||
|
Reference in New Issue
Block a user