mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Restructure codebase to use src directory
This commit is contained in:
33
src/services/BeerComment/createNewBeerComment.ts
Normal file
33
src/services/BeerComment/createNewBeerComment.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import BeerCommentValidationSchema from './schema/CreateBeerCommentValidationSchema';
|
||||
|
||||
const CreateNewBeerCommentServiceSchema = BeerCommentValidationSchema.extend({
|
||||
userId: z.string().uuid(),
|
||||
beerPostId: z.string().uuid(),
|
||||
});
|
||||
|
||||
const createNewBeerComment = async ({
|
||||
content,
|
||||
rating,
|
||||
beerPostId,
|
||||
userId,
|
||||
}: z.infer<typeof CreateNewBeerCommentServiceSchema>) => {
|
||||
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 } },
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default createNewBeerComment;
|
||||
28
src/services/BeerComment/getAllBeerComments.ts
Normal file
28
src/services/BeerComment/getAllBeerComments.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||
import { z } from 'zod';
|
||||
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||
|
||||
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 BeerCommentQueryResult>[] =
|
||||
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;
|
||||
};
|
||||
|
||||
export default getAllBeerComments;
|
||||
11
src/services/BeerComment/getBeerCommentCount.ts
Normal file
11
src/services/BeerComment/getBeerCommentCount.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
const getBeerCommentCount = async (beerPostId: string) => {
|
||||
const count = await DBClient.instance.beerComment.count({
|
||||
where: { beerPostId },
|
||||
});
|
||||
|
||||
return count;
|
||||
};
|
||||
|
||||
export default getBeerCommentCount;
|
||||
14
src/services/BeerComment/schema/BeerCommentQueryResult.ts
Normal file
14
src/services/BeerComment/schema/BeerCommentQueryResult.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const BeerCommentQueryResult = z.object({
|
||||
id: z.string().uuid(),
|
||||
content: z.string().min(1).max(500),
|
||||
rating: z.number().int().min(1).max(5),
|
||||
createdAt: z.coerce.date(),
|
||||
postedBy: z.object({
|
||||
id: z.string().uuid(),
|
||||
username: z.string().min(1).max(50),
|
||||
}),
|
||||
});
|
||||
|
||||
export default BeerCommentQueryResult;
|
||||
@@ -0,0 +1,15 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const BeerCommentValidationSchema = z.object({
|
||||
content: z
|
||||
.string()
|
||||
.min(1, { message: 'Comment must not be empty.' })
|
||||
.max(300, { message: 'Comment must be less than 300 characters.' }),
|
||||
rating: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1, { message: 'Rating must be greater than 1.' })
|
||||
.max(5, { message: 'Rating must be less than 5.' }),
|
||||
});
|
||||
|
||||
export default BeerCommentValidationSchema;
|
||||
Reference in New Issue
Block a user