mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Refactor and formatting
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import CreateCommentValidationSchema from '../schema/CommentSchema/CreateCommentValidationSchema';
|
||||
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||
|
||||
const CreateNewBeerCommentServiceSchema = CreateCommentValidationSchema.extend({
|
||||
userId: z.string().cuid(),
|
||||
beerPostId: z.string().cuid(),
|
||||
});
|
||||
|
||||
type CreateNewBeerCommentArgs = z.infer<typeof CreateNewBeerCommentServiceSchema>;
|
||||
|
||||
const createNewBeerComment = async ({
|
||||
content,
|
||||
rating,
|
||||
beerPostId,
|
||||
userId,
|
||||
}: z.infer<typeof CreateNewBeerCommentServiceSchema>) => {
|
||||
}: CreateNewBeerCommentArgs): Promise<z.infer<typeof BeerCommentQueryResult>> => {
|
||||
return DBClient.instance.beerComment.create({
|
||||
data: {
|
||||
content,
|
||||
@@ -26,6 +29,7 @@ const createNewBeerComment = async ({
|
||||
rating: true,
|
||||
postedBy: { select: { id: true, username: true } },
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||
|
||||
interface EditBeerCommentByIdArgs {
|
||||
id: string;
|
||||
@@ -6,17 +8,23 @@ interface EditBeerCommentByIdArgs {
|
||||
rating: number;
|
||||
}
|
||||
|
||||
const editBeerCommentById = async ({ id, content, rating }: EditBeerCommentByIdArgs) => {
|
||||
const updated = await DBClient.instance.beerComment.update({
|
||||
const editBeerCommentById = async ({
|
||||
id,
|
||||
content,
|
||||
rating,
|
||||
}: EditBeerCommentByIdArgs): Promise<z.infer<typeof BeerCommentQueryResult>> => {
|
||||
return DBClient.instance.beerComment.update({
|
||||
where: { id },
|
||||
data: {
|
||||
content,
|
||||
rating,
|
||||
updatedAt: new Date(),
|
||||
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 } },
|
||||
},
|
||||
});
|
||||
|
||||
return updated;
|
||||
};
|
||||
|
||||
export default editBeerCommentById;
|
||||
|
||||
@@ -1,11 +1,25 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||
|
||||
const findBeerCommentById = async (id: string) => {
|
||||
const comment = await DBClient.instance.beerComment.findUnique({
|
||||
where: { id },
|
||||
interface FindBeerCommentArgs {
|
||||
beerCommentId: string;
|
||||
}
|
||||
|
||||
const findBeerCommentById = async ({
|
||||
beerCommentId,
|
||||
}: FindBeerCommentArgs): Promise<z.infer<typeof BeerCommentQueryResult> | 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 } },
|
||||
},
|
||||
});
|
||||
|
||||
return comment;
|
||||
};
|
||||
|
||||
export default findBeerCommentById;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
const getBeerCommentCount = async (beerPostId: string) => {
|
||||
const count = await DBClient.instance.beerComment.count({
|
||||
where: { beerPostId },
|
||||
});
|
||||
interface GetBeerCommentCountArgs {
|
||||
beerPostId: string;
|
||||
}
|
||||
|
||||
return count;
|
||||
const getBeerCommentCount = async ({
|
||||
beerPostId,
|
||||
}: GetBeerCommentCountArgs): Promise<number> => {
|
||||
return DBClient.instance.beerComment.count({ where: { beerPostId } });
|
||||
};
|
||||
|
||||
export default getBeerCommentCount;
|
||||
|
||||
15
src/services/BeerComment/schema/BeerCommentQueryResult.ts
Normal file
15
src/services/BeerComment/schema/BeerCommentQueryResult.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const BeerCommentQueryResult = z.object({
|
||||
id: z.string().cuid(),
|
||||
content: z.string(),
|
||||
rating: z.number(),
|
||||
postedBy: z.object({
|
||||
id: z.string().cuid(),
|
||||
username: z.string(),
|
||||
}),
|
||||
createdAt: z.coerce.date(),
|
||||
updatedAt: z.coerce.date().nullable(),
|
||||
});
|
||||
|
||||
export default BeerCommentQueryResult;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const CreateCommentValidationSchema = z.object({
|
||||
userId: z.string().uuid(),
|
||||
beerPostId: z.string().uuid(),
|
||||
});
|
||||
|
||||
export default CreateCommentValidationSchema;
|
||||
Reference in New Issue
Block a user