mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
import NextConnectConfig from '@/config/nextConnect/NextConnectConfig';
|
|
import ServerError from '@/config/util/ServerError';
|
|
import createNewBeerComment from '@/services/BeerComment/createNewBeerComment';
|
|
import { BeerCommentQueryResultT } from '@/services/BeerComment/schema/BeerCommentQueryResult';
|
|
import BeerCommentValidationSchema from '@/services/BeerComment/schema/CreateBeerCommentValidationSchema';
|
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
|
import { NextApiHandler } from 'next';
|
|
import nextConnect from 'next-connect';
|
|
import { z } from 'zod';
|
|
|
|
const createComment: NextApiHandler<z.infer<typeof APIResponseValidationSchema>> = async (
|
|
req,
|
|
res,
|
|
) => {
|
|
const cleanedReqBody = BeerCommentValidationSchema.safeParse(req.body);
|
|
if (!cleanedReqBody.success) {
|
|
throw new ServerError('Invalid request body', 400);
|
|
}
|
|
const { content, rating, beerPostId } = cleanedReqBody.data;
|
|
|
|
const newBeerComment: BeerCommentQueryResultT = await createNewBeerComment({
|
|
content,
|
|
rating,
|
|
beerPostId,
|
|
});
|
|
|
|
res.status(201).json({
|
|
message: 'Beer comment created successfully',
|
|
statusCode: 201,
|
|
payload: newBeerComment,
|
|
success: true,
|
|
});
|
|
};
|
|
|
|
const handler = nextConnect(NextConnectConfig).post(createComment);
|
|
|
|
export default handler;
|