Refactor and formatting

This commit is contained in:
Aaron William Po
2023-09-29 20:48:19 -04:00
parent 39980eb8c3
commit eb6dbb2115
23 changed files with 152 additions and 128 deletions

View File

@@ -1,28 +1,31 @@
import DBClient from '@/prisma/DBClient';
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import { z } from 'zod';
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
const getAllBeerComments = async (
{ id }: Pick<z.infer<typeof BeerPostQueryResult>, 'id'>,
{ pageSize, pageNum = 0 }: { pageSize: number; pageNum?: number },
) => {
const skip = (pageNum - 1) * pageSize;
const beerComments: z.infer<typeof CommentQueryResult>[] =
await DBClient.instance.beerComment.findMany({
skip,
take: pageSize,
where: { beerPostId: id },
select: {
id: true,
content: true,
rating: true,
createdAt: true,
postedBy: { select: { id: true, username: true, createdAt: true } },
},
orderBy: { createdAt: 'desc' },
});
return beerComments;
interface GetAllBeerCommentsArgs {
beerPostId: string;
pageNum: number;
pageSize: number;
}
const getAllBeerComments = async ({
beerPostId,
pageNum,
pageSize,
}: GetAllBeerCommentsArgs): Promise<z.infer<typeof CommentQueryResult>[]> => {
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,
postedBy: { select: { id: true, username: true, createdAt: true } },
},
});
};
export default getAllBeerComments;