From a6c3738c4854d9324b2b0f278bb9dafc81bc7b2c Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Thu, 11 Apr 2024 22:21:09 +0000 Subject: [PATCH] Add name and date columns to sharedAdventures table and display them in shared adventure page --- migrations/0011_heavy_ben_urich.sql | 2 + migrations/meta/0011_snapshot.json | 219 ++++++++++++++++++++++++ migrations/meta/_journal.json | 7 + src/lib/db/schema.ts | 2 + src/routes/api/share/+server.ts | 8 +- src/routes/shared/[key]/+page.server.ts | 5 + src/routes/shared/[key]/+page.svelte | 1 + 7 files changed, 241 insertions(+), 3 deletions(-) create mode 100644 migrations/0011_heavy_ben_urich.sql create mode 100644 migrations/meta/0011_snapshot.json diff --git a/migrations/0011_heavy_ben_urich.sql b/migrations/0011_heavy_ben_urich.sql new file mode 100644 index 0000000..6f47f52 --- /dev/null +++ b/migrations/0011_heavy_ben_urich.sql @@ -0,0 +1,2 @@ +ALTER TABLE "sharedAdventures" ADD COLUMN "name" text NOT NULL;--> statement-breakpoint +ALTER TABLE "sharedAdventures" ADD COLUMN "date" text NOT NULL; \ No newline at end of file diff --git a/migrations/meta/0011_snapshot.json b/migrations/meta/0011_snapshot.json new file mode 100644 index 0000000..5b38d4a --- /dev/null +++ b/migrations/meta/0011_snapshot.json @@ -0,0 +1,219 @@ +{ + "id": "b318f7a7-c4e1-49f3-9bfc-7d68f118bf7d", + "prevId": "d6cd08c8-9dc8-42df-aab9-0f5800602864", + "version": "5", + "dialect": "pg", + "tables": { + "featuredAdventures": { + "name": "featuredAdventures", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "location": { + "name": "location", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "session": { + "name": "session", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "session_user_id_user_id_fk": { + "name": "session_user_id_user_id_fk", + "tableFrom": "session", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "sharedAdventures": { + "name": "sharedAdventures", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "data": { + "name": "data", + "type": "json", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "date": { + "name": "date", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "first_name": { + "name": "first_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "last_name": { + "name": "last_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "icon": { + "name": "icon", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "hashed_password": { + "name": "hashed_password", + "type": "varchar", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "userVisitedAdventures": { + "name": "userVisitedAdventures", + "schema": "", + "columns": { + "adventure_id": { + "name": "adventure_id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "adventure_name": { + "name": "adventure_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "location": { + "name": "location", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "visited_date": { + "name": "visited_date", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "userVisitedAdventures_user_id_user_id_fk": { + "name": "userVisitedAdventures_user_id_user_id_fk", + "tableFrom": "userVisitedAdventures", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": {}, + "schemas": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/migrations/meta/_journal.json b/migrations/meta/_journal.json index 3f770a8..ef98192 100644 --- a/migrations/meta/_journal.json +++ b/migrations/meta/_journal.json @@ -78,6 +78,13 @@ "when": 1712842196443, "tag": "0010_lean_gamma_corps", "breakpoints": true + }, + { + "idx": 11, + "version": "5", + "when": 1712873663208, + "tag": "0011_heavy_ben_urich", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/lib/db/schema.ts b/src/lib/db/schema.ts index 9ed9b15..5df214e 100644 --- a/src/lib/db/schema.ts +++ b/src/lib/db/schema.ts @@ -16,6 +16,8 @@ export const featuredAdventures = pgTable("featuredAdventures", { export const sharedAdventures = pgTable("sharedAdventures", { id: text("id").primaryKey(), data: json("data").notNull(), + name: text("name").notNull(), + date: text("date").notNull(), }); export const userTable = pgTable("user", { diff --git a/src/routes/api/share/+server.ts b/src/routes/api/share/+server.ts index 5fdf9a3..86f3dc7 100644 --- a/src/routes/api/share/+server.ts +++ b/src/routes/api/share/+server.ts @@ -2,13 +2,15 @@ import { db } from "$lib/db/db.server"; import { sharedAdventures } from "$lib/db/schema"; import type { Adventure } from "$lib/utils/types"; -export async function POST({ request }: { request: Request }) { +export async function POST({ request, locals }) { const { key, data } = await request.json(); let adventure = data as Adventure; console.log(adventure); + let date = new Date().toISOString().split("T")[0]; + let name = locals.user ? locals.user.username : "Anonymous"; await db .insert(sharedAdventures) - .values({ id: key, data: adventure }) + .values({ id: key, data: adventure, name:name, date:date }) .execute(); return new Response(JSON.stringify({ key: key })); -} +} \ No newline at end of file diff --git a/src/routes/shared/[key]/+page.server.ts b/src/routes/shared/[key]/+page.server.ts index 6dbe5c7..92f7d08 100644 --- a/src/routes/shared/[key]/+page.server.ts +++ b/src/routes/shared/[key]/+page.server.ts @@ -29,9 +29,14 @@ export async function load({ params }) { } as Adventure; }); + let name = rawData.name; + let date = rawData.date; + // Return the array of Adventure objects return { adventureArray, + name, + date, }; } diff --git a/src/routes/shared/[key]/+page.svelte b/src/routes/shared/[key]/+page.svelte index bb9f20d..9c2e69f 100644 --- a/src/routes/shared/[key]/+page.svelte +++ b/src/routes/shared/[key]/+page.svelte @@ -15,6 +15,7 @@ {/each} -->

Shared Adventure List

+

By {data.name} on {data.date}