Refactored api services into sep files. Client fix

Fixed hydration errors in beers/[id] by implementing timeDistanceState
This commit is contained in:
Aaron William Po
2023-01-31 22:38:13 -05:00
parent 0b96c8f1f5
commit 5cf2087cd1
29 changed files with 380 additions and 430 deletions

View File

@@ -0,0 +1,28 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerCommentValidationSchema from './schema/CreateBeerCommentValidationSchema';
const createNewBeerComment = async ({
content,
rating,
beerPostId,
}: z.infer<typeof BeerCommentValidationSchema>) => {
const user = await DBClient.instance.user.findFirstOrThrow();
return DBClient.instance.beerComment.create({
data: {
content,
rating,
beerPost: { connect: { id: beerPostId } },
postedBy: { connect: { id: user.id } },
},
select: {
id: true,
content: true,
rating: true,
postedBy: { select: { id: true, username: true } },
createdAt: true,
},
});
};
export default createNewBeerComment;

View File

@@ -0,0 +1,37 @@
import DBClient from '@/prisma/DBClient';
import BeerPostQueryResult from '../BeerPost/schema/BeerPostQueryResult';
import { BeerCommentQueryResultArrayT } from './schema/BeerCommentQueryResult';
const getAllBeerComments = async (
{ id }: Pick<BeerPostQueryResult, 'id'>,
{ pageSize, pageNum = 0 }: { pageSize: number; pageNum?: number },
) => {
const skip = (pageNum - 1) * pageSize;
const beerComments: BeerCommentQueryResultArrayT =
await DBClient.instance.beerComment.findMany({
where: {
beerPostId: id,
},
select: {
id: true,
content: true,
rating: true,
createdAt: true,
postedBy: {
select: {
id: true,
username: true,
createdAt: true,
},
},
},
orderBy: {
createdAt: 'desc',
},
skip,
take: pageSize,
});
return beerComments;
};
export default getAllBeerComments;

View File

@@ -0,0 +1,15 @@
import { z } from 'zod';
export const BeerCommentQueryResult = z.object({
id: z.string().uuid(),
content: z.string().min(1).max(300),
rating: z.number().int().min(1).max(5),
createdAt: z.date().or(z.string().datetime()),
postedBy: z.object({
id: z.string().uuid(),
username: z.string().min(1).max(50),
}),
});
export const BeerCommentQueryResultArray = z.array(BeerCommentQueryResult);
export type BeerCommentQueryResultT = z.infer<typeof BeerCommentQueryResult>;
export type BeerCommentQueryResultArrayT = z.infer<typeof BeerCommentQueryResultArray>;

View File

@@ -0,0 +1,22 @@
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.' }),
beerPostId: z.string().uuid({
message: 'Beer post ID must be a valid UUID.',
}),
});
export default BeerCommentValidationSchema;