mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-04 04:35:19 +02:00
Add clearvisits API endpoint and implement DELETE method***
***Handle error when no id is found in DELETE method*** ***Update deleteData function in log page to use clearvisits API endpoint
This commit is contained in:
parent
bc0e1b4db2
commit
3dcad53004
3 changed files with 53 additions and 3 deletions
27
src/routes/api/clearvisits/+server.ts
Normal file
27
src/routes/api/clearvisits/+server.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import type { RequestEvent } from "@sveltejs/kit";
|
||||
import { db } from "$lib/db/db.server";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { userVisitedAdventures } from "$lib/db/schema";
|
||||
|
||||
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",
|
||||
},
|
||||
});
|
||||
}
|
||||
let res = await db
|
||||
.delete(userVisitedAdventures)
|
||||
.where(
|
||||
eq(userVisitedAdventures.userId, event.locals.user.id),
|
||||
)
|
||||
.execute();
|
||||
return new Response(JSON.stringify({ res: res }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
|
@ -52,6 +52,15 @@ export async function DELETE(event: RequestEvent): Promise<Response> {
|
|||
// 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
|
||||
.delete(userVisitedAdventures)
|
||||
.where(
|
||||
|
|
|
@ -167,9 +167,23 @@
|
|||
}
|
||||
|
||||
function deleteData() {
|
||||
clearAdventures();
|
||||
adventures = getAdventures();
|
||||
showToast("deleted");
|
||||
fetch("/api/clearvisits", {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
console.log("Success:", data);
|
||||
// remove adventure from array where id matches
|
||||
adventures = [];
|
||||
showToast("removed");
|
||||
visitCount.set(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error:", error);
|
||||
});
|
||||
}
|
||||
|
||||
function removeAdventure(event: { detail: number }) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue