Update AdventureCard component to include regionId and visited properties

This commit is contained in:
Sean Morley
2024-04-13 23:09:52 +00:00
parent b2210dc7c9
commit 00d9270546
4 changed files with 161 additions and 5 deletions

View File

@@ -0,0 +1,77 @@
import type { RequestEvent } from "@sveltejs/kit";
import { db } from "$lib/db/db.server";
import { userVisitedAdventures, userVisitedWorldTravel } from "$lib/db/schema";
import { and, eq } from "drizzle-orm";
export async function POST(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let body = await event.request.json();
let res = await db
.insert(userVisitedWorldTravel)
.values({
userId: event.locals.user.id,
region_id: body.region_id,
})
.execute();
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}
export async function GET(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let res = await db
.select()
.from(userVisitedWorldTravel)
.where(eq(userVisitedWorldTravel.userId,event.locals.user.id));
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}
export async function DELETE(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let body = await event.request.json();
let res = await db
.delete(userVisitedWorldTravel)
.where(
and(
eq(userVisitedWorldTravel.userId, event.locals.user.id),
eq(userVisitedWorldTravel.region_id, body.region_id),
)
)
.execute();
return new Response(JSON.stringify({ res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}