Skip to content
Snippets Groups Projects
Commit e1ca1fce authored by Sercan Yesildal's avatar Sercan Yesildal
Browse files

added user shopping list

parent 34526cff
No related branches found
No related tags found
No related merge requests found
...@@ -106,6 +106,20 @@ func GetUser(id int) (*model.User, error) { ...@@ -106,6 +106,20 @@ func GetUser(id int) (*model.User, error) {
return nil, errors.New("account not found") 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) queryMeals, err := udb.Query("SELECT * FROM userMeal WHERE userId = ?;", id)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -44,3 +44,17 @@ func DeleteShoppingDate(w http.ResponseWriter, r *http.Request) { ...@@ -44,3 +44,17 @@ func DeleteShoppingDate(w http.ResponseWriter, r *http.Request) {
} }
sendJson(w, nil) 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)
}
...@@ -33,6 +33,7 @@ func init() { ...@@ -33,6 +33,7 @@ func init() {
func main() { func main() {
port := 8000 port := 8000
router := mux.NewRouter() 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/{id}", handler.DeleteShoppingDate).Methods(http.MethodDelete)
router.HandleFunc("/user/date", handler.CreateShoppingDate).Methods(http.MethodPost) router.HandleFunc("/user/date", handler.CreateShoppingDate).Methods(http.MethodPost)
router.HandleFunc("/user/meal/{id}", handler.DeleteUserMeal).Methods(http.MethodDelete) router.HandleFunc("/user/meal/{id}", handler.DeleteUserMeal).Methods(http.MethodDelete)
......
...@@ -10,3 +10,8 @@ type ShoppingDateWithId struct { ...@@ -10,3 +10,8 @@ type ShoppingDateWithId struct {
UserID int `json:"userId"` UserID int `json:"userId"`
Date string `json:"date"` Date string `json:"date"`
} }
type ShoppingLists struct {
Name string `json:"name"`
Lists map[string][]*Ingredient `json:"lists"`
}
...@@ -5,6 +5,7 @@ type User struct { ...@@ -5,6 +5,7 @@ type User struct {
Mail string `json:"mail"` Mail string `json:"mail"`
FirstName string `json:"firstName"` FirstName string `json:"firstName"`
LastName string `json:"lastName"` LastName string `json:"lastName"`
GroupIds []int `json:"groupIds"`
Meals []UserMealWithId `json:"meals"` Meals []UserMealWithId `json:"meals"`
ShoppingDates []ShoppingDateWithId `json:"shoppingDates"` ShoppingDates []ShoppingDateWithId `json:"shoppingDates"`
} }
......
package service package service
import ( 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/db"
"gitlab.reutlingen-university.de/yesildas/mealplanner2go/service/planner/model" "gitlab.reutlingen-university.de/yesildas/mealplanner2go/service/planner/model"
) )
...@@ -22,3 +26,59 @@ func DeleteShoppingDate(id int) error { ...@@ -22,3 +26,59 @@ func DeleteShoppingDate(id int) error {
return nil 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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment