mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
feat: add beer style comments
This commit is contained in:
106
src/pages/api/beer-style-comments/[id].ts
Normal file
106
src/pages/api/beer-style-comments/[id].ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
||||
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { createRouter, NextHandler } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
|
||||
interface DeleteCommentRequest extends UserExtendedNextApiRequest {
|
||||
query: { id: string };
|
||||
}
|
||||
|
||||
interface EditCommentRequest extends UserExtendedNextApiRequest {
|
||||
query: { id: string };
|
||||
body: z.infer<typeof CreateCommentValidationSchema>;
|
||||
}
|
||||
|
||||
const checkIfCommentOwner = async (
|
||||
req: DeleteCommentRequest | EditCommentRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
next: NextHandler,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
const user = req.user!;
|
||||
const comment = await DBClient.instance.beerStyleComment.findFirst({ where: { id } });
|
||||
|
||||
if (!comment) {
|
||||
throw new ServerError('Comment not found', 404);
|
||||
}
|
||||
|
||||
if (comment.postedById !== user.id) {
|
||||
throw new ServerError('You are not authorized to modify this comment', 403);
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
const editComment = async (
|
||||
req: EditCommentRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
|
||||
const updated = await DBClient.instance.beerStyleComment.update({
|
||||
where: { id },
|
||||
data: {
|
||||
content: req.body.content,
|
||||
rating: req.body.rating,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: 'Comment updated successfully',
|
||||
statusCode: 200,
|
||||
payload: updated,
|
||||
});
|
||||
};
|
||||
|
||||
const deleteComment = async (
|
||||
req: DeleteCommentRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
|
||||
await DBClient.instance.beerStyleComment.delete({ where: { id } });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Comment deleted successfully',
|
||||
statusCode: 200,
|
||||
});
|
||||
};
|
||||
|
||||
const router = createRouter<
|
||||
DeleteCommentRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router
|
||||
.delete(
|
||||
validateRequest({
|
||||
querySchema: z.object({ id: z.string().cuid() }),
|
||||
}),
|
||||
getCurrentUser,
|
||||
checkIfCommentOwner,
|
||||
deleteComment,
|
||||
)
|
||||
.put(
|
||||
validateRequest({
|
||||
querySchema: z.object({ id: z.string().cuid() }),
|
||||
bodySchema: CreateCommentValidationSchema,
|
||||
}),
|
||||
getCurrentUser,
|
||||
checkIfCommentOwner,
|
||||
editComment,
|
||||
);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
export default handler;
|
||||
103
src/pages/api/beers/styles/[id]/comments/index.ts
Normal file
103
src/pages/api/beers/styles/[id]/comments/index.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||
|
||||
import { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import { NextApiResponse } from 'next';
|
||||
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
|
||||
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
||||
import createNewBeerStyleComment from '@/services/BeerStyleComment/createNewBeerStyleComment';
|
||||
import getAllBeerStyleComments from '@/services/BeerStyleComment/getAllBeerStyleComments';
|
||||
|
||||
interface CreateCommentRequest extends UserExtendedNextApiRequest {
|
||||
body: z.infer<typeof CreateCommentValidationSchema>;
|
||||
query: { id: string };
|
||||
}
|
||||
|
||||
interface GetAllCommentsRequest extends UserExtendedNextApiRequest {
|
||||
query: { id: string; page_size: string; page_num: string };
|
||||
}
|
||||
|
||||
const createComment = async (
|
||||
req: CreateCommentRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { content, rating } = req.body;
|
||||
|
||||
const newBeerStyleComment: z.infer<typeof CommentQueryResult> =
|
||||
await createNewBeerStyleComment({
|
||||
content,
|
||||
rating,
|
||||
beerStyleId: req.query.id,
|
||||
userId: req.user!.id,
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
message: 'Beer comment created successfully',
|
||||
statusCode: 201,
|
||||
payload: newBeerStyleComment,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
const getAll = async (
|
||||
req: GetAllCommentsRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const beerStyleId = req.query.id;
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
const { page_size, page_num } = req.query;
|
||||
|
||||
const comments = await getAllBeerStyleComments({
|
||||
beerStyleId,
|
||||
pageNum: parseInt(page_num, 10),
|
||||
pageSize: parseInt(page_size, 10),
|
||||
});
|
||||
|
||||
const pageCount = await DBClient.instance.beerStyleComment.count({
|
||||
where: { beerStyleId },
|
||||
});
|
||||
|
||||
res.setHeader('X-Total-Count', pageCount);
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Beer comments fetched successfully',
|
||||
statusCode: 200,
|
||||
payload: comments,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
const router = createRouter<
|
||||
// I don't want to use any, but I can't figure out how to get the types to work
|
||||
any,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.post(
|
||||
validateRequest({
|
||||
bodySchema: CreateCommentValidationSchema,
|
||||
querySchema: z.object({ id: z.string().cuid() }),
|
||||
}),
|
||||
getCurrentUser,
|
||||
createComment,
|
||||
);
|
||||
|
||||
router.get(
|
||||
validateRequest({
|
||||
querySchema: z.object({
|
||||
id: z.string().cuid(),
|
||||
page_size: z.coerce.number().int().positive(),
|
||||
page_num: z.coerce.number().int().positive(),
|
||||
}),
|
||||
}),
|
||||
getAll,
|
||||
);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
export default handler;
|
||||
@@ -8,12 +8,13 @@ import { Tab } from '@headlessui/react';
|
||||
import getBeerStyleById from '@/services/BeerStyles/getBeerStyleById';
|
||||
import BeerStyleHeader from '@/components/BeerStyleById/BeerStyleHeader';
|
||||
import BeerStyleQueryResult from '@/services/BeerStyles/schema/BeerStyleQueryResult';
|
||||
import BeerStyleCommentSection from '@/components/BeerStyleById/BeerStyleCommentSection';
|
||||
|
||||
interface BeerStylePageProps {
|
||||
beerStyle: z.infer<typeof BeerStyleQueryResult>;
|
||||
}
|
||||
|
||||
const BeerByIdPage: NextPage<BeerStylePageProps> = ({ beerStyle }) => {
|
||||
const BeerStyleByIdPage: NextPage<BeerStylePageProps> = ({ beerStyle }) => {
|
||||
const isDesktop = useMediaQuery('(min-width: 1024px)');
|
||||
|
||||
return (
|
||||
@@ -29,8 +30,10 @@ const BeerByIdPage: NextPage<BeerStylePageProps> = ({ beerStyle }) => {
|
||||
|
||||
{isDesktop ? (
|
||||
<div className="mt-4 flex flex-row space-x-3 space-y-0">
|
||||
<div className="w-[60%]">{/* Comments go here */}</div>
|
||||
<div className="w-[40%]">{/* Recommendations go here */}</div>
|
||||
<div className="w-[60%]">
|
||||
<BeerStyleCommentSection beerStyle={beerStyle} />
|
||||
</div>
|
||||
<div className="w-[40%]">{/* Beers of this style go here */}</div>
|
||||
</div>
|
||||
) : (
|
||||
<Tab.Group>
|
||||
@@ -43,8 +46,10 @@ const BeerByIdPage: NextPage<BeerStylePageProps> = ({ beerStyle }) => {
|
||||
</Tab>
|
||||
</Tab.List>
|
||||
<Tab.Panels className="mt-2">
|
||||
<Tab.Panel>{/* Comments go here */}</Tab.Panel>
|
||||
<Tab.Panel>{/* Recommendations go here */}</Tab.Panel>
|
||||
<Tab.Panel>
|
||||
<BeerStyleCommentSection beerStyle={beerStyle} />
|
||||
</Tab.Panel>
|
||||
<Tab.Panel>{/* Beers of this style go here */}</Tab.Panel>
|
||||
</Tab.Panels>
|
||||
</Tab.Group>
|
||||
)}
|
||||
@@ -55,7 +60,7 @@ const BeerByIdPage: NextPage<BeerStylePageProps> = ({ beerStyle }) => {
|
||||
);
|
||||
};
|
||||
|
||||
export default BeerByIdPage;
|
||||
export default BeerStyleByIdPage;
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = async ({ params }) => {
|
||||
const id = params!.id as string;
|
||||
|
||||
Reference in New Issue
Block a user