Skip to content

Commit

Permalink
Added meals and meallogs tables
Browse files Browse the repository at this point in the history
  • Loading branch information
szabolcs-horvath committed Dec 21, 2024
1 parent e366112 commit f8da897
Show file tree
Hide file tree
Showing 10 changed files with 590 additions and 23 deletions.
121 changes: 121 additions & 0 deletions repository/meallogs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package repository

import (
"context"
sqlc "github.com/szabolcs-horvath/nutrition-tracker/generated"
"time"
)

type MealLog struct {
ID int64
Meal *Meal
Item *Item
Portion *Portion
PortionMultiplier float64
DateTime time.Time
}

func (m MealLog) getMultiplier() float64 {
return m.PortionMultiplier * m.Portion.getMultiplier()
}

func (m MealLog) GetCalories() float64 {
return m.getMultiplier() * m.Item.CaloriesPer100
}

func (m MealLog) GetFats() float64 {
return m.getMultiplier() * m.Item.FatsPer100
}

func (m MealLog) GetFatsSaturated() *float64 {
var result float64
if m.Item.FatsSaturatedPer100 != nil {
result = m.getMultiplier() * *m.Item.FatsSaturatedPer100
}
return &result
}

func (m MealLog) GetCarbs() float64 {
return m.getMultiplier() * m.Item.CarbsPer100
}

func (m MealLog) GetCarbsSugar() *float64 {
var result float64
if m.Item.CarbsSugarPer100 != nil {
result = m.getMultiplier() * *m.Item.CarbsSugarPer100
}
return &result
}

func (m MealLog) GetCarbsSlowRelease() *float64 {
var result float64
if m.Item.CarbsSlowReleasePer100 != nil {
result = m.getMultiplier() * *m.Item.CarbsSlowReleasePer100
}
return &result
}

func (m MealLog) GetCarbsFastRelease() *float64 {
var result float64
if m.Item.CarbsFastReleasePer100 != nil {
result = m.getMultiplier() * *m.Item.CarbsFastReleasePer100
}
return &result
}

func (m MealLog) GetProteins() float64 {
return m.getMultiplier() * m.Item.ProteinsPer100
}

func (m MealLog) GetSalt() *float64 {
var result float64
if m.Item.SaltPer100 != nil {
result = m.getMultiplier() * *m.Item.SaltPer100
}
return &result
}

func convertMealLog(mealLog *sqlc.MealLog_sqlc) *MealLog {
return &MealLog{
ID: mealLog.ID,
PortionMultiplier: mealLog.PortionMultiplier,
DateTime: mealLog.Datetime,
}
}

func CreateMealLog(ctx context.Context, mealLog *MealLog) (*MealLog, error) {
queries, err := GetQueries()
if err != nil {
return nil, err
}
mealLogSqlc, err := queries.CreateMealLog(ctx, sqlc.CreateMealLogParams{
MealID: mealLog.Meal.ID,
ItemID: mealLog.Item.ID,
PortionID: mealLog.Portion.ID,
PortionMultiplier: mealLog.PortionMultiplier,
Datetime: mealLog.DateTime,
})
if err != nil {
return nil, err
}
return convertMealLog(&mealLogSqlc), nil
}

func UpdateMealLog(ctx context.Context, mealLog *MealLog) (*MealLog, error) {
queries, err := GetQueries()
if err != nil {
return nil, err
}
mealLogSqlc, err := queries.UpdateMealLog(ctx, sqlc.UpdateMealLogParams{
MealID: mealLog.Meal.ID,
ItemID: mealLog.Item.ID,
PortionID: mealLog.Portion.ID,
PortionMultiplier: mealLog.PortionMultiplier,
Datetime: mealLog.DateTime,
ID: mealLog.ID,
})
if err != nil {
return nil, err
}
return convertMealLog(&mealLogSqlc), nil
}
144 changes: 144 additions & 0 deletions repository/meals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package repository

import (
"context"
sqlc "github.com/szabolcs-horvath/nutrition-tracker/generated"
"time"
)

type Meal struct {
ID int64
Owner *User
Notification *Notification
Name string
Time time.Time
CaloriesQuota *float64
FatsQuota *float64
FatsSaturatedQuota *float64
CarbsQuota *float64
CarbsSugarQuota *float64
CarbsSlowReleaseQuota *float64
CarbsFastReleaseQuota *float64
ProteinsQuota *float64
SaltQuota *float64
Archived bool
}

func convertMeal(meal *sqlc.Meal_sqlc) *Meal {
return &Meal{
ID: meal.ID,
Name: meal.Name,
Time: meal.Time,
CaloriesQuota: meal.CaloriesQuota,
FatsQuota: meal.FatsQuota,
FatsSaturatedQuota: meal.FatsSaturatedQuota,
CarbsQuota: meal.CarbsQuota,
CarbsSugarQuota: meal.CarbsSugarQuota,
CarbsSlowReleaseQuota: meal.CarbsSlowReleaseQuota,
CarbsFastReleaseQuota: meal.CarbsFastReleaseQuota,
ProteinsQuota: meal.ProteinsQuota,
SaltQuota: meal.SaltQuota,
Archived: meal.Archived,
}
}

func FindNonArchivedMealsForUser(ctx context.Context, ownerId int64) ([]*Meal, error) {
queries, err := GetQueries()
if err != nil {
return nil, err
}
list, err := queries.FindNonArchivedMealsForUser(ctx, ownerId)
if err != nil {
return nil, err
}
result := make([]*Meal, len(list))
for i, m := range list {
result[i] = convertMeal(&m.MealSqlc)
result[i].Owner = convertUser(UserSqlcWrapper{m.UserSqlc})
result[i].Notification = convertNotification(MealsNotificationsViewWrapper{m.MealsNotificationsView})
}
return result, nil
}

func CreateMeal(ctx context.Context, meal *Meal) (*Meal, error) {
db, err := GetDB()
if err != nil {
return nil, err
}
var result *Meal
err = DoInTransaction(ctx, db, func(queries *sqlc.Queries) error {
notificationSqlc, notiErr := queries.CreateNotification(ctx, sqlc.CreateNotificationParams{
OwnerID: meal.Notification.Owner.ID,
Time: meal.Notification.Time,
Delay: meal.Notification.Delay,
DelayDate: meal.Notification.DelayDate,
Name: meal.Notification.Name,
})
if notiErr != nil {
return notiErr
}
mealSqlc, mealErr := queries.CreateMeal(ctx, sqlc.CreateMealParams{
OwnerID: meal.Owner.ID,
NotificationID: meal.Notification.ID,
Name: meal.Name,
Time: meal.Time,
CaloriesQuota: meal.CaloriesQuota,
FatsQuota: meal.FatsQuota,
FatsSaturatedQuota: meal.FatsSaturatedQuota,
CarbsQuota: meal.CarbsQuota,
CarbsSugarQuota: meal.CarbsSugarQuota,
CarbsSlowReleaseQuota: meal.CarbsSlowReleaseQuota,
CarbsFastReleaseQuota: meal.CarbsFastReleaseQuota,
ProteinsQuota: meal.ProteinsQuota,
SaltQuota: meal.SaltQuota,
})
if mealErr != nil {
return err
}
result = convertMeal(&mealSqlc)
result.Notification = convertNotification(NotificationSqlcWrapper{notificationSqlc})
return nil
})
if err != nil {
return nil, err
}
return result, nil
}

func UpdateMeal(ctx context.Context, meal *Meal) (*Meal, error) {
queries, err := GetQueries()
if err != nil {
return nil, err
}
mealSqlc, err := queries.UpdateMeal(ctx, sqlc.UpdateMealParams{
OwnerID: meal.Owner.ID,
NotificationID: meal.Notification.ID,
Name: meal.Name,
Time: meal.Time,
CaloriesQuota: meal.CaloriesQuota,
FatsQuota: meal.FatsQuota,
FatsSaturatedQuota: meal.FatsSaturatedQuota,
CarbsQuota: meal.CarbsQuota,
CarbsSugarQuota: meal.CarbsSugarQuota,
CarbsSlowReleaseQuota: meal.CarbsSlowReleaseQuota,
CarbsFastReleaseQuota: meal.CarbsFastReleaseQuota,
ProteinsQuota: meal.ProteinsQuota,
SaltQuota: meal.SaltQuota,
ID: meal.ID,
})
if err != nil {
return nil, err
}
return convertMeal(&mealSqlc), nil
}

func ArchiveMeal(ctx context.Context, mealId int64) error {
queries, err := GetQueries()
if err != nil {
return err
}
if err = queries.ArchiveMeal(ctx, mealId); err != nil {
return err
}
return nil
}
Loading

0 comments on commit f8da897

Please sign in to comment.