diff --git a/service/planner/db/client.go b/service/planner/db/client.go index 6dc7b5e582ce909bc6a7e54f96a3ad0f700fe39f..2a8e4b4f021317eba4020e48803205b808f0b2c8 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 ab6254d723d40b289af79434008469cc2c4c38c3..55fa1ebb976d323c8f285c76c71acc6182791254 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 af28c06b8a98e202140046aaf832c6a42a958e8c..f17e725146f8dce38f60529f8a22cbf47a6772d0 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 892a0702f4a62eb5bdaf713c90e787c99a42316b..c5921d04f4644278e2507d4fb5a93853afbb3798 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 86dd23f30353f5b260dbbf2c3560b2d4346165eb..58d6325e1d46a8dc38241955211e90efb4c027cc 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 296d0f4182cfcfd60ff87135b9922ccb2f150fc5..8763772689652bea0def2edc58b844b53bd8736e 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 +}