Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Security trade-off: localStorage is readable by JavaScript on the same origin. A

`/cookbook` and `/recipes/new` require a validated session. On refresh, the router waits for session restoration before rendering protected content or redirecting to login. A temporary API/network error shows a retry action and preserves the token. Successful login or registration returns to the requested protected URL, including its query and hash. Explore and recipe details remain public. This is client-side routing; future protected API operations must enforce authentication and ownership on the server.

## Database

Se [databasemodellen](docs/database-model.md) for tabeller, ER-diagram, regler, migrasjon og tester. Modellen utvider autentiseringen og er klar for CRUD i neste steg.

## Nyttige kommandoer

| Kommando | Bruk |
Expand Down
105 changes: 105 additions & 0 deletions apps/api/migrations/0002_recipe_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
-- Existing placeholder recipes need a deliberate author/content backfill.
-- Stop before changing tables rather than inventing ownership or losing data.
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM "project2"."recipes") THEN
RAISE EXCEPTION 'Recipe migration requires an empty legacy recipes table. Back up existing rows and prepare an explicit author/content backfill before retrying.';
END IF;
END $$;
--> statement-breakpoint
CREATE TABLE "project2"."ratings" (
"user_id" uuid NOT NULL,
"recipe_id" uuid NOT NULL,
"score" integer NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "ratings_user_id_recipe_id_pk" PRIMARY KEY("user_id","recipe_id"),
CONSTRAINT "ratings_score_valid" CHECK ("project2"."ratings"."score" BETWEEN 1 AND 5)
);
--> statement-breakpoint
CREATE TABLE "project2"."recipe_ingredients" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"recipe_id" uuid NOT NULL,
"name" text NOT NULL,
"amount" numeric(12, 4) NOT NULL,
"unit" text NOT NULL,
"note" text,
"position" integer NOT NULL,
CONSTRAINT "recipe_ingredients_name_not_blank" CHECK (length(trim("project2"."recipe_ingredients"."name")) > 0),
CONSTRAINT "recipe_ingredients_amount_positive" CHECK ("project2"."recipe_ingredients"."amount" > 0 AND "project2"."recipe_ingredients"."amount" <> 'NaN'::numeric),
CONSTRAINT "recipe_ingredients_position_positive" CHECK ("project2"."recipe_ingredients"."position" > 0)
);
--> statement-breakpoint
CREATE TABLE "project2"."recipe_instructions" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"recipe_id" uuid NOT NULL,
"text" text NOT NULL,
"position" integer NOT NULL,
CONSTRAINT "recipe_instructions_text_not_blank" CHECK (length(trim("project2"."recipe_instructions"."text")) > 0),
CONSTRAINT "recipe_instructions_position_positive" CHECK ("project2"."recipe_instructions"."position" > 0)
);
--> statement-breakpoint
CREATE TABLE "project2"."recipe_tags" (
"recipe_id" uuid NOT NULL,
"tag" text NOT NULL,
CONSTRAINT "recipe_tags_recipe_id_tag_pk" PRIMARY KEY("recipe_id","tag"),
CONSTRAINT "recipe_tags_normalized" CHECK (length("project2"."recipe_tags"."tag") > 0 AND "project2"."recipe_tags"."tag" = lower(trim("project2"."recipe_tags"."tag")))
);
--> statement-breakpoint
CREATE TABLE "project2"."saved_recipes" (
"user_id" uuid NOT NULL,
"recipe_id" uuid NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "saved_recipes_user_id_recipe_id_pk" PRIMARY KEY("user_id","recipe_id")
);
--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "author_id" uuid NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "description" text NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "image_url" text;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "category" text NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "cuisine" text NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "difficulty" text NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "visibility" text DEFAULT 'private' NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "prep_minutes" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "cook_minutes" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "servings" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "created_at" timestamp with time zone DEFAULT now() NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD COLUMN "updated_at" timestamp with time zone DEFAULT now() NOT NULL;--> statement-breakpoint
ALTER TABLE "project2"."ratings" ADD CONSTRAINT "ratings_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "project2"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "project2"."ratings" ADD CONSTRAINT "ratings_recipe_id_recipes_id_fk" FOREIGN KEY ("recipe_id") REFERENCES "project2"."recipes"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "project2"."recipe_ingredients" ADD CONSTRAINT "recipe_ingredients_recipe_id_recipes_id_fk" FOREIGN KEY ("recipe_id") REFERENCES "project2"."recipes"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "project2"."recipe_instructions" ADD CONSTRAINT "recipe_instructions_recipe_id_recipes_id_fk" FOREIGN KEY ("recipe_id") REFERENCES "project2"."recipes"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "project2"."recipe_tags" ADD CONSTRAINT "recipe_tags_recipe_id_recipes_id_fk" FOREIGN KEY ("recipe_id") REFERENCES "project2"."recipes"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "project2"."saved_recipes" ADD CONSTRAINT "saved_recipes_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "project2"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "project2"."saved_recipes" ADD CONSTRAINT "saved_recipes_recipe_id_recipes_id_fk" FOREIGN KEY ("recipe_id") REFERENCES "project2"."recipes"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "ratings_recipe_idx" ON "project2"."ratings" USING btree ("recipe_id");--> statement-breakpoint
CREATE UNIQUE INDEX "recipe_ingredients_recipe_position_unique" ON "project2"."recipe_ingredients" USING btree ("recipe_id","position");--> statement-breakpoint
CREATE UNIQUE INDEX "recipe_instructions_recipe_position_unique" ON "project2"."recipe_instructions" USING btree ("recipe_id","position");--> statement-breakpoint
CREATE INDEX "saved_recipes_recipe_idx" ON "project2"."saved_recipes" USING btree ("recipe_id");--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD CONSTRAINT "recipes_author_id_users_id_fk" FOREIGN KEY ("author_id") REFERENCES "project2"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "recipes_author_idx" ON "project2"."recipes" USING btree ("author_id");--> statement-breakpoint
CREATE INDEX "recipes_public_created_idx" ON "project2"."recipes" USING btree ("created_at","id") WHERE "project2"."recipes"."visibility" = 'public';--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD CONSTRAINT "recipes_title_not_blank" CHECK (length(trim("project2"."recipes"."title")) > 0);--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD CONSTRAINT "recipes_category_not_blank" CHECK (length(trim("project2"."recipes"."category")) > 0);--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD CONSTRAINT "recipes_cuisine_not_blank" CHECK (length(trim("project2"."recipes"."cuisine")) > 0);--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD CONSTRAINT "recipes_difficulty_valid" CHECK ("project2"."recipes"."difficulty" IN ('easy', 'medium', 'hard'));--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD CONSTRAINT "recipes_visibility_valid" CHECK ("project2"."recipes"."visibility" IN ('public', 'private'));--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD CONSTRAINT "recipes_prep_minutes_nonnegative" CHECK ("project2"."recipes"."prep_minutes" >= 0);--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD CONSTRAINT "recipes_cook_minutes_nonnegative" CHECK ("project2"."recipes"."cook_minutes" >= 0);--> statement-breakpoint
ALTER TABLE "project2"."recipes" ADD CONSTRAINT "recipes_servings_positive" CHECK ("project2"."recipes"."servings" > 0);
--> statement-breakpoint
-- Database triggers also cover writes made through raw SQL (as in auth).
CREATE FUNCTION "project2"."touch_updated_at"() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
NEW.updated_at = clock_timestamp();
RETURN NEW;
END $$;
--> statement-breakpoint
CREATE TRIGGER recipes_touch_updated_at
BEFORE UPDATE ON "project2"."recipes"
FOR EACH ROW EXECUTE FUNCTION "project2"."touch_updated_at"();
--> statement-breakpoint
CREATE TRIGGER ratings_touch_updated_at
BEFORE UPDATE ON "project2"."ratings"
FOR EACH ROW EXECUTE FUNCTION "project2"."touch_updated_at"();
Loading