2024-04-03 23:55:00 +00:00
|
|
|
import { lucia } from "$lib/server/auth";
|
|
|
|
import type { RequestEvent } from "@sveltejs/kit";
|
|
|
|
import { userVisitedAdventures } from "$lib/db/schema";
|
|
|
|
import { db } from "$lib/db/db.server";
|
2024-04-10 14:33:41 +00:00
|
|
|
import { and, eq } from "drizzle-orm";
|
2024-04-06 12:54:17 +00:00
|
|
|
import type { Adventure } from "$lib/utils/types";
|
2024-04-03 23:55:00 +00:00
|
|
|
|
|
|
|
// Gets all the adventures that the user has visited
|
|
|
|
export async function GET(event: RequestEvent): Promise<Response> {
|
|
|
|
if (!event.locals.user) {
|
|
|
|
return new Response(JSON.stringify({ error: "No user found" }), {
|
|
|
|
status: 401,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let result = await db
|
|
|
|
.select()
|
|
|
|
.from(userVisitedAdventures)
|
|
|
|
.where(eq(userVisitedAdventures.userId, event.locals.user.id))
|
|
|
|
.execute();
|
|
|
|
return new Response(
|
|
|
|
JSON.stringify({
|
2024-04-06 12:54:17 +00:00
|
|
|
adventures: result.map((item) => ({
|
|
|
|
id: item.adventureID,
|
|
|
|
name: item.adventureName,
|
|
|
|
location: item.location,
|
|
|
|
created: item.visitedDate,
|
|
|
|
})),
|
2024-04-03 23:55:00 +00:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2024-04-10 14:33:41 +00:00
|
|
|
|
|
|
|
// deletes the adventure given the adventure id and the user object
|
|
|
|
export async function DELETE(event: RequestEvent): Promise<Response> {
|
|
|
|
if (!event.locals.user) {
|
|
|
|
return new Response(JSON.stringify({ error: "No user found" }), {
|
|
|
|
status: 401,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// get id from the body
|
|
|
|
const { id } = await event.request.json();
|
|
|
|
|
2024-04-10 17:54:19 +00:00
|
|
|
if (!id) {
|
|
|
|
return new Response(JSON.stringify({ error: "No id found" }), {
|
|
|
|
status: 400,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-10 14:33:41 +00:00
|
|
|
let res = await db
|
|
|
|
.delete(userVisitedAdventures)
|
|
|
|
.where(
|
|
|
|
and(
|
|
|
|
eq(userVisitedAdventures.userId, event.locals.user.id),
|
|
|
|
eq(userVisitedAdventures.adventureID, Number(id))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
return new Response(JSON.stringify({ id: id, res: res }), {
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
2024-04-10 14:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add the adventure to the user's visited list
|
|
|
|
export async function POST(event: RequestEvent): Promise<Response> {
|
|
|
|
if (!event.locals.user) {
|
|
|
|
return new Response(JSON.stringify({ error: "No user found" }), {
|
|
|
|
status: 401,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// get properties from the body
|
|
|
|
const { name, location, created } = await event.request.json();
|
|
|
|
|
|
|
|
// insert the adventure to the user's visited list
|
|
|
|
await db
|
|
|
|
.insert(userVisitedAdventures)
|
|
|
|
.values({
|
|
|
|
userId: event.locals.user.id,
|
|
|
|
adventureName: name,
|
|
|
|
location: location,
|
|
|
|
visitedDate: created,
|
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
let res = await db
|
|
|
|
.select()
|
|
|
|
.from(userVisitedAdventures)
|
|
|
|
.where(
|
|
|
|
and(
|
|
|
|
eq(userVisitedAdventures.userId, event.locals.user.id),
|
|
|
|
eq(userVisitedAdventures.adventureName, name),
|
|
|
|
eq(userVisitedAdventures.location, location),
|
|
|
|
eq(userVisitedAdventures.visitedDate, created)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
// return a response with the adventure object values
|
|
|
|
return new Response(
|
|
|
|
JSON.stringify({
|
|
|
|
adventure: { name, location, created },
|
|
|
|
message: { message: "Adventure added" },
|
|
|
|
id: res[0].adventureID
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2024-04-10 14:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// put route to update existing adventure
|
|
|
|
export async function PUT(event: RequestEvent): Promise<Response> {
|
|
|
|
if (!event.locals.user) {
|
|
|
|
return new Response(JSON.stringify({ error: "No user found" }), {
|
|
|
|
status: 401,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// get properties from the body
|
|
|
|
const { id, name, location, created } = await event.request.json();
|
|
|
|
|
|
|
|
// update the adventure in the user's visited list
|
|
|
|
await db
|
|
|
|
.update(userVisitedAdventures)
|
|
|
|
.set({
|
|
|
|
adventureName: name,
|
|
|
|
location: location,
|
|
|
|
visitedDate: created,
|
|
|
|
})
|
|
|
|
.where(
|
|
|
|
and(
|
|
|
|
eq(userVisitedAdventures.userId, event.locals.user.id),
|
|
|
|
eq(userVisitedAdventures.adventureID, Number(id))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
return new Response(
|
|
|
|
JSON.stringify({
|
|
|
|
adventure: { id, name, location, created },
|
|
|
|
message: { message: "Adventure updated" },
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2024-04-10 14:33:41 +00:00
|
|
|
}
|