1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-23 23:24:23 +00:00
festa/pages/api/events/[slug].ts

38 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-05-31 14:46:06 +00:00
import { database } from "../../../utils/prismaClient";
2022-05-30 03:19:49 +00:00
import { NextApiRequest, NextApiResponse } from "next";
import { ApiResult } from "../../../types/api";
2022-05-31 10:00:55 +00:00
import { Model, restInPeace } from "../../../utils/restInPeace";
import { handleInterrupts, Interrupt } from "../../../utils/interrupt";
2022-05-31 14:39:56 +00:00
import { authorizeUser } from "../../../utils/authorizeUser";
2022-05-31 10:00:55 +00:00
import { Event } from "@prisma/client";
2022-05-31 03:03:48 +00:00
2022-05-30 03:19:49 +00:00
export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResult<Event | Event[]>>) {
2022-05-31 03:03:48 +00:00
handleInterrupts(res, async () => {
const user = await authorizeUser(req)
2022-05-31 03:03:48 +00:00
2022-05-31 14:01:34 +00:00
const canEdit = async (_model: Model, obj?: Event) => {
2022-05-31 10:00:55 +00:00
if(obj && obj.creatorId !== user.id) {
throw new Interrupt(403, {error: "Only the creator can edit an event"})
}
}
2022-05-31 03:03:48 +00:00
const which = {
2022-06-03 02:07:12 +00:00
slug: req.query.slug,
2022-05-31 03:03:48 +00:00
}
const update = {
2022-06-03 02:07:12 +00:00
name: req.body.name,
postcard: req.body.postcard ?? null,
2022-06-06 01:15:44 +00:00
description: req.body.description,
startingAt: req.body.startingAt,
endingAt: req.body.endingAt,
2022-05-31 03:03:48 +00:00
}
2022-05-31 10:06:37 +00:00
await restInPeace(req, res, {
2022-05-31 14:46:06 +00:00
model: database.event,
2022-05-31 03:03:48 +00:00
retrieve: {which},
update: {which, update, before: canEdit},
destroy: {which, before: canEdit},
2022-05-31 03:03:48 +00:00
})
2022-05-30 03:19:49 +00:00
})
}