1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package handlers
import (
"sort"
"task-dashboard/internal/models"
)
// groupMeals groups meals by date+mealType, combining recipe names into CombinedMeal entries.
// Results are sorted by date then meal type (breakfast → lunch → dinner).
func groupMeals(meals []models.Meal) []CombinedMeal {
type mealKey struct {
date string
mealType string
}
mealGroups := make(map[mealKey][]models.Meal)
for _, meal := range meals {
key := mealKey{
date: meal.Date.Format("2006-01-02"),
mealType: meal.MealType,
}
mealGroups[key] = append(mealGroups[key], meal)
}
var combined []CombinedMeal
for _, group := range mealGroups {
if len(group) == 0 {
continue
}
cm := CombinedMeal{
ID: group[0].ID,
Date: group[0].Date,
MealType: group[0].MealType,
RecipeURL: group[0].RecipeURL,
Meals: group,
}
for _, m := range group {
cm.RecipeNames = append(cm.RecipeNames, m.RecipeName)
}
combined = append(combined, cm)
}
sort.Slice(combined, func(i, j int) bool {
if !combined[i].Date.Equal(combined[j].Date) {
return combined[i].Date.Before(combined[j].Date)
}
return mealTypeOrder(combined[i].MealType) < mealTypeOrder(combined[j].MealType)
})
return combined
}
|