# Database scripts SQL in this folder is applied **manually** on PostgreSQL. The API is database-first and does not run migrations. ## Diet tracking module Run on your DailyMeals database: ```bash psql "$DATABASE_URL" -f database/scripts/001_diet_tracking.sql ``` ### Tables created | Table | Purpose | |-------|---------| | `UserDailyGoals` | Daily calorie, macro, and water targets (per user) | | `DrinkCatalog` | Preset drinks (water, tea, coffee, …) with default volumes | | `MealConsumptions` | Meals the user ate (optional link to `Recipes`, macros snapshotted) | | `DrinkConsumptions` | Hydration / beverage log | | `DietReminders` | Reminder schedules (water, meal, custom) | ### API (after script + API deploy) | Method | Endpoint | |--------|----------| | GET | `/api/diet/summary?date=YYYY-MM-DD` | | GET/PUT | `/api/diet/goals` | | GET/POST/DELETE | `/api/diet/meals` | | GET | `/api/diet/drinks/catalog` | | GET/POST/DELETE | `/api/diet/drinks` | | GET/POST/PUT/DELETE | `/api/diet/reminders` | | GET | `/api/diet/reminders/due?date=&time=` | Web UI: **My Diet** at `/diet` in the React frontend. ## Ingredient nutrition catalog ```bash psql "$DATABASE_URL" -f database/scripts/002_ingredient_nutrition_catalog.sql ``` ### Tables created | Table | Purpose | |-------|---------| | `IngredientCategories` | Food groups (meat, vegetables, dairy, …) | | `IngredientNutritionCatalog` | Per-100 g calories, protein, fat, carbs, fiber | ### API (after script + API deploy) | Method | Endpoint | |--------|----------| | GET | `/api/ingredient-catalog/categories` | | GET | `/api/ingredient-catalog?category=&search=` | | GET | `/api/ingredient-catalog/{id}` | | POST | `/api/ingredient-catalog` | | PUT | `/api/ingredient-catalog/{id}` | | DELETE | `/api/ingredient-catalog/{id}` | Web UI: **Ingredient catalog** at `/ingredients` in all frontends. ### Polish ingredient names ```bash psql "$DATABASE_URL" -f database/scripts/003_ingredient_catalog_polish_names.sql ``` Adds `NamePl` column and Polish display names for all seeded catalog items. Search matches both English and Polish names. ## Extended features (meal planner, auth persistence, catalog linking) ```bash psql "$DATABASE_URL" -f database/scripts/004_extended_features.sql ``` ### Changes | Object | Purpose | |--------|---------| | `RefreshTokens` | Persist web login sessions across API restarts | | `MealPlanEntries` | Weekly meal planner (one recipe per meal slot per day) | | `UserFavoriteRecipes` | Quick-log favorites | | `Ingredients.CatalogItemId` | FK to nutrition catalog for auto macro calculation | Run **after** scripts 001–003. ## Catalog expansion + bulk ingredient linking One-time migration for existing recipe data (based on your `Ingredients.csv` export): ```bash psql "$DATABASE_URL" -f database/scripts/005_catalog_expansion_and_ingredient_linking.sql ``` ### What it does 1. **Adds ~70 catalog items** missing from the seed (Polish staples: rye bread, dry pasta/rice, feta, tahini, passata, herbs/spices, …). 2. **Links `Ingredients.CatalogItemId`** via a name→catalog mapping (~218 ingredient name variants). 3. **Recalculates `Recipes` macros** as `SUM(catalog × AmountGrams / 100)` for all recipes with linked ingredients. Reference mapping: `database/scripts/005_ingredient_name_mapping.csv` (ingredient name → catalog name → status). After the script, check PostgreSQL notices for unlinked rows and recipes still missing macros. Spice rows (`do smaku`, `szczypta`) and rows without grams are intentionally skipped. ### API alternative (after linking) If ingredients already have `CatalogItemId` set, you can recalculate without re-running SQL: | Method | Endpoint | |--------|----------| | POST | `/api/recipes/recalculate-macros` | Returns `{ recipesProcessed, recipesUpdated, unlinkedIngredientRows }`. Requires auth. Overwrites recipe macros when at least one linked ingredient has grams. ## AI generators (diet plan + recipes) ```bash psql "$DATABASE_URL" -f database/scripts/006_diet_and_recipe_generators.sql ``` Configure OpenAI in `MealPlan.Api/appsettings.Local.json`: ```json "OpenAI": { "ApiKey": "sk-...", "Model": "gpt-4.1-mini", "MacrosModel": "gpt-4.1-mini", "PlanModel": "gpt-4.1-mini", "RecipeModel": "gpt-4.1", "MaxTokens": 4096 } ``` | Setting | Used for | |---------|----------| | `MacrosModel` | Diet generator — refine daily kcal/macros | | `PlanModel` | Diet generator — pick recipes for the week | | `RecipeModel` | Recipe generator — ingredients + steps | | `Model` | Fallback when a task model is omitted | Without an API key, macro targets still use TDEE math and meal plans use a local greedy picker; recipe generation uses a simple fallback template. ### Diet generator API | Method | Endpoint | |--------|----------| | GET/PUT | `/api/diet-generator/profile` | | POST | `/api/diet-generator/sessions` | | GET | `/api/diet-generator/sessions/{id}` | | POST | `/api/diet-generator/sessions/{id}/propose-macros` | | POST | `/api/diet-generator/sessions/{id}/accept-macros` | | POST | `/api/diet-generator/sessions/{id}/generate-plan` | | POST | `/api/diet-generator/sessions/{id}/regenerate-meal` | | PATCH | `/api/diet-generator/sessions/{id}/meals/{mealId}` | | POST | `/api/diet-generator/sessions/{id}/commit` | Commit writes `UserDailyGoals` + `MealPlanEntries`. Daily calorie tolerance defaults to **10 kcal**. ### Recipe generator API | Method | Endpoint | |--------|----------| | POST | `/api/recipe-generator/sessions` | | GET | `/api/recipe-generator/sessions/{id}` | | POST | `/api/recipe-generator/sessions/{id}/generate` | | POST | `/api/recipe-generator/sessions/{id}/regenerate` | | PUT | `/api/recipe-generator/sessions/{id}/draft` | | POST | `/api/recipe-generator/sessions/{id}/commit` | Saved recipes get `Source = ai_generated` and macros from the nutrition catalog. Web UI: `/diet-generator` and `/recipe-generator` in the React frontend.