From 6ca4865e1dd80eb7ae46cb5893bda021998e9254 Mon Sep 17 00:00:00 2001 From: abermack Date: Fri, 25 Oct 2024 13:21:02 -0400 Subject: [PATCH] added PUT and DELETE --- src/app/api/event/route.client.ts | 65 ++++++++++++++++++++++++------- src/app/api/event/route.ts | 36 +++++++++++++++++ src/components/ExampleButton.tsx | 21 +++++----- 3 files changed, 99 insertions(+), 23 deletions(-) diff --git a/src/app/api/event/route.client.ts b/src/app/api/event/route.client.ts index 556732a..c137cce 100644 --- a/src/app/api/event/route.client.ts +++ b/src/app/api/event/route.client.ts @@ -1,15 +1,52 @@ +import { Event } from "@prisma/client"; + +// interface addEventRequest { + +// } + +// { +// EventName: string; +// Description: string; +// MaxPeople: number; +// DateTime: Date; +// }; export const addEvent = async (request: { - body: { event: { EventName: string, Description: string, MaxPeople: number, DateTime: Date} }; - }) => { - const { body, ...options } = request; - const response = await fetch("/api/event", { - method: "POST", - body: JSON.stringify(body), - ...options, - }); - - const json = await response.json(); - - return json; - }; - \ No newline at end of file + body: { event: Partial }; +}) => { + const { body, ...options } = request; + const response = await fetch("/api/event", { + method: "POST", + body: JSON.stringify(body), + ...options, + }); + + const json = await response.json(); + + return json; +}; + +export const updateEvent = async (request: { body: { event: Event } }) => { + const { body, ...options } = request; + const response = await fetch("/api/event", { + method: "PUT", + body: JSON.stringify(body), + ...options, + }); + + const json = await response.json(); + + return json; +}; + +export const deleteEvent = async (request: { body: { id: string } }) => { + const { body, ...options } = request; + const response = await fetch("/api/event", { + method: "DELETE", + body: JSON.stringify(body), + ...options, + }); + + const json = await response.json(); + + return json; +}; diff --git a/src/app/api/event/route.ts b/src/app/api/event/route.ts index c77316a..3ff241f 100644 --- a/src/app/api/event/route.ts +++ b/src/app/api/event/route.ts @@ -28,6 +28,42 @@ export const POST = async (request: NextRequest) => { export const PUT = async (request: NextRequest) => { try { + const { event } = await request.json(); + const { id, ...other } = event; + const updateEvent = await prisma.event.update({ + where: { + id: event.id, + }, + data: other, + }); + + return NextResponse.json({ + code: "SUCCESS", + message: updateEvent.EventName, + }); + } catch (error) { + console.error("Error:", error); + return NextResponse.json({ + code: "ERROR", + message: error, + }); + } +}; + +export const DELETE = async (request: NextRequest) => { + try { + const { id } = await request.json(); + + const deleteEvent = await prisma.event.delete({ + where: { + id: id, + }, + }); + + return NextResponse.json({ + code: "SUCCESS", + message: deleteEvent.EventName, + }); } catch (error) { console.error("Error:", error); return NextResponse.json({ diff --git a/src/components/ExampleButton.tsx b/src/components/ExampleButton.tsx index eb09470..88e549b 100644 --- a/src/components/ExampleButton.tsx +++ b/src/components/ExampleButton.tsx @@ -1,6 +1,6 @@ import { addUser } from "@api/user/route.client"; import { Icon } from "@iconify/react/dist/iconify.js"; -import {addEvent} from "@api/event/route.client"; +import { addEvent, deleteEvent, updateEvent } from "@api/event/route.client"; interface ExampleButtonProps { buttonText: string; @@ -19,20 +19,23 @@ const ExampleButton = ({ buttonText }: ExampleButtonProps) => { }, }, }); - - const response2 = await addEvent({ + await addEvent({ body: { event: { - EventName: "bread & roses meeting :)", - DateTime: new Date("2024-10-18"), - Description: "friday meeting yippee", - MaxPeople: 10000, + EventName: "eventname", + Description: "desc", + MaxPeople: 10, + DateTime: new Date(), }, }, }); - + // const response2 = await deleteEvent({ + // body: { + // id: "6712a960e56a47a7792d837c", + // }, + // }); console.log(response); - console.log(response2); + // console.log(response2); }} > {buttonText}