2024-04-03 23:55:00 +00:00
|
|
|
import { lucia } from "$lib/server/auth";
|
2024-04-28 21:54:43 +00:00
|
|
|
import { error, type RequestEvent } from "@sveltejs/kit";
|
2024-04-26 23:04:41 +00:00
|
|
|
import { adventureTable } from "$lib/db/schema";
|
2024-04-03 23:55:00 +00:00
|
|
|
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()
|
2024-04-26 01:14:13 +00:00
|
|
|
.from(adventureTable)
|
2024-05-04 15:00:02 +00:00
|
|
|
.where(
|
|
|
|
and(
|
|
|
|
eq(adventureTable.userId, event.locals.user.id),
|
|
|
|
eq(adventureTable.type, "mylog")
|
|
|
|
)
|
|
|
|
)
|
2024-04-03 23:55:00 +00:00
|
|
|
.execute();
|
|
|
|
return new Response(
|
2024-04-26 01:14:13 +00:00
|
|
|
// turn the result into an Adventure object array
|
2024-04-30 22:39:09 +00:00
|
|
|
JSON.stringify(
|
|
|
|
result.map((r) => {
|
|
|
|
const adventure: Adventure = r as Adventure;
|
|
|
|
if (typeof adventure.activityTypes === "string") {
|
|
|
|
try {
|
|
|
|
adventure.activityTypes = JSON.parse(adventure.activityTypes);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error parsing activityTypes:", error);
|
|
|
|
adventure.activityTypes = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return adventure;
|
|
|
|
})
|
|
|
|
),
|
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
|
2024-04-26 01:14:13 +00:00
|
|
|
.delete(adventureTable)
|
2024-04-10 14:33:41 +00:00
|
|
|
.where(
|
|
|
|
and(
|
2024-04-26 01:14:13 +00:00
|
|
|
eq(adventureTable.userId, event.locals.user.id),
|
|
|
|
eq(adventureTable.id, Number(id))
|
2024-04-10 14:33:41 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
|
|
|
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-05-03 21:29:35 +00:00
|
|
|
const body = await event.request.json();
|
|
|
|
if (!body.detailAdventure) {
|
|
|
|
return error(400, {
|
|
|
|
message: "No adventure data provided",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:43:18 +00:00
|
|
|
const { name, location, date, description, activityTypes, rating } =
|
2024-05-03 21:29:35 +00:00
|
|
|
body.detailAdventure;
|
2024-04-10 14:51:51 +00:00
|
|
|
|
2024-04-28 21:54:43 +00:00
|
|
|
if (!name) {
|
|
|
|
return error(400, {
|
|
|
|
message: "Name field is required!",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:43:18 +00:00
|
|
|
if (rating && (rating < 1 || rating > 5)) {
|
|
|
|
return error(400, {
|
|
|
|
message: "Rating must be between 1 and 5",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-30 22:39:09 +00:00
|
|
|
console.log(activityTypes);
|
|
|
|
|
2024-04-10 14:51:51 +00:00
|
|
|
// insert the adventure to the user's visited list
|
2024-04-28 21:54:43 +00:00
|
|
|
let res = await db
|
2024-04-26 01:14:13 +00:00
|
|
|
.insert(adventureTable)
|
2024-04-10 14:51:51 +00:00
|
|
|
.values({
|
|
|
|
userId: event.locals.user.id,
|
2024-04-26 01:14:13 +00:00
|
|
|
type: "mylog",
|
|
|
|
name: name,
|
2024-04-28 21:54:43 +00:00
|
|
|
location: location || null,
|
|
|
|
date: date || null,
|
|
|
|
description: description || null,
|
2024-04-30 22:39:09 +00:00
|
|
|
activityTypes: JSON.stringify(activityTypes) || null,
|
2024-05-04 00:43:18 +00:00
|
|
|
rating: rating || null,
|
2024-04-10 14:51:51 +00:00
|
|
|
})
|
2024-04-28 21:54:43 +00:00
|
|
|
.returning({ insertedId: adventureTable.id })
|
2024-04-10 14:51:51 +00:00
|
|
|
.execute();
|
2024-04-28 21:54:43 +00:00
|
|
|
|
|
|
|
let insertedId = res[0].insertedId;
|
|
|
|
console.log(insertedId);
|
2024-04-10 14:51:51 +00:00
|
|
|
|
2024-05-03 21:29:35 +00:00
|
|
|
body.detailAdventure.id = insertedId;
|
|
|
|
|
2024-04-21 22:56:27 +00:00
|
|
|
// return a response with the adventure object values
|
2024-04-10 14:51:51 +00:00
|
|
|
return new Response(
|
|
|
|
JSON.stringify({
|
2024-05-03 21:29:35 +00:00
|
|
|
adventure: body.detailAdventure,
|
2024-04-10 14:51:51 +00:00
|
|
|
message: { message: "Adventure added" },
|
2024-04-28 21:54:43 +00:00
|
|
|
id: insertedId,
|
2024-04-10 14:51:51 +00:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-03 21:29:35 +00:00
|
|
|
const body = await event.request.json();
|
|
|
|
if (!body.detailAdventure) {
|
|
|
|
return error(400, {
|
|
|
|
message: "No adventure data provided",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:43:18 +00:00
|
|
|
const { name, location, date, description, activityTypes, id, rating } =
|
2024-05-03 21:29:35 +00:00
|
|
|
body.detailAdventure;
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
return error(400, {
|
|
|
|
message: "Name field is required!",
|
|
|
|
});
|
|
|
|
}
|
2024-04-10 14:58:42 +00:00
|
|
|
|
|
|
|
// update the adventure in the user's visited list
|
|
|
|
await db
|
2024-04-26 01:14:13 +00:00
|
|
|
.update(adventureTable)
|
2024-04-10 14:58:42 +00:00
|
|
|
.set({
|
2024-04-26 01:14:13 +00:00
|
|
|
name: name,
|
2024-04-10 14:58:42 +00:00
|
|
|
location: location,
|
2024-04-26 01:14:13 +00:00
|
|
|
date: date,
|
2024-04-28 16:03:38 +00:00
|
|
|
description: description,
|
2024-05-04 00:43:18 +00:00
|
|
|
rating: rating,
|
2024-05-01 00:14:31 +00:00
|
|
|
activityTypes: JSON.stringify(activityTypes),
|
2024-04-10 14:58:42 +00:00
|
|
|
})
|
|
|
|
.where(
|
|
|
|
and(
|
2024-04-26 01:14:13 +00:00
|
|
|
eq(adventureTable.userId, event.locals.user.id),
|
|
|
|
eq(adventureTable.id, Number(id))
|
2024-04-10 14:58:42 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
return new Response(
|
|
|
|
JSON.stringify({
|
2024-05-03 21:29:35 +00:00
|
|
|
adventure: body.detailAdventure,
|
2024-04-10 14:58:42 +00:00
|
|
|
message: { message: "Adventure updated" },
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2024-04-21 22:56:27 +00:00
|
|
|
}
|