mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
78 lines
1.9 KiB
TypeScript
78 lines
1.9 KiB
TypeScript
import BeerPostValidationSchema from '@/validation/CreateBeerPostValidationSchema';
|
|
import DBClient from '@/prisma/DBClient';
|
|
import { NextApiHandler } from 'next';
|
|
|
|
import { z } from 'zod';
|
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
|
import ServerError from '@/config/util/ServerError';
|
|
|
|
const handler: NextApiHandler<z.infer<typeof APIResponseValidationSchema>> = async (
|
|
req,
|
|
res,
|
|
) => {
|
|
try {
|
|
const { method } = req;
|
|
|
|
if (method !== 'POST') {
|
|
throw new ServerError('Method not allowed', 405);
|
|
}
|
|
|
|
const cleanedReqBody = BeerPostValidationSchema.safeParse(req.body);
|
|
if (!cleanedReqBody.success) {
|
|
throw new ServerError('Invalid request body', 400);
|
|
}
|
|
|
|
const { name, description, typeId, abv, ibu, breweryId } = cleanedReqBody.data;
|
|
const user = await DBClient.instance.user.findFirstOrThrow();
|
|
|
|
const newBeerPost = await DBClient.instance.beerPost.create({
|
|
data: {
|
|
name,
|
|
description,
|
|
abv,
|
|
ibu,
|
|
type: {
|
|
connect: {
|
|
id: typeId,
|
|
},
|
|
},
|
|
postedBy: {
|
|
connect: {
|
|
id: user.id,
|
|
},
|
|
},
|
|
brewery: {
|
|
connect: {
|
|
id: breweryId,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
res.status(201).json({
|
|
message: 'Beer post created successfully',
|
|
statusCode: 201,
|
|
payload: newBeerPost,
|
|
success: true,
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof ServerError) {
|
|
res.status(error.statusCode).json({
|
|
message: error.message,
|
|
statusCode: error.statusCode,
|
|
payload: null,
|
|
success: false,
|
|
});
|
|
} else {
|
|
res.status(500).json({
|
|
message: 'Internal server error',
|
|
statusCode: 500,
|
|
payload: null,
|
|
success: false,
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
export default handler;
|