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

shopping balancer finished

parent e1ca1fce
No related branches found
No related tags found
No related merge requests found
......@@ -70,15 +70,78 @@ func GetShoppingList(id int) ([]*model.ShoppingLists, error) {
}
}
}
meal, err := GetMeal(v.MealID)
if err != nil {
return nil, err
if lowestDuration != 255*time.Hour*24*365 {
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...)
}
key := dates[currentIdx].Format(time.DateOnly)
userList.Lists[key] = append(userList.Lists[key], meal.Ingredients...)
}
}
result = append(result, &userList)
for _, groupId := range user.GroupIds {
group, err := GetGroup(groupId)
if err != nil {
continue
}
var groupList model.ShoppingLists
groupList.Name = group.Name
groupList.Lists = make(map[string][]*model.Ingredient)
groupDates := make(map[int][]time.Time)
for _, userId := range group.UserIDs {
groupUser, err := GetUser(userId)
if err != nil {
continue
}
for _, v := range groupUser.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) {
groupDates[userId] = append(groupDates[userId], date)
}
}
}
if len(group.Meals) != 0 {
for _, v := range group.Meals {
date, err := time.Parse(time.RFC3339, v.Date)
if err != nil {
return nil, err
}
currentUserId := 0
currentIdx := 0
lowestDuration := 255 * time.Hour * 24 * 365
for id, dates := range groupDates {
for idx, d := range dates {
if date.Equal(d) || date.After(d) {
if date.Sub(d) < lowestDuration {
currentUserId = id
currentIdx = idx
lowestDuration = date.Sub(d)
}
}
}
}
if lowestDuration != 255*time.Hour*24*365 {
if currentUserId == id {
meal, err := GetMeal(v.MealID)
if err != nil {
return nil, err
}
key := groupDates[currentUserId][currentIdx].Format(time.DateOnly)
groupList.Lists[key] = append(groupList.Lists[key], meal.Ingredients...)
}
}
}
}
result = append(result, &groupList)
}
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