refactor: update comments services

This commit is contained in:
Aaron William Po
2023-12-11 18:17:41 -05:00
parent 8b0d182cb3
commit 80404802dc
22 changed files with 406 additions and 413 deletions

View File

@@ -1,13 +1,19 @@
import ServerError from '@/config/util/ServerError';
import DBClient from '@/prisma/DBClient';
import editBeerCommentById from '@/services/comments/beer-comment/editBeerCommentById';
import findBeerCommentById from '@/services/comments/beer-comment/findBeerCommentById';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next';
import { NextHandler } from 'next-connect';
import { z } from 'zod';
import createNewBeerComment from '@/services/comments/beer-comment/createNewBeerComment';
import getAllBeerComments from '@/services/comments/beer-comment/getAllBeerComments';
import ServerError from '@/config/util/ServerError';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import {
getBeerPostCommentByIdService,
editBeerPostCommentByIdService,
createBeerPostCommentService,
getAllBeerCommentsService,
deleteBeerCommentByIdService,
getBeerPostCommentCountService,
} from '@/services/comments/beer-comment';
import {
CommentRequest,
EditAndCreateCommentRequest,
@@ -21,7 +27,7 @@ export const checkIfBeerCommentOwner = async <T extends CommentRequest>(
) => {
const { id } = req.query;
const user = req.user!;
const comment = await findBeerCommentById({ beerCommentId: id });
const comment = await getBeerPostCommentByIdService({ beerPostCommentId: id });
if (!comment) {
throw new ServerError('Comment not found', 404);
@@ -40,17 +46,12 @@ export const editBeerPostComment = async (
) => {
const { id } = req.query;
const updated = await editBeerCommentById({
content: req.body.content,
rating: req.body.rating,
id,
});
await editBeerPostCommentByIdService({ body: req.body, beerPostCommentId: id });
res.status(200).json({
success: true,
message: 'Comment updated successfully',
statusCode: 200,
payload: updated,
});
};
@@ -60,9 +61,7 @@ export const deleteBeerPostComment = async (
) => {
const { id } = req.query;
await DBClient.instance.beerComment.delete({
where: { id },
});
await deleteBeerCommentByIdService({ beerPostCommentId: id });
res.status(200).json({
success: true,
@@ -75,15 +74,12 @@ export const createBeerPostComment = async (
req: EditAndCreateCommentRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const { content, rating } = req.body;
const beerPostId = req.query.id;
const newBeerComment = await createNewBeerComment({
content,
rating,
beerPostId,
const newBeerComment = await createBeerPostCommentService({
body: req.body,
userId: req.user!.id,
beerPostId,
});
res.status(201).json({
@@ -102,13 +98,13 @@ export const getAllBeerPostComments = async (
// eslint-disable-next-line @typescript-eslint/naming-convention
const { page_size, page_num } = req.query;
const comments = await getAllBeerComments({
const comments = await getAllBeerCommentsService({
beerPostId,
pageNum: parseInt(page_num, 10),
pageSize: parseInt(page_size, 10),
});
const count = await DBClient.instance.beerComment.count({ where: { beerPostId } });
const count = await getBeerPostCommentCountService({ beerPostId });
res.setHeader('X-Total-Count', count);

View File

@@ -1,14 +1,18 @@
import ServerError from '@/config/util/ServerError';
import DBClient from '@/prisma/DBClient';
import updateBeerStyleCommentById from '@/services/comments/beer-style-comment/updateBeerStyleCommentById';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next';
import { NextHandler } from 'next-connect';
import { z } from 'zod';
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import createNewBeerStyleComment from '@/services/comments/beer-style-comment/createNewBeerStyleComment';
import getAllBeerStyleComments from '@/services/comments/beer-style-comment/getAllBeerStyleComments';
import {
updateBeerStyleCommentById,
createNewBeerStyleComment,
getAllBeerStyleComments,
findBeerStyleCommentById,
deleteBeerStyleCommentById,
} from '@/services/comments/beer-style-comment';
import {
CommentRequest,
@@ -25,15 +29,14 @@ export const checkIfBeerStyleCommentOwner = async <
) => {
const { id } = req.query;
const user = req.user!;
const beerStyleComment = await DBClient.instance.beerStyleComment.findFirst({
where: { id },
});
const beerStyleComment = await findBeerStyleCommentById({ beerStyleCommentId: id });
if (!beerStyleComment) {
throw new ServerError('Beer style comment not found.', 404);
}
if (beerStyleComment.postedById !== user.id) {
if (beerStyleComment.postedBy.id !== user.id) {
throw new ServerError(
'You are not authorized to modify this beer style comment.',
403,
@@ -47,8 +50,8 @@ export const editBeerStyleComment = async (
req: EditAndCreateCommentRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const updated = await updateBeerStyleCommentById({
id: req.query.id,
await updateBeerStyleCommentById({
beerStyleCommentId: req.query.id,
body: req.body,
});
@@ -56,7 +59,6 @@ export const editBeerStyleComment = async (
success: true,
message: 'Comment updated successfully',
statusCode: 200,
payload: updated,
});
};
@@ -66,7 +68,7 @@ export const deleteBeerStyleComment = async (
) => {
const { id } = req.query;
await DBClient.instance.beerStyleComment.delete({ where: { id } });
await deleteBeerStyleCommentById({ beerStyleCommentId: id });
res.status(200).json({
success: true,
@@ -79,15 +81,11 @@ export const createComment = async (
req: EditAndCreateCommentRequest,
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,
});
const newBeerStyleComment = await createNewBeerStyleComment({
body: req.body,
beerStyleId: req.query.id,
userId: req.user!.id,
});
res.status(201).json({
message: 'Beer comment created successfully',

View File

@@ -1,13 +1,18 @@
import ServerError from '@/config/util/ServerError';
import DBClient from '@/prisma/DBClient';
import getBreweryCommentById from '@/services/comments/brewery-comment/getBreweryCommentById';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next';
import { NextHandler } from 'next-connect';
import { z } from 'zod';
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import createNewBreweryComment from '@/services/comments/brewery-comment/createNewBreweryComment';
import getAllBreweryComments from '@/services/comments/brewery-comment/getAllBreweryComments';
import {
getBreweryCommentById,
createNewBreweryComment,
getAllBreweryComments,
deleteBreweryCommentByIdService,
updateBreweryCommentById,
getBreweryCommentCount,
} from '@/services/comments/brewery-comment';
import {
CommentRequest,
EditAndCreateCommentRequest,
@@ -23,13 +28,13 @@ export const checkIfBreweryCommentOwner = async <
) => {
const { id } = req.query;
const user = req.user!;
const comment = await getBreweryCommentById(id);
const comment = await getBreweryCommentById({ breweryCommentId: id });
if (!comment) {
throw new ServerError('Comment not found', 404);
}
if (comment.postedById !== user.id) {
if (comment.postedBy.id !== user.id) {
throw new ServerError('You are not authorized to modify this comment', 403);
}
@@ -42,13 +47,9 @@ export const editBreweryPostComment = async (
) => {
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(),
},
const updated = updateBreweryCommentById({
breweryCommentId: id,
body: req.body,
});
return res.status(200).json({
@@ -65,7 +66,7 @@ export const deleteBreweryPostComment = async (
) => {
const { id } = req.query;
await DBClient.instance.breweryComment.delete({ where: { id } });
await deleteBreweryCommentByIdService({ breweryCommentId: id });
res.status(200).json({
success: true,
@@ -78,19 +79,15 @@ export const createComment = async (
req: EditAndCreateCommentRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const { content, rating } = req.body;
const breweryPostId = req.query.id;
const user = req.user!;
const newBreweryComment: z.infer<typeof CommentQueryResult> =
await createNewBreweryComment({
content,
rating,
breweryPostId,
userId: user.id,
});
const newBreweryComment = await createNewBreweryComment({
body: req.body,
breweryPostId,
userId: user.id,
});
res.status(201).json({
message: 'Beer comment created successfully',
@@ -114,9 +111,7 @@ export const getAll = async (
pageSize: parseInt(page_size, 10),
});
const count = await DBClient.instance.breweryComment.count({
where: { breweryPostId },
});
const count = await getBreweryCommentCount({ breweryPostId });
res.setHeader('X-Total-Count', count);