Started Migration

This commit is contained in:
Sean Morley
2024-04-26 01:14:13 +00:00
parent e197de9d39
commit 37f050d254
4 changed files with 54 additions and 81 deletions

View File

@@ -1,6 +1,6 @@
import { lucia } from "$lib/server/auth";
import type { RequestEvent } from "@sveltejs/kit";
import { userVisitedAdventures } from "$lib/db/schema";
import { adventureTable, userVisitedAdventures } from "$lib/db/schema";
import { db } from "$lib/db/db.server";
import { and, eq } from "drizzle-orm";
import type { Adventure } from "$lib/utils/types";
@@ -17,18 +17,12 @@ export async function GET(event: RequestEvent): Promise<Response> {
}
let result = await db
.select()
.from(userVisitedAdventures)
.where(eq(userVisitedAdventures.userId, event.locals.user.id))
.from(adventureTable)
.where(eq(adventureTable.userId, event.locals.user.id))
.execute();
return new Response(
JSON.stringify({
adventures: result.map((item) => ({
id: item.adventureID,
name: item.adventureName,
location: item.location,
date: item.visitedDate,
})),
}),
// turn the result into an Adventure object array
JSON.stringify(result.map((r) => r as Adventure)),
{
status: 200,
headers: {
@@ -62,11 +56,11 @@ export async function DELETE(event: RequestEvent): Promise<Response> {
}
let res = await db
.delete(userVisitedAdventures)
.delete(adventureTable)
.where(
and(
eq(userVisitedAdventures.userId, event.locals.user.id),
eq(userVisitedAdventures.adventureID, Number(id))
eq(adventureTable.userId, event.locals.user.id),
eq(adventureTable.id, Number(id))
)
)
.execute();
@@ -90,28 +84,30 @@ export async function POST(event: RequestEvent): Promise<Response> {
});
}
// get properties from the body
const { name, location, date } = await event.request.json();
const { newAdventure } = await event.request.json();
console.log(newAdventure);
const { name, location, date } = newAdventure;
// insert the adventure to the user's visited list
await db
.insert(userVisitedAdventures)
.insert(adventureTable)
.values({
userId: event.locals.user.id,
adventureName: name,
type: "mylog",
name: name,
location: location,
visitedDate: date,
date: date,
})
.execute();
let res = await db
.select()
.from(userVisitedAdventures)
.from(adventureTable)
.where(
and(
eq(userVisitedAdventures.userId, event.locals.user.id),
eq(userVisitedAdventures.adventureName, name),
eq(userVisitedAdventures.location, location),
eq(userVisitedAdventures.visitedDate, date)
eq(adventureTable.userId, event.locals.user.id),
eq(adventureTable.name, name),
eq(adventureTable.location, location),
eq(adventureTable.date, date)
)
)
.execute();
@@ -121,7 +117,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
JSON.stringify({
adventure: { name, location, date },
message: { message: "Adventure added" },
id: res[0].adventureID,
id: res[0].id,
}),
{
status: 200,
@@ -144,20 +140,22 @@ export async function PUT(event: RequestEvent): Promise<Response> {
}
// get properties from the body
const { id, name, location, date } = await event.request.json();
const { newAdventure } = await event.request.json();
console.log(newAdventure);
const { name, location, date, id } = newAdventure;
// update the adventure in the user's visited list
await db
.update(userVisitedAdventures)
.update(adventureTable)
.set({
adventureName: name,
name: name,
location: location,
visitedDate: date,
date: date,
})
.where(
and(
eq(userVisitedAdventures.userId, event.locals.user.id),
eq(userVisitedAdventures.adventureID, Number(id))
eq(adventureTable.userId, event.locals.user.id),
eq(adventureTable.id, Number(id))
)
)
.execute();