mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Update api routes and begin to extract controllers out of routing logic
This commit is contained in:
66
src/controllers/beerComments/index.ts
Normal file
66
src/controllers/beerComments/index.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import editBeerCommentById from '@/services/BeerComment/editBeerCommentById';
|
||||
import findBeerCommentById from '@/services/BeerComment/findBeerCommentById';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { NextHandler } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import { CommentRequest, EditCommentRequest } from '../requestTypes';
|
||||
|
||||
export const checkIfBeerCommentOwner = async <T extends CommentRequest>(
|
||||
req: T,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
next: NextHandler,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
const user = req.user!;
|
||||
const comment = await findBeerCommentById({ beerCommentId: id });
|
||||
|
||||
if (!comment) {
|
||||
throw new ServerError('Comment not found', 404);
|
||||
}
|
||||
|
||||
if (comment.postedBy.id !== user.id) {
|
||||
throw new ServerError('You are not authorized to modify this comment', 403);
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
export const editBeerPostComment = async (
|
||||
req: EditCommentRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
|
||||
const updated = await editBeerCommentById({
|
||||
content: req.body.content,
|
||||
rating: req.body.rating,
|
||||
id,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Comment updated successfully',
|
||||
statusCode: 200,
|
||||
payload: updated,
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteBeerPostComment = async (
|
||||
req: CommentRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
|
||||
await DBClient.instance.beerComment.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Comment deleted successfully',
|
||||
statusCode: 200,
|
||||
});
|
||||
};
|
||||
67
src/controllers/beerStyleComments/index.ts
Normal file
67
src/controllers/beerStyleComments/index.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import updateBeerStyleCommentById from '@/services/BeerStyleComment/updateBeerStyleCommentById';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { NextHandler } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import { CommentRequest, EditCommentRequest } from '../requestTypes';
|
||||
|
||||
export const checkIfBeerStyleCommentOwner = async <
|
||||
CommentRequestType extends CommentRequest,
|
||||
>(
|
||||
req: CommentRequestType,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
next: NextHandler,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
const user = req.user!;
|
||||
const beerStyleComment = await DBClient.instance.beerStyleComment.findFirst({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!beerStyleComment) {
|
||||
throw new ServerError('Beer style comment not found.', 404);
|
||||
}
|
||||
|
||||
if (beerStyleComment.postedById !== user.id) {
|
||||
throw new ServerError(
|
||||
'You are not authorized to modify this beer style comment.',
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
export const editBeerStyleComment = async (
|
||||
req: EditCommentRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const updated = await updateBeerStyleCommentById({
|
||||
id: req.query.id,
|
||||
body: req.body,
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: 'Comment updated successfully',
|
||||
statusCode: 200,
|
||||
payload: updated,
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteBeerStyleComment = async (
|
||||
req: CommentRequest,
|
||||
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,
|
||||
});
|
||||
};
|
||||
68
src/controllers/breweryComments/index.ts
Normal file
68
src/controllers/breweryComments/index.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import getBreweryCommentById from '@/services/BreweryComment/getBreweryCommentById';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { NextHandler } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import { CommentRequest, EditCommentRequest } from '../requestTypes';
|
||||
|
||||
export const checkIfBreweryCommentOwner = async <
|
||||
CommentRequestType extends CommentRequest,
|
||||
>(
|
||||
req: CommentRequestType,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
next: NextHandler,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
const user = req.user!;
|
||||
const comment = await getBreweryCommentById(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();
|
||||
};
|
||||
|
||||
export const editBreweryPostComment = async (
|
||||
req: EditCommentRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
|
||||
const updated = await DBClient.instance.breweryComment.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,
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteBreweryPostComment = async (
|
||||
req: CommentRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
|
||||
await DBClient.instance.breweryComment.delete({ where: { id } });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Comment deleted successfully',
|
||||
statusCode: 200,
|
||||
});
|
||||
};
|
||||
11
src/controllers/requestTypes/index.ts
Normal file
11
src/controllers/requestTypes/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
||||
import { z } from 'zod';
|
||||
|
||||
export interface CommentRequest extends UserExtendedNextApiRequest {
|
||||
query: { id: string };
|
||||
}
|
||||
|
||||
export interface EditCommentRequest extends CommentRequest {
|
||||
body: z.infer<typeof CreateCommentValidationSchema>;
|
||||
}
|
||||
Reference in New Issue
Block a user