Remove planned trip

This commit is contained in:
Sean Morley
2024-05-13 23:03:28 +00:00
parent 01cd12d0e3
commit 3863d0b2ac
3 changed files with 141 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
import { db } from "$lib/db/db.server";
import { userPlannedTrips } from "$lib/db/schema";
import { error, type RequestEvent } from "@sveltejs/kit";
import { eq } from "drizzle-orm";
import { and, eq } from "drizzle-orm";
export async function POST(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
@@ -92,3 +92,38 @@ export async function GET(event: RequestEvent): Promise<Response> {
},
});
}
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",
},
});
}
const body = await event.request.json();
if (!body.id) {
return error(400, {
message: "No trip id provided",
});
}
let res = await db
.delete(userPlannedTrips)
.where(
and(
eq(userPlannedTrips.userId, event.locals.user.id),
eq(userPlannedTrips.id, body.id)
)
)
.execute();
return new Response(JSON.stringify({ message: "Trip deleted" }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}