1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-23 15:14:23 +00:00
festa/pages/api/events/index.ts

34 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-06-02 02:26:52 +00:00
import { database } from "../../../utils/prismaClient";
import { NextApiRequest, NextApiResponse } from "next";
import { ApiResult } from "../../../types/api";
2022-06-02 02:31:09 +00:00
import { restInPeace } from "../../../utils/restInPeace";
2022-06-02 02:26:52 +00:00
import { default as cryptoRandomString } from "crypto-random-string";
import { handleInterrupts, Interrupt } from "../../../utils/interrupt";
import { authorizeUser } from "../../../utils/authorizeUser";
import { Event } from "@prisma/client";
export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResult<Event | Event[]>>) {
handleInterrupts(res, async () => {
const user = await authorizeUser(req)
if (req.body.name.length === 0) {
throw new Interrupt(400, { error: "Name is empty" })
}
const create = {
slug: cryptoRandomString({ length: 12, type: "url-safe" }),
creatorId: user.id,
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-06-02 02:26:52 +00:00
}
await restInPeace(req, res, {
model: database.event,
create: { create },
})
})
}