1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-04 20:55:19 +02:00
AdventureLog/src/routes/api/visits/+server.ts

176 lines
4.2 KiB
TypeScript
Raw Normal View History

import { lucia } from "$lib/server/auth";
import type { RequestEvent } from "@sveltejs/kit";
2024-04-26 01:14:13 +00:00
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";
// 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()
2024-04-26 01:14:13 +00:00
.from(adventureTable)
.where(eq(adventureTable.userId, event.locals.user.id))
.execute();
return new Response(
2024-04-26 01:14:13 +00:00
// turn the result into an Adventure object array
JSON.stringify(result.map((r) => r as Adventure)),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}
// 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();
if (!id) {
return new Response(JSON.stringify({ error: "No id found" }), {
status: 400,
headers: {
"Content-Type": "application/json",
},
});
}
let res = await db
2024-04-26 01:14:13 +00:00
.delete(adventureTable)
.where(
and(
2024-04-26 01:14:13 +00:00
eq(adventureTable.userId, event.locals.user.id),
eq(adventureTable.id, Number(id))
)
)
.execute();
return new Response(JSON.stringify({ id: id, res: res }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}
// 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",
},
});
}
2024-04-26 01:14:13 +00:00
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
2024-04-26 01:14:13 +00:00
.insert(adventureTable)
.values({
userId: event.locals.user.id,
2024-04-26 01:14:13 +00:00
type: "mylog",
name: name,
location: location,
2024-04-26 01:14:13 +00:00
date: date,
})
.execute();
let res = await db
.select()
2024-04-26 01:14:13 +00:00
.from(adventureTable)
.where(
and(
2024-04-26 01:14:13 +00:00
eq(adventureTable.userId, event.locals.user.id),
eq(adventureTable.name, name),
eq(adventureTable.location, location),
eq(adventureTable.date, date)
)
)
.execute();
// return a response with the adventure object values
return new Response(
JSON.stringify({
adventure: { name, location, date },
message: { message: "Adventure added" },
2024-04-26 01:14:13 +00:00
id: res[0].id,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}
// 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
2024-04-26 01:14:13 +00:00
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
2024-04-26 01:14:13 +00:00
.update(adventureTable)
.set({
2024-04-26 01:14:13 +00:00
name: name,
location: location,
2024-04-26 01:14:13 +00:00
date: date,
})
.where(
and(
2024-04-26 01:14:13 +00:00
eq(adventureTable.userId, event.locals.user.id),
eq(adventureTable.id, Number(id))
)
)
.execute();
return new Response(
JSON.stringify({
adventure: { id, name, location, date },
message: { message: "Adventure updated" },
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}