From e1ca1fce2e3b3da6741aecee819e8f556f3788c4 Mon Sep 17 00:00:00 2001 From: Sercan Yesildal <sercan.yesildal@gmail.com> Date: Mon, 10 Jul 2023 17:43:37 +0200 Subject: [PATCH] added user shopping list --- service/planner/db/client.go | 14 +++++++ service/planner/handler/shopping.go | 14 +++++++ service/planner/main.go | 1 + service/planner/model/shopping.go | 5 +++ service/planner/model/user.go | 1 + service/planner/service/shopping.go | 60 +++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+) diff --git a/service/planner/db/client.go b/service/planner/db/client.go index 6dc7b5e..2a8e4b4 100644 --- a/service/planner/db/client.go +++ b/service/planner/db/client.go @@ -106,6 +106,20 @@ func GetUser(id int) (*model.User, error) { return nil, errors.New("account not found") } + queryGroupIds, err := udb.Query("SELECT groupId FROM userGroupRelation WHERE userId = ?;", id) + if err != nil { + return nil, err + } + defer queryGroupIds.Close() + for queryGroupIds.Next() { + var groupId int + err = queryGroupIds.Scan(&groupId) + if err != nil { + continue + } + user.GroupIds = append(user.GroupIds, groupId) + } + queryMeals, err := udb.Query("SELECT * FROM userMeal WHERE userId = ?;", id) if err != nil { return nil, err diff --git a/service/planner/handler/shopping.go b/service/planner/handler/shopping.go index ab6254d..55fa1eb 100644 --- a/service/planner/handler/shopping.go +++ b/service/planner/handler/shopping.go @@ -44,3 +44,17 @@ func DeleteShoppingDate(w http.ResponseWriter, r *http.Request) { } sendJson(w, nil) } + +func GetShoppingList(w http.ResponseWriter, r *http.Request) { + id, err := getId(r) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + users, err := service.GetShoppingList(id) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + sendJson(w, users) +} diff --git a/service/planner/main.go b/service/planner/main.go index af28c06..f17e725 100644 --- a/service/planner/main.go +++ b/service/planner/main.go @@ -33,6 +33,7 @@ func init() { func main() { port := 8000 router := mux.NewRouter() + router.HandleFunc("/user/list/{id}", handler.GetShoppingList).Methods(http.MethodGet) router.HandleFunc("/user/date/{id}", handler.DeleteShoppingDate).Methods(http.MethodDelete) router.HandleFunc("/user/date", handler.CreateShoppingDate).Methods(http.MethodPost) router.HandleFunc("/user/meal/{id}", handler.DeleteUserMeal).Methods(http.MethodDelete) diff --git a/service/planner/model/shopping.go b/service/planner/model/shopping.go index 892a070..c5921d0 100644 --- a/service/planner/model/shopping.go +++ b/service/planner/model/shopping.go @@ -10,3 +10,8 @@ type ShoppingDateWithId struct { UserID int `json:"userId"` Date string `json:"date"` } + +type ShoppingLists struct { + Name string `json:"name"` + Lists map[string][]*Ingredient `json:"lists"` +} diff --git a/service/planner/model/user.go b/service/planner/model/user.go index 86dd23f..58d6325 100644 --- a/service/planner/model/user.go +++ b/service/planner/model/user.go @@ -5,6 +5,7 @@ type User struct { Mail string `json:"mail"` FirstName string `json:"firstName"` LastName string `json:"lastName"` + GroupIds []int `json:"groupIds"` Meals []UserMealWithId `json:"meals"` ShoppingDates []ShoppingDateWithId `json:"shoppingDates"` } diff --git a/service/planner/service/shopping.go b/service/planner/service/shopping.go index 296d0f4..8763772 100644 --- a/service/planner/service/shopping.go +++ b/service/planner/service/shopping.go @@ -1,6 +1,10 @@ package service import ( + "errors" + "time" + + log "github.com/sirupsen/logrus" "gitlab.reutlingen-university.de/yesildas/mealplanner2go/service/planner/db" "gitlab.reutlingen-university.de/yesildas/mealplanner2go/service/planner/model" ) @@ -22,3 +26,59 @@ func DeleteShoppingDate(id int) error { return nil } + +func GetShoppingList(id int) ([]*model.ShoppingLists, error) { + var result []*model.ShoppingLists + user, err := db.GetUser(id) + if err != nil { + return nil, err + } + var dates []time.Time + for _, v := range user.ShoppingDates { + date, err := time.Parse(time.RFC3339, v.Date) + if err != nil { + return nil, err + } + timeNow := time.Now().Truncate(24 * time.Hour) + log.Infof("Compare time (%s, %s)", timeNow.Format(time.DateOnly), date.Format(time.DateOnly)) + if timeNow.Equal(date) || timeNow.Before(date) { + dates = append(dates, date) + } + } + + log.Infof("Valid shopping dates: %d", len(dates)) + if len(dates) == 0 { + return nil, errors.New("valid shopping date is missing") + } + + var userList model.ShoppingLists + userList.Name = user.FirstName + userList.Lists = make(map[string][]*model.Ingredient) + if len(user.Meals) != 0 { + for _, v := range user.Meals { + date, err := time.Parse(time.RFC3339, v.Date) + if err != nil { + return nil, err + } + currentIdx := 0 + lowestDuration := 255 * time.Hour * 24 * 365 + for idx, d := range dates { + if date.Equal(d) || date.After(d) { + if date.Sub(d) < lowestDuration { + currentIdx = idx + lowestDuration = date.Sub(d) + } + } + } + meal, err := GetMeal(v.MealID) + if err != nil { + return nil, err + } + key := dates[currentIdx].Format(time.DateOnly) + userList.Lists[key] = append(userList.Lists[key], meal.Ingredients...) + } + } + + result = append(result, &userList) + return result, nil +} -- GitLab