mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
feat: add beer style comments
This commit is contained in:
37
src/services/BeerStyleComment/createNewBeerStyleComment.ts
Normal file
37
src/services/BeerStyleComment/createNewBeerStyleComment.ts
Normal 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;
|
||||
32
src/services/BeerStyleComment/getAllBeerStyleComments.ts
Normal file
32
src/services/BeerStyleComment/getAllBeerStyleComments.ts
Normal 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;
|
||||
15
src/services/BeerStyleComment/getBeerStyleCommentCount.ts
Normal file
15
src/services/BeerStyleComment/getBeerStyleCommentCount.ts
Normal 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;
|
||||
Reference in New Issue
Block a user