mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Refactor: update types for brewery comments
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import CreateCommentValidationSchema from '../schema/CommentSchema/CreateCommentValidationSchema';
|
||||
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
|
||||
|
||||
const CreateNewBeerCommentServiceSchema = CreateCommentValidationSchema.extend({
|
||||
userId: z.string().cuid(),
|
||||
@@ -15,7 +15,7 @@ const createNewBeerComment = async ({
|
||||
rating,
|
||||
beerPostId,
|
||||
userId,
|
||||
}: CreateNewBeerCommentArgs): Promise<z.infer<typeof BeerCommentQueryResult>> => {
|
||||
}: CreateNewBeerCommentArgs): Promise<z.infer<typeof CommentQueryResult>> => {
|
||||
return DBClient.instance.beerComment.create({
|
||||
data: {
|
||||
content,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
|
||||
|
||||
interface EditBeerCommentByIdArgs {
|
||||
id: string;
|
||||
@@ -12,7 +12,7 @@ const editBeerCommentById = async ({
|
||||
id,
|
||||
content,
|
||||
rating,
|
||||
}: EditBeerCommentByIdArgs): Promise<z.infer<typeof BeerCommentQueryResult>> => {
|
||||
}: EditBeerCommentByIdArgs): Promise<z.infer<typeof CommentQueryResult>> => {
|
||||
return DBClient.instance.beerComment.update({
|
||||
where: { id },
|
||||
data: { content, rating, updatedAt: new Date() },
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
|
||||
|
||||
interface FindBeerCommentArgs {
|
||||
beerCommentId: string;
|
||||
@@ -8,7 +8,7 @@ interface FindBeerCommentArgs {
|
||||
|
||||
const findBeerCommentById = async ({
|
||||
beerCommentId,
|
||||
}: FindBeerCommentArgs): Promise<z.infer<typeof BeerCommentQueryResult> | null> => {
|
||||
}: FindBeerCommentArgs): Promise<z.infer<typeof CommentQueryResult> | null> => {
|
||||
return DBClient.instance.beerComment.findUnique({
|
||||
where: { id: beerCommentId },
|
||||
select: {
|
||||
|
||||
@@ -23,6 +23,7 @@ const getAllBeerComments = async ({
|
||||
content: true,
|
||||
rating: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
postedBy: { select: { id: true, username: true, createdAt: true } },
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
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;
|
||||
@@ -1,6 +1,7 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import CreateCommentValidationSchema from '../schema/CommentSchema/CreateCommentValidationSchema';
|
||||
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
|
||||
|
||||
const CreateNewBreweryCommentServiceSchema = CreateCommentValidationSchema.extend({
|
||||
userId: z.string().cuid(),
|
||||
@@ -12,7 +13,9 @@ const createNewBreweryComment = async ({
|
||||
rating,
|
||||
breweryPostId,
|
||||
userId,
|
||||
}: z.infer<typeof CreateNewBreweryCommentServiceSchema>) => {
|
||||
}: z.infer<typeof CreateNewBreweryCommentServiceSchema>): Promise<
|
||||
z.infer<typeof CommentQueryResult>
|
||||
> => {
|
||||
return DBClient.instance.breweryComment.create({
|
||||
data: {
|
||||
content,
|
||||
@@ -26,6 +29,7 @@ const createNewBreweryComment = async ({
|
||||
rating: true,
|
||||
postedBy: { select: { id: true, username: true } },
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||
import { z } from 'zod';
|
||||
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
|
||||
|
||||
const getAllBreweryComments = async (
|
||||
{ id }: Pick<z.infer<typeof BeerPostQueryResult>, 'id'>,
|
||||
{ pageSize, pageNum = 0 }: { pageSize: number; pageNum?: number },
|
||||
) => {
|
||||
const getAllBreweryComments = async ({
|
||||
id,
|
||||
pageNum,
|
||||
pageSize,
|
||||
}: {
|
||||
id: string;
|
||||
pageNum: number;
|
||||
pageSize: number;
|
||||
}) => {
|
||||
const skip = (pageNum - 1) * pageSize;
|
||||
const breweryComments: z.infer<typeof CommentQueryResult>[] =
|
||||
await DBClient.instance.breweryComment.findMany({
|
||||
@@ -18,6 +22,7 @@ const getAllBreweryComments = async (
|
||||
content: true,
|
||||
rating: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
postedBy: { select: { id: true, username: true, createdAt: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
|
||||
9
src/services/BreweryComment/getBreweryCommentById.ts
Normal file
9
src/services/BreweryComment/getBreweryCommentById.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
const getBreweryCommentById = async (id: string) => {
|
||||
return DBClient.instance.breweryComment.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
};
|
||||
|
||||
export default getBreweryCommentById;
|
||||
@@ -9,6 +9,7 @@ const CommentQueryResult = z.object({
|
||||
id: z.string().cuid(),
|
||||
username: z.string().min(1).max(50),
|
||||
}),
|
||||
updatedAt: z.coerce.date().nullable(),
|
||||
});
|
||||
|
||||
export default CommentQueryResult;
|
||||
|
||||
Reference in New Issue
Block a user