Files
DailyMeals/database/scripts/004_extended_features.sql
Piotr Kus f15bb7a916 * Extended functionalities
* Added different UIs
2026-06-24 21:43:40 +02:00

63 lines
3.0 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Extended features: refresh tokens, meal planner, favorites, recipe-catalog link
-- Apply after 001003. Schema: diet
BEGIN;
-- ---------------------------------------------------------------------------
-- Refresh tokens (persist sessions across API restarts)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS diet."RefreshTokens" (
"Token" varchar(128) PRIMARY KEY,
"UserId" uuid NOT NULL REFERENCES diet."AspNetUsers"("Id") ON DELETE CASCADE,
"ExpiresAtUtc" timestamptz NOT NULL,
"CreatedAtUtc" timestamptz NOT NULL DEFAULT now(),
"IsRevoked" boolean NOT NULL DEFAULT false
);
CREATE INDEX IF NOT EXISTS "IX_RefreshTokens_UserId" ON diet."RefreshTokens" ("UserId");
CREATE INDEX IF NOT EXISTS "IX_RefreshTokens_ExpiresAtUtc" ON diet."RefreshTokens" ("ExpiresAtUtc");
-- ---------------------------------------------------------------------------
-- Weekly meal plan (one recipe per meal slot per day)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS diet."MealPlanEntries" (
"Id" serial PRIMARY KEY,
"UserId" uuid NOT NULL REFERENCES diet."AspNetUsers"("Id") ON DELETE CASCADE,
"PlanDate" date NOT NULL,
"MealCategory" int NOT NULL CHECK ("MealCategory" BETWEEN 0 AND 4),
"RecipeId" int NOT NULL REFERENCES diet."Recipes"("Id") ON DELETE CASCADE,
"Portions" numeric(4,2) NOT NULL DEFAULT 1 CHECK ("Portions" > 0),
"CreatedAt" timestamptz NOT NULL DEFAULT now(),
"UpdatedAt" timestamptz NULL,
UNIQUE ("UserId", "PlanDate", "MealCategory")
);
CREATE INDEX IF NOT EXISTS "IX_MealPlanEntries_UserId_PlanDate"
ON diet."MealPlanEntries" ("UserId", "PlanDate");
-- ---------------------------------------------------------------------------
-- Favorites (quick logging)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS diet."UserFavoriteRecipes" (
"UserId" uuid NOT NULL REFERENCES diet."AspNetUsers"("Id") ON DELETE CASCADE,
"RecipeId" int NOT NULL REFERENCES diet."Recipes"("Id") ON DELETE CASCADE,
"CreatedAt" timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY ("UserId", "RecipeId")
);
CREATE TABLE IF NOT EXISTS diet."UserFavoriteCatalogItems" (
"UserId" uuid NOT NULL REFERENCES diet."AspNetUsers"("Id") ON DELETE CASCADE,
"CatalogItemId" int NOT NULL REFERENCES diet."IngredientNutritionCatalog"("Id") ON DELETE CASCADE,
"CreatedAt" timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY ("UserId", "CatalogItemId")
);
-- ---------------------------------------------------------------------------
-- Link recipe ingredients to nutrition catalog (auto macros)
-- ---------------------------------------------------------------------------
ALTER TABLE diet."Ingredients"
ADD COLUMN IF NOT EXISTS "CatalogItemId" int NULL
REFERENCES diet."IngredientNutritionCatalog"("Id") ON DELETE SET NULL;
COMMIT;