summaryrefslogtreecommitdiff
path: root/internal/handlers/meals.go
diff options
context:
space:
mode:
authorClaude <agent@claude.ai>2026-03-18 10:04:57 +0000
committerClaude <agent@claude.ai>2026-03-18 10:04:57 +0000
commite85b42d373de55781af9d699b246c0d6a492aec1 (patch)
tree50e40abda62e60144186c1916ddd0f683533c2f4 /internal/handlers/meals.go
parente3195a6534bae000a63e884ff647fac95d9d2498 (diff)
refactor: RF-03/06 extract groupMeals helper, eliminate convertSyncItemToTask wrapper
RF-03: Extract shared groupMeals helper into internal/handlers/meals.go. Both HandleTabMeals and BuildTimeline now call groupMeals instead of duplicating the date+mealType grouping algorithm inline. CombinedMeal gains ID and Meals fields to carry the first-meal ID and original records needed by BuildTimeline when constructing TimelineItems. RF-06: Add api.ConvertSyncItemToTask for single-item conversion. ConvertSyncItemsToTasks now delegates to it, eliminating duplication. The Handler.convertSyncItemToTask wrapper (which allocated a one-element slice just to unwrap it) is deleted; its caller uses api.ConvertSyncItemToTask directly. Covered by TestConvertSyncItemToTask in internal/api/todoist_test.go. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/handlers/meals.go')
-rw-r--r--internal/handlers/meals.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/internal/handlers/meals.go b/internal/handlers/meals.go
new file mode 100644
index 0000000..c4c327c
--- /dev/null
+++ b/internal/handlers/meals.go
@@ -0,0 +1,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
+}