diff options
| author | Peter Stone <thepeterstone@gmail.com> | 2026-01-12 09:27:16 -1000 |
|---|---|---|
| committer | Peter Stone <thepeterstone@gmail.com> | 2026-01-12 09:27:16 -1000 |
| commit | 9fe0998436488537a8a2e8ffeefb0c4424b41c60 (patch) | |
| tree | ce877f04e60a187c2bd0e481e80298ec5e7cdf80 /internal/api/plantoeat.go | |
Initial commit: Personal Consolidation Dashboard (Phase 1 Complete)
Implemented a unified web dashboard aggregating tasks, notes, and meal planning:
Core Features:
- Trello integration (PRIMARY feature - boards, cards, lists)
- Todoist integration (tasks and projects)
- Obsidian integration (20 most recent notes)
- PlanToEat integration (optional - 7-day meal planning)
- Mobile-responsive web UI with auto-refresh (5 min)
- SQLite caching with 5-minute TTL
- AI agent endpoint with Bearer token authentication
Technical Implementation:
- Go 1.21+ backend with chi router
- Interface-based API client design for testability
- Parallel data fetching with goroutines
- Graceful degradation (partial data on API failures)
- .env file loading with godotenv
- Comprehensive test coverage (9/9 tests passing)
Bug Fixes:
- Fixed .env file not being loaded at startup
- Fixed nil pointer dereference with optional API clients (typed nil interface gotcha)
Documentation:
- START_HERE.md - Quick 5-minute setup guide
- QUICKSTART.md - Fast track setup
- SETUP_GUIDE.md - Detailed step-by-step instructions
- PROJECT_SUMMARY.md - Complete project overview
- CLAUDE.md - Guide for Claude Code instances
- AI_AGENT_ACCESS.md - AI agent design document
- AI_AGENT_SETUP.md - Claude.ai integration guide
- TRELLO_AUTH_UPDATE.md - New Power-Up auth process
Statistics:
- Binary: 17MB
- Code: 2,667 lines
- Tests: 5 unit + 4 acceptance tests (all passing)
- Dependencies: chi, sqlite3, godotenv
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'internal/api/plantoeat.go')
| -rw-r--r-- | internal/api/plantoeat.go | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/internal/api/plantoeat.go b/internal/api/plantoeat.go new file mode 100644 index 0000000..6fe640d --- /dev/null +++ b/internal/api/plantoeat.go @@ -0,0 +1,138 @@ +package api + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "time" + + "task-dashboard/internal/models" +) + +const ( + planToEatBaseURL = "https://www.plantoeat.com/api/v2" +) + +// PlanToEatClient handles interactions with the PlanToEat API +type PlanToEatClient struct { + apiKey string + httpClient *http.Client +} + +// NewPlanToEatClient creates a new PlanToEat API client +func NewPlanToEatClient(apiKey string) *PlanToEatClient { + return &PlanToEatClient{ + apiKey: apiKey, + httpClient: &http.Client{ + Timeout: 30 * time.Second, + }, + } +} + +// planToEatPlannerItem represents a planner item from the API +type planToEatPlannerItem struct { + ID int `json:"id"` + Date string `json:"date"` + MealType string `json:"meal_type"` + Recipe struct { + ID int `json:"id"` + Title string `json:"title"` + URL string `json:"url"` + } `json:"recipe"` +} + +// planToEatResponse wraps the API response +type planToEatResponse struct { + Items []planToEatPlannerItem `json:"items"` +} + +// GetUpcomingMeals fetches meals for the next N days +func (c *PlanToEatClient) GetUpcomingMeals(ctx context.Context, days int) ([]models.Meal, error) { + if days <= 0 { + days = 7 // Default to 7 days + } + + startDate := time.Now() + endDate := startDate.AddDate(0, 0, days) + + req, err := http.NewRequestWithContext(ctx, "GET", planToEatBaseURL+"/planner_items", nil) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + // Add query parameters + q := req.URL.Query() + q.Add("start_date", startDate.Format("2006-01-02")) + q.Add("end_date", endDate.Format("2006-01-02")) + req.URL.RawQuery = q.Encode() + + // Add API key (check docs for correct header name) + req.Header.Set("Authorization", "Bearer "+c.apiKey) + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to fetch meals: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf("plantoeat API error (status %d): %s", resp.StatusCode, string(body)) + } + + var apiResponse planToEatResponse + if err := json.NewDecoder(resp.Body).Decode(&apiResponse); err != nil { + return nil, fmt.Errorf("failed to decode response: %w", err) + } + + // Convert to our model + meals := make([]models.Meal, 0, len(apiResponse.Items)) + for _, item := range apiResponse.Items { + mealDate, err := time.Parse("2006-01-02", item.Date) + if err != nil { + continue // Skip invalid dates + } + + meal := models.Meal{ + ID: fmt.Sprintf("%d", item.ID), + RecipeName: item.Recipe.Title, + Date: mealDate, + MealType: normalizeMealType(item.MealType), + RecipeURL: item.Recipe.URL, + } + + meals = append(meals, meal) + } + + return meals, nil +} + +// normalizeMealType ensures meal type matches our expected values +func normalizeMealType(mealType string) string { + switch mealType { + case "breakfast", "Breakfast": + return "breakfast" + case "lunch", "Lunch": + return "lunch" + case "dinner", "Dinner": + return "dinner" + case "snack", "Snack": + return "snack" + default: + return "dinner" // Default to dinner + } +} + +// GetRecipes fetches recipes (for Phase 2) +func (c *PlanToEatClient) GetRecipes(ctx context.Context) error { + // This will be implemented in Phase 2 + return fmt.Errorf("not implemented yet") +} + +// AddMealToPlanner adds a meal to the planner (for Phase 2) +func (c *PlanToEatClient) AddMealToPlanner(ctx context.Context, recipeID string, date time.Time, mealType string) error { + // This will be implemented in Phase 2 + return fmt.Errorf("not implemented yet") +} |
