feat: add beer style comments

This commit is contained in:
Aaron William Po
2023-10-15 20:24:40 -04:00
parent 27af922a91
commit c8e8207e30
16 changed files with 774 additions and 26 deletions

View File

@@ -0,0 +1,37 @@
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<typeof CreateNewBeerStyleCommentServiceSchema>;
const createNewBeerStyleComment = async ({
content,
rating,
userId,
beerStyleId,
}: CreateNewBeerCommentArgs): Promise<z.infer<typeof CommentQueryResult>> => {
return DBClient.instance.beerStyleComment.create({
data: {
content,
rating,
beerStyle: { connect: { id: beerStyleId } },
postedBy: { connect: { id: userId } },
},
select: {
id: true,
content: true,
rating: true,
postedBy: { select: { id: true, username: true } },
createdAt: true,
updatedAt: true,
},
});
};
export default createNewBeerStyleComment;

View File

@@ -0,0 +1,32 @@
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<z.infer<typeof CommentQueryResult>[]> => {
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 } },
},
});
};
export default getAllBeerStyleComments;

View File

@@ -0,0 +1,15 @@
import DBClient from '@/prisma/DBClient';
interface GetBeerStyleCommentCountArgs {
beerStyleId: string;
}
const getBeerCommentCount = async ({
beerStyleId,
}: GetBeerStyleCommentCountArgs): Promise<number> => {
return DBClient.instance.beerStyleComment.count({
where: { beerStyleId },
});
};
export default getBeerCommentCount;