diff --git a/src/controllers/comments/beer-comments/index.ts b/src/controllers/comments/beer-comments/index.ts index d2fbf4c..2dc7327 100644 --- a/src/controllers/comments/beer-comments/index.ts +++ b/src/controllers/comments/beer-comments/index.ts @@ -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 ( ) => { 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>, ) => { - 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); diff --git a/src/controllers/comments/beer-style-comments/index.ts b/src/controllers/comments/beer-style-comments/index.ts index f50dc59..2ca878b 100644 --- a/src/controllers/comments/beer-style-comments/index.ts +++ b/src/controllers/comments/beer-style-comments/index.ts @@ -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>, ) => { - 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>, ) => { - const { content, rating } = req.body; - - const newBeerStyleComment: z.infer = - 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', diff --git a/src/controllers/comments/brewery-comments/index.ts b/src/controllers/comments/brewery-comments/index.ts index 86484fc..81e6a3a 100644 --- a/src/controllers/comments/brewery-comments/index.ts +++ b/src/controllers/comments/brewery-comments/index.ts @@ -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>, ) => { - const { content, rating } = req.body; - const breweryPostId = req.query.id; const user = req.user!; - const newBreweryComment: z.infer = - 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); diff --git a/src/controllers/users/profile/index.ts b/src/controllers/users/profile/index.ts index ffcf253..de93f05 100644 --- a/src/controllers/users/profile/index.ts +++ b/src/controllers/users/profile/index.ts @@ -10,7 +10,8 @@ import updateUserAvatarById, { } from '@/services/users/account/UpdateUserAvatarByIdParams'; import { UserExtendedNextApiRequest } from '@/config/auth/types'; import updateUserProfileById from '@/services/users/auth/updateUserProfileById'; - +import getUsersFollowingUser from '@/services/users/follows/getUsersFollowingUser'; +import getUsersFollowedByUser from '@/services/users/follows/getUsersFollowedByUser'; import { UserRouteRequest, GetUserFollowInfoRequest, @@ -18,8 +19,6 @@ import { UpdateAvatarRequest, UpdateProfileRequest, } from './types'; -import getUsersFollowingUser from '@/services/users/follows/getUsersFollowingUser'; -import getUsersFollowedByUser from '@/services/users/follows/getUsersFollowedByUser'; export const followUser = async ( req: UserRouteRequest, diff --git a/src/services/comments/beer-comment/createNewBeerComment.ts b/src/services/comments/beer-comment/createNewBeerComment.ts deleted file mode 100644 index 664e19e..0000000 --- a/src/services/comments/beer-comment/createNewBeerComment.ts +++ /dev/null @@ -1,37 +0,0 @@ -import DBClient from '@/prisma/DBClient'; -import { z } from 'zod'; -import CreateCommentValidationSchema from '../../schema/CommentSchema/CreateCommentValidationSchema'; -import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult'; - -const CreateNewBeerCommentServiceSchema = CreateCommentValidationSchema.extend({ - userId: z.string().cuid(), - beerPostId: z.string().cuid(), -}); - -type CreateNewBeerCommentArgs = z.infer; - -const createNewBeerComment = async ({ - content, - rating, - beerPostId, - userId, -}: CreateNewBeerCommentArgs): Promise> => { - return DBClient.instance.beerComment.create({ - data: { - content, - rating, - beerPost: { connect: { id: beerPostId } }, - postedBy: { connect: { id: userId } }, - }, - select: { - id: true, - content: true, - rating: true, - postedBy: { select: { id: true, username: true, userAvatar: true } }, - createdAt: true, - updatedAt: true, - }, - }); -}; - -export default createNewBeerComment; diff --git a/src/services/comments/beer-comment/editBeerCommentById.ts b/src/services/comments/beer-comment/editBeerCommentById.ts deleted file mode 100644 index 5449bf4..0000000 --- a/src/services/comments/beer-comment/editBeerCommentById.ts +++ /dev/null @@ -1,32 +0,0 @@ -import DBClient from '@/prisma/DBClient'; -import { z } from 'zod'; -import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult'; - -interface EditBeerCommentByIdArgs { - id: string; - content: string; - rating: number; -} - -const editBeerCommentById = async ({ - id, - content, - rating, -}: EditBeerCommentByIdArgs): Promise> => { - return DBClient.instance.beerComment.update({ - where: { id }, - data: { content, rating, updatedAt: new Date() }, - select: { - id: true, - content: true, - rating: true, - createdAt: true, - updatedAt: true, - postedBy: { - select: { id: true, username: true, createdAt: true, userAvatar: true }, - }, - }, - }); -}; - -export default editBeerCommentById; diff --git a/src/services/comments/beer-comment/findBeerCommentById.ts b/src/services/comments/beer-comment/findBeerCommentById.ts deleted file mode 100644 index e27acff..0000000 --- a/src/services/comments/beer-comment/findBeerCommentById.ts +++ /dev/null @@ -1,27 +0,0 @@ -import DBClient from '@/prisma/DBClient'; -import { z } from 'zod'; -import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult'; - -interface FindBeerCommentArgs { - beerCommentId: string; -} - -const findBeerCommentById = async ({ - beerCommentId, -}: FindBeerCommentArgs): Promise | null> => { - return DBClient.instance.beerComment.findUnique({ - where: { id: beerCommentId }, - select: { - id: true, - content: true, - rating: true, - createdAt: true, - updatedAt: true, - postedBy: { - select: { id: true, username: true, createdAt: true, userAvatar: true }, - }, - }, - }); -}; - -export default findBeerCommentById; diff --git a/src/services/comments/beer-comment/getAllBeerComments.ts b/src/services/comments/beer-comment/getAllBeerComments.ts deleted file mode 100644 index 26c5e1d..0000000 --- a/src/services/comments/beer-comment/getAllBeerComments.ts +++ /dev/null @@ -1,34 +0,0 @@ -import DBClient from '@/prisma/DBClient'; -import { z } from 'zod'; -import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult'; - -interface GetAllBeerCommentsArgs { - beerPostId: string; - pageNum: number; - pageSize: number; -} - -const getAllBeerComments = async ({ - beerPostId, - pageNum, - pageSize, -}: GetAllBeerCommentsArgs): Promise[]> => { - return DBClient.instance.beerComment.findMany({ - skip: (pageNum - 1) * pageSize, - take: pageSize, - where: { beerPostId }, - orderBy: { createdAt: 'desc' }, - select: { - id: true, - content: true, - rating: true, - createdAt: true, - updatedAt: true, - postedBy: { - select: { id: true, username: true, createdAt: true, userAvatar: true }, - }, - }, - }); -}; - -export default getAllBeerComments; diff --git a/src/services/comments/beer-comment/getBeerCommentCount.ts b/src/services/comments/beer-comment/getBeerCommentCount.ts deleted file mode 100644 index 989e7cd..0000000 --- a/src/services/comments/beer-comment/getBeerCommentCount.ts +++ /dev/null @@ -1,13 +0,0 @@ -import DBClient from '@/prisma/DBClient'; - -interface GetBeerCommentCountArgs { - beerPostId: string; -} - -const getBeerCommentCount = async ({ - beerPostId, -}: GetBeerCommentCountArgs): Promise => { - return DBClient.instance.beerComment.count({ where: { beerPostId } }); -}; - -export default getBeerCommentCount; diff --git a/src/services/comments/beer-comment/index.ts b/src/services/comments/beer-comment/index.ts new file mode 100644 index 0000000..6b82814 --- /dev/null +++ b/src/services/comments/beer-comment/index.ts @@ -0,0 +1,87 @@ +import DBClient from '@/prisma/DBClient'; + +import { + CreateBeerPostComment, + EditBeerPostCommentById, + FindOrDeleteBeerPostCommentById, + GetAllBeerPostComments, + GetBeerPostCommentCount, +} from './types'; + +const beerPostCommentSelect = { + id: true, + content: true, + rating: true, + createdAt: true, + updatedAt: true, + postedBy: { + select: { id: true, username: true, createdAt: true, userAvatar: true }, + }, +} as const; + +export const createBeerPostCommentService: CreateBeerPostComment = ({ + body, + beerPostId, + userId, +}) => { + const { content, rating } = body; + return DBClient.instance.beerComment.create({ + data: { + content, + rating, + beerPost: { connect: { id: beerPostId } }, + postedBy: { connect: { id: userId } }, + }, + select: beerPostCommentSelect, + }); +}; + +export const editBeerPostCommentByIdService: EditBeerPostCommentById = ({ + beerPostCommentId, + body, +}) => { + const { content, rating } = body; + return DBClient.instance.beerComment.update({ + where: { id: beerPostCommentId }, + data: { content, rating, updatedAt: new Date() }, + select: beerPostCommentSelect, + }); +}; + +export const getBeerPostCommentByIdService: FindOrDeleteBeerPostCommentById = ({ + beerPostCommentId, +}) => { + return DBClient.instance.beerComment.findUnique({ + where: { id: beerPostCommentId }, + select: beerPostCommentSelect, + }); +}; + +export const deleteBeerCommentByIdService: FindOrDeleteBeerPostCommentById = ({ + beerPostCommentId, +}) => { + return DBClient.instance.beerComment.delete({ + where: { id: beerPostCommentId }, + select: beerPostCommentSelect, + }); +}; + +export const getAllBeerCommentsService: GetAllBeerPostComments = ({ + beerPostId, + pageNum, + pageSize, +}) => { + return DBClient.instance.beerComment.findMany({ + skip: (pageNum - 1) * pageSize, + take: pageSize, + where: { beerPostId }, + orderBy: { createdAt: 'desc' }, + select: beerPostCommentSelect, + }); +}; + +export const getBeerPostCommentCountService: GetBeerPostCommentCount = ({ + beerPostId, +}) => { + return DBClient.instance.beerComment.count({ where: { beerPostId } }); +}; diff --git a/src/services/comments/beer-comment/types/index.ts b/src/services/comments/beer-comment/types/index.ts new file mode 100644 index 0000000..63236e3 --- /dev/null +++ b/src/services/comments/beer-comment/types/index.ts @@ -0,0 +1,28 @@ +import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult'; +import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema'; +import { z } from 'zod'; + +type BeerPostComment = z.infer; + +export type CreateBeerPostComment = (args: { + body: z.infer; + userId: string; + beerPostId: string; +}) => Promise; + +export type EditBeerPostCommentById = (args: { + body: z.infer; + beerPostCommentId: string; +}) => Promise; + +export type FindOrDeleteBeerPostCommentById = (args: { + beerPostCommentId: string; +}) => Promise; + +export type GetBeerPostCommentCount = (args: { beerPostId: string }) => Promise; + +export type GetAllBeerPostComments = (args: { + beerPostId: string; + pageNum: number; + pageSize: number; +}) => Promise; diff --git a/src/services/comments/beer-style-comment/createNewBeerStyleComment.ts b/src/services/comments/beer-style-comment/createNewBeerStyleComment.ts deleted file mode 100644 index 8197438..0000000 --- a/src/services/comments/beer-style-comment/createNewBeerStyleComment.ts +++ /dev/null @@ -1,39 +0,0 @@ -import DBClient from '@/prisma/DBClient'; -import { z } from 'zod'; -import CreateCommentValidationSchema from '../../schema/CommentSchema/CreateCommentValidationSchema'; -import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult'; - -const CreateNewBeerStyleCommentServiceSchema = CreateCommentValidationSchema.extend({ - userId: z.string().cuid(), - beerStyleId: z.string().cuid(), -}); - -type CreateNewBeerCommentArgs = z.infer; - -const createNewBeerStyleComment = async ({ - content, - rating, - userId, - beerStyleId, -}: CreateNewBeerCommentArgs): Promise> => { - return DBClient.instance.beerStyleComment.create({ - data: { - content, - rating, - beerStyle: { connect: { id: beerStyleId } }, - postedBy: { connect: { id: userId } }, - }, - select: { - id: true, - content: true, - rating: true, - createdAt: true, - updatedAt: true, - postedBy: { - select: { id: true, username: true, createdAt: true, userAvatar: true }, - }, - }, - }); -}; - -export default createNewBeerStyleComment; diff --git a/src/services/comments/beer-style-comment/getAllBeerStyleComments.ts b/src/services/comments/beer-style-comment/getAllBeerStyleComments.ts deleted file mode 100644 index 08219bf..0000000 --- a/src/services/comments/beer-style-comment/getAllBeerStyleComments.ts +++ /dev/null @@ -1,34 +0,0 @@ -import DBClient from '@/prisma/DBClient'; -import { z } from 'zod'; -import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult'; - -interface GetAllBeerStyleCommentArgs { - beerStyleId: string; - pageNum: number; - pageSize: number; -} - -const getAllBeerStyleComments = async ({ - beerStyleId, - pageNum, - pageSize, -}: GetAllBeerStyleCommentArgs): Promise[]> => { - return DBClient.instance.beerStyleComment.findMany({ - skip: (pageNum - 1) * pageSize, - take: pageSize, - where: { beerStyleId }, - orderBy: { createdAt: 'desc' }, - select: { - id: true, - content: true, - rating: true, - createdAt: true, - updatedAt: true, - postedBy: { - select: { id: true, username: true, createdAt: true, userAvatar: true }, - }, - }, - }); -}; - -export default getAllBeerStyleComments; diff --git a/src/services/comments/beer-style-comment/getBeerStyleCommentCount.ts b/src/services/comments/beer-style-comment/getBeerStyleCommentCount.ts deleted file mode 100644 index c52185c..0000000 --- a/src/services/comments/beer-style-comment/getBeerStyleCommentCount.ts +++ /dev/null @@ -1,15 +0,0 @@ -import DBClient from '@/prisma/DBClient'; - -interface GetBeerStyleCommentCountArgs { - beerStyleId: string; -} - -const getBeerCommentCount = async ({ - beerStyleId, -}: GetBeerStyleCommentCountArgs): Promise => { - return DBClient.instance.beerStyleComment.count({ - where: { beerStyleId }, - }); -}; - -export default getBeerCommentCount; diff --git a/src/services/comments/beer-style-comment/index.ts b/src/services/comments/beer-style-comment/index.ts new file mode 100644 index 0000000..172ea3a --- /dev/null +++ b/src/services/comments/beer-style-comment/index.ts @@ -0,0 +1,85 @@ +import DBClient from '@/prisma/DBClient'; +import { + CreateNewBeerStyleComment, + GetAllBeerStyleComments, + GetBeerStyleCommentCount, + UpdateBeerStyleCommentById, + FindOrDeleteBeerStyleCommentById, +} from './types'; + +const beerStyleCommentSelect = { + id: true, + content: true, + rating: true, + createdAt: true, + updatedAt: true, + postedBy: { + select: { id: true, username: true, createdAt: true, userAvatar: true }, + }, +} as const; + +export const createNewBeerStyleComment: CreateNewBeerStyleComment = ({ + body, + userId, + beerStyleId, +}) => { + const { content, rating } = body; + return DBClient.instance.beerStyleComment.create({ + data: { + content, + rating, + beerStyle: { connect: { id: beerStyleId } }, + postedBy: { connect: { id: userId } }, + }, + select: beerStyleCommentSelect, + }); +}; + +export const getAllBeerStyleComments: GetAllBeerStyleComments = ({ + beerStyleId, + pageNum, + pageSize, +}) => { + return DBClient.instance.beerStyleComment.findMany({ + skip: (pageNum - 1) * pageSize, + take: pageSize, + where: { beerStyleId }, + orderBy: { createdAt: 'desc' }, + select: beerStyleCommentSelect, + }); +}; + +export const getBeerStyleCommentCount: GetBeerStyleCommentCount = ({ beerStyleId }) => { + return DBClient.instance.beerStyleComment.count({ where: { beerStyleId } }); +}; + +export const updateBeerStyleCommentById: UpdateBeerStyleCommentById = ({ + body, + beerStyleCommentId, +}) => { + const { content, rating } = body; + + return DBClient.instance.beerStyleComment.update({ + where: { id: beerStyleCommentId }, + data: { content, rating, updatedAt: new Date() }, + select: beerStyleCommentSelect, + }); +}; + +export const findBeerStyleCommentById: FindOrDeleteBeerStyleCommentById = ({ + beerStyleCommentId, +}) => { + return DBClient.instance.beerStyleComment.findUnique({ + where: { id: beerStyleCommentId }, + select: beerStyleCommentSelect, + }); +}; + +export const deleteBeerStyleCommentById: FindOrDeleteBeerStyleCommentById = ({ + beerStyleCommentId, +}) => { + return DBClient.instance.beerStyleComment.delete({ + where: { id: beerStyleCommentId }, + select: beerStyleCommentSelect, + }); +}; diff --git a/src/services/comments/beer-style-comment/types/index.ts b/src/services/comments/beer-style-comment/types/index.ts new file mode 100644 index 0000000..18c1366 --- /dev/null +++ b/src/services/comments/beer-style-comment/types/index.ts @@ -0,0 +1,28 @@ +import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult'; +import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema'; +import { z } from 'zod'; + +type BeerStyleComment = z.infer; + +export type FindOrDeleteBeerStyleCommentById = (args: { + beerStyleCommentId: string; +}) => Promise; + +export type UpdateBeerStyleCommentById = (args: { + body: z.infer; + beerStyleCommentId: string; +}) => Promise; + +export type GetBeerStyleCommentCount = (args: { beerStyleId: string }) => Promise; + +export type GetAllBeerStyleComments = (args: { + beerStyleId: string; + pageNum: number; + pageSize: number; +}) => Promise; + +export type CreateNewBeerStyleComment = (args: { + body: z.infer; + userId: string; + beerStyleId: string; +}) => Promise; diff --git a/src/services/comments/beer-style-comment/updateBeerStyleCommentById.ts b/src/services/comments/beer-style-comment/updateBeerStyleCommentById.ts deleted file mode 100644 index a457043..0000000 --- a/src/services/comments/beer-style-comment/updateBeerStyleCommentById.ts +++ /dev/null @@ -1,23 +0,0 @@ -import DBClient from '@/prisma/DBClient'; -import { z } from 'zod'; -import CreateCommentValidationSchema from '../../schema/CommentSchema/CreateCommentValidationSchema'; - -interface UpdateBeerStyleCommentByIdParams { - body: z.infer; - id: string; -} - -const updateBeerStyleCommentById = ({ body, id }: UpdateBeerStyleCommentByIdParams) => { - const { content, rating } = body; - - return DBClient.instance.beerStyleComment.update({ - where: { id }, - data: { - content, - rating, - updatedAt: new Date(), - }, - }); -}; - -export default updateBeerStyleCommentById; diff --git a/src/services/comments/brewery-comment/createNewBreweryComment.ts b/src/services/comments/brewery-comment/createNewBreweryComment.ts deleted file mode 100644 index ea8de7b..0000000 --- a/src/services/comments/brewery-comment/createNewBreweryComment.ts +++ /dev/null @@ -1,39 +0,0 @@ -import DBClient from '@/prisma/DBClient'; -import { z } from 'zod'; -import CreateCommentValidationSchema from '../../schema/CommentSchema/CreateCommentValidationSchema'; -import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult'; - -const CreateNewBreweryCommentServiceSchema = CreateCommentValidationSchema.extend({ - userId: z.string().cuid(), - breweryPostId: z.string().cuid(), -}); - -const createNewBreweryComment = async ({ - content, - rating, - breweryPostId, - userId, -}: z.infer): Promise< - z.infer -> => { - return DBClient.instance.breweryComment.create({ - data: { - content, - rating, - breweryPost: { connect: { id: breweryPostId } }, - postedBy: { connect: { id: userId } }, - }, - select: { - id: true, - content: true, - rating: true, - createdAt: true, - updatedAt: true, - postedBy: { - select: { id: true, username: true, createdAt: true, userAvatar: true }, - }, - }, - }); -}; - -export default createNewBreweryComment; diff --git a/src/services/comments/brewery-comment/getAllBreweryComments.ts b/src/services/comments/brewery-comment/getAllBreweryComments.ts deleted file mode 100644 index 5c8a1a7..0000000 --- a/src/services/comments/brewery-comment/getAllBreweryComments.ts +++ /dev/null @@ -1,35 +0,0 @@ -import DBClient from '@/prisma/DBClient'; -import { z } from 'zod'; -import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult'; - -const getAllBreweryComments = async ({ - id, - pageNum, - pageSize, -}: { - id: string; - pageNum: number; - pageSize: number; -}) => { - const skip = (pageNum - 1) * pageSize; - const breweryComments: z.infer[] = - await DBClient.instance.breweryComment.findMany({ - skip, - take: pageSize, - where: { breweryPostId: id }, - select: { - id: true, - content: true, - rating: true, - createdAt: true, - updatedAt: true, - postedBy: { - select: { id: true, username: true, createdAt: true, userAvatar: true }, - }, - }, - orderBy: { createdAt: 'desc' }, - }); - return breweryComments; -}; - -export default getAllBreweryComments; diff --git a/src/services/comments/brewery-comment/getBreweryCommentById.ts b/src/services/comments/brewery-comment/getBreweryCommentById.ts deleted file mode 100644 index c3420a0..0000000 --- a/src/services/comments/brewery-comment/getBreweryCommentById.ts +++ /dev/null @@ -1,9 +0,0 @@ -import DBClient from '@/prisma/DBClient'; - -const getBreweryCommentById = async (id: string) => { - return DBClient.instance.breweryComment.findUnique({ - where: { id }, - }); -}; - -export default getBreweryCommentById; diff --git a/src/services/comments/brewery-comment/index.ts b/src/services/comments/brewery-comment/index.ts new file mode 100644 index 0000000..aecd5e0 --- /dev/null +++ b/src/services/comments/brewery-comment/index.ts @@ -0,0 +1,85 @@ +import DBClient from '@/prisma/DBClient'; +import { + CreateNewBreweryComment, + FindDeleteBreweryCommentById, + GetAllBreweryComments, + GetBreweryCommentCount, + UpdateBreweryCommentById, +} from './types'; + +const breweryCommentSelect = { + id: true, + content: true, + rating: true, + createdAt: true, + updatedAt: true, + postedBy: { + select: { id: true, username: true, createdAt: true, userAvatar: true }, + }, +} as const; + +export const updateBreweryCommentById: UpdateBreweryCommentById = ({ + breweryCommentId, + body, +}) => { + const { content, rating } = body; + + return DBClient.instance.breweryComment.update({ + where: { id: breweryCommentId }, + data: { content, rating }, + select: breweryCommentSelect, + }); +}; + +export const createNewBreweryComment: CreateNewBreweryComment = ({ + body, + breweryPostId, + userId, +}) => { + const { content, rating } = body; + return DBClient.instance.breweryComment.create({ + data: { + content, + rating, + breweryPost: { connect: { id: breweryPostId } }, + postedBy: { connect: { id: userId } }, + }, + select: breweryCommentSelect, + }); +}; + +export const getAllBreweryComments: GetAllBreweryComments = ({ + id, + pageNum, + pageSize, +}) => { + return DBClient.instance.breweryComment.findMany({ + skip: (pageNum - 1) * pageSize, + take: pageSize, + where: { breweryPostId: id }, + select: breweryCommentSelect, + orderBy: { createdAt: 'desc' }, + }); +}; + +export const getBreweryCommentById: FindDeleteBreweryCommentById = ({ + breweryCommentId, +}) => { + return DBClient.instance.breweryComment.findUnique({ + where: { id: breweryCommentId }, + select: breweryCommentSelect, + }); +}; + +export const deleteBreweryCommentByIdService: FindDeleteBreweryCommentById = ({ + breweryCommentId, +}) => { + return DBClient.instance.breweryComment.delete({ + where: { id: breweryCommentId }, + select: breweryCommentSelect, + }); +}; + +export const getBreweryCommentCount: GetBreweryCommentCount = ({ breweryPostId }) => { + return DBClient.instance.breweryComment.count({ where: { breweryPostId } }); +}; diff --git a/src/services/comments/brewery-comment/types/index.ts b/src/services/comments/brewery-comment/types/index.ts new file mode 100644 index 0000000..4e7d7bb --- /dev/null +++ b/src/services/comments/brewery-comment/types/index.ts @@ -0,0 +1,29 @@ +import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult'; +import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema'; + +import { z } from 'zod'; + +type BreweryComment = z.infer; + +export type UpdateBreweryCommentById = (args: { + breweryCommentId: string; + body: z.infer; +}) => Promise; + +export type CreateNewBreweryComment = (args: { + body: z.infer; + breweryPostId: string; + userId: string; +}) => Promise; + +export type GetAllBreweryComments = (args: { + id: string; + pageNum: number; + pageSize: number; +}) => Promise; + +export type FindDeleteBreweryCommentById = (args: { + breweryCommentId: string; +}) => Promise; + +export type GetBreweryCommentCount = (args: { breweryPostId: string }) => Promise;