Add DELETE endpoint to visits API and update log page
This commit is contained in:
@@ -2,7 +2,7 @@ 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";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import type { Adventure } from "$lib/utils/types";
|
||||
|
||||
// Gets all the adventures that the user has visited
|
||||
@@ -37,3 +37,39 @@ export async function GET(event: RequestEvent): Promise<Response> {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
let res = await db
|
||||
.delete(userVisitedAdventures)
|
||||
.where(
|
||||
and(
|
||||
eq(userVisitedAdventures.userId, event.locals.user.id),
|
||||
eq(userVisitedAdventures.adventureID, Number(id))
|
||||
)
|
||||
)
|
||||
.execute();
|
||||
|
||||
console.log(res);
|
||||
console.log(id);
|
||||
console.log(event.locals.user.id);
|
||||
|
||||
return new Response(JSON.stringify({ id: id, res: res }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user