Skip to content

Commit

Permalink
added PUT and DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
abermack committed Oct 25, 2024
1 parent 6820f17 commit 6ca4865
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 23 deletions.
65 changes: 51 additions & 14 deletions src/app/api/event/route.client.ts
Original file line number Diff line number Diff line change
@@ -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;
};

body: { event: Partial<Event> };
}) => {
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;
};
36 changes: 36 additions & 0 deletions src/app/api/event/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
21 changes: 12 additions & 9 deletions src/components/ExampleButton.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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}
Expand Down

0 comments on commit 6ca4865

Please sign in to comment.