refactor: update comments services

This commit is contained in:
Aaron William Po
2023-12-11 18:17:41 -05:00
parent 8b0d182cb3
commit 80404802dc
22 changed files with 406 additions and 413 deletions

View File

@@ -1,37 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import CreateCommentValidationSchema from '../../schema/CommentSchema/CreateCommentValidationSchema';
import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult';
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,
}: CreateNewBeerCommentArgs): Promise<z.infer<typeof CommentQueryResult>> => {
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, userAvatar: true } },
createdAt: true,
updatedAt: true,
},
});
};
export default createNewBeerComment;

View File

@@ -1,32 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult';
interface EditBeerCommentByIdArgs {
id: string;
content: string;
rating: number;
}
const editBeerCommentById = async ({
id,
content,
rating,
}: EditBeerCommentByIdArgs): Promise<z.infer<typeof CommentQueryResult>> => {
return DBClient.instance.beerComment.update({
where: { id },
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, userAvatar: true },
},
},
});
};
export default editBeerCommentById;

View File

@@ -1,27 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult';
interface FindBeerCommentArgs {
beerCommentId: string;
}
const findBeerCommentById = async ({
beerCommentId,
}: FindBeerCommentArgs): Promise<z.infer<typeof CommentQueryResult> | 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, userAvatar: true },
},
},
});
};
export default findBeerCommentById;

View File

@@ -1,34 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult';
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,
updatedAt: true,
postedBy: {
select: { id: true, username: true, createdAt: true, userAvatar: true },
},
},
});
};
export default getAllBeerComments;

View File

@@ -1,13 +0,0 @@
import DBClient from '@/prisma/DBClient';
interface GetBeerCommentCountArgs {
beerPostId: string;
}
const getBeerCommentCount = async ({
beerPostId,
}: GetBeerCommentCountArgs): Promise<number> => {
return DBClient.instance.beerComment.count({ where: { beerPostId } });
};
export default getBeerCommentCount;

View File

@@ -0,0 +1,87 @@
import DBClient from '@/prisma/DBClient';
import {
CreateBeerPostComment,
EditBeerPostCommentById,
FindOrDeleteBeerPostCommentById,
GetAllBeerPostComments,
GetBeerPostCommentCount,
} from './types';
const beerPostCommentSelect = {
id: true,
content: true,
rating: true,
createdAt: true,
updatedAt: true,
postedBy: {
select: { id: true, username: true, createdAt: true, userAvatar: true },
},
} as const;
export const createBeerPostCommentService: CreateBeerPostComment = ({
body,
beerPostId,
userId,
}) => {
const { content, rating } = body;
return DBClient.instance.beerComment.create({
data: {
content,
rating,
beerPost: { connect: { id: beerPostId } },
postedBy: { connect: { id: userId } },
},
select: beerPostCommentSelect,
});
};
export const editBeerPostCommentByIdService: EditBeerPostCommentById = ({
beerPostCommentId,
body,
}) => {
const { content, rating } = body;
return DBClient.instance.beerComment.update({
where: { id: beerPostCommentId },
data: { content, rating, updatedAt: new Date() },
select: beerPostCommentSelect,
});
};
export const getBeerPostCommentByIdService: FindOrDeleteBeerPostCommentById = ({
beerPostCommentId,
}) => {
return DBClient.instance.beerComment.findUnique({
where: { id: beerPostCommentId },
select: beerPostCommentSelect,
});
};
export const deleteBeerCommentByIdService: FindOrDeleteBeerPostCommentById = ({
beerPostCommentId,
}) => {
return DBClient.instance.beerComment.delete({
where: { id: beerPostCommentId },
select: beerPostCommentSelect,
});
};
export const getAllBeerCommentsService: GetAllBeerPostComments = ({
beerPostId,
pageNum,
pageSize,
}) => {
return DBClient.instance.beerComment.findMany({
skip: (pageNum - 1) * pageSize,
take: pageSize,
where: { beerPostId },
orderBy: { createdAt: 'desc' },
select: beerPostCommentSelect,
});
};
export const getBeerPostCommentCountService: GetBeerPostCommentCount = ({
beerPostId,
}) => {
return DBClient.instance.beerComment.count({ where: { beerPostId } });
};

View File

@@ -0,0 +1,28 @@
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import { z } from 'zod';
type BeerPostComment = z.infer<typeof CommentQueryResult>;
export type CreateBeerPostComment = (args: {
body: z.infer<typeof CreateCommentValidationSchema>;
userId: string;
beerPostId: string;
}) => Promise<BeerPostComment>;
export type EditBeerPostCommentById = (args: {
body: z.infer<typeof CreateCommentValidationSchema>;
beerPostCommentId: string;
}) => Promise<BeerPostComment>;
export type FindOrDeleteBeerPostCommentById = (args: {
beerPostCommentId: string;
}) => Promise<BeerPostComment | null>;
export type GetBeerPostCommentCount = (args: { beerPostId: string }) => Promise<number>;
export type GetAllBeerPostComments = (args: {
beerPostId: string;
pageNum: number;
pageSize: number;
}) => Promise<BeerPostComment[]>;

View File

@@ -1,39 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import CreateCommentValidationSchema from '../../schema/CommentSchema/CreateCommentValidationSchema';
import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult';
const CreateNewBeerStyleCommentServiceSchema = CreateCommentValidationSchema.extend({
userId: z.string().cuid(),
beerStyleId: z.string().cuid(),
});
type CreateNewBeerCommentArgs = z.infer<typeof CreateNewBeerStyleCommentServiceSchema>;
const createNewBeerStyleComment = async ({
content,
rating,
userId,
beerStyleId,
}: CreateNewBeerCommentArgs): Promise<z.infer<typeof CommentQueryResult>> => {
return DBClient.instance.beerStyleComment.create({
data: {
content,
rating,
beerStyle: { connect: { id: beerStyleId } },
postedBy: { connect: { id: userId } },
},
select: {
id: true,
content: true,
rating: true,
createdAt: true,
updatedAt: true,
postedBy: {
select: { id: true, username: true, createdAt: true, userAvatar: true },
},
},
});
};
export default createNewBeerStyleComment;

View File

@@ -1,34 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult';
interface GetAllBeerStyleCommentArgs {
beerStyleId: string;
pageNum: number;
pageSize: number;
}
const getAllBeerStyleComments = async ({
beerStyleId,
pageNum,
pageSize,
}: GetAllBeerStyleCommentArgs): Promise<z.infer<typeof CommentQueryResult>[]> => {
return DBClient.instance.beerStyleComment.findMany({
skip: (pageNum - 1) * pageSize,
take: pageSize,
where: { beerStyleId },
orderBy: { createdAt: 'desc' },
select: {
id: true,
content: true,
rating: true,
createdAt: true,
updatedAt: true,
postedBy: {
select: { id: true, username: true, createdAt: true, userAvatar: true },
},
},
});
};
export default getAllBeerStyleComments;

View File

@@ -1,15 +0,0 @@
import DBClient from '@/prisma/DBClient';
interface GetBeerStyleCommentCountArgs {
beerStyleId: string;
}
const getBeerCommentCount = async ({
beerStyleId,
}: GetBeerStyleCommentCountArgs): Promise<number> => {
return DBClient.instance.beerStyleComment.count({
where: { beerStyleId },
});
};
export default getBeerCommentCount;

View File

@@ -0,0 +1,85 @@
import DBClient from '@/prisma/DBClient';
import {
CreateNewBeerStyleComment,
GetAllBeerStyleComments,
GetBeerStyleCommentCount,
UpdateBeerStyleCommentById,
FindOrDeleteBeerStyleCommentById,
} from './types';
const beerStyleCommentSelect = {
id: true,
content: true,
rating: true,
createdAt: true,
updatedAt: true,
postedBy: {
select: { id: true, username: true, createdAt: true, userAvatar: true },
},
} as const;
export const createNewBeerStyleComment: CreateNewBeerStyleComment = ({
body,
userId,
beerStyleId,
}) => {
const { content, rating } = body;
return DBClient.instance.beerStyleComment.create({
data: {
content,
rating,
beerStyle: { connect: { id: beerStyleId } },
postedBy: { connect: { id: userId } },
},
select: beerStyleCommentSelect,
});
};
export const getAllBeerStyleComments: GetAllBeerStyleComments = ({
beerStyleId,
pageNum,
pageSize,
}) => {
return DBClient.instance.beerStyleComment.findMany({
skip: (pageNum - 1) * pageSize,
take: pageSize,
where: { beerStyleId },
orderBy: { createdAt: 'desc' },
select: beerStyleCommentSelect,
});
};
export const getBeerStyleCommentCount: GetBeerStyleCommentCount = ({ beerStyleId }) => {
return DBClient.instance.beerStyleComment.count({ where: { beerStyleId } });
};
export const updateBeerStyleCommentById: UpdateBeerStyleCommentById = ({
body,
beerStyleCommentId,
}) => {
const { content, rating } = body;
return DBClient.instance.beerStyleComment.update({
where: { id: beerStyleCommentId },
data: { content, rating, updatedAt: new Date() },
select: beerStyleCommentSelect,
});
};
export const findBeerStyleCommentById: FindOrDeleteBeerStyleCommentById = ({
beerStyleCommentId,
}) => {
return DBClient.instance.beerStyleComment.findUnique({
where: { id: beerStyleCommentId },
select: beerStyleCommentSelect,
});
};
export const deleteBeerStyleCommentById: FindOrDeleteBeerStyleCommentById = ({
beerStyleCommentId,
}) => {
return DBClient.instance.beerStyleComment.delete({
where: { id: beerStyleCommentId },
select: beerStyleCommentSelect,
});
};

View File

@@ -0,0 +1,28 @@
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import { z } from 'zod';
type BeerStyleComment = z.infer<typeof CommentQueryResult>;
export type FindOrDeleteBeerStyleCommentById = (args: {
beerStyleCommentId: string;
}) => Promise<BeerStyleComment | null>;
export type UpdateBeerStyleCommentById = (args: {
body: z.infer<typeof CreateCommentValidationSchema>;
beerStyleCommentId: string;
}) => Promise<BeerStyleComment>;
export type GetBeerStyleCommentCount = (args: { beerStyleId: string }) => Promise<number>;
export type GetAllBeerStyleComments = (args: {
beerStyleId: string;
pageNum: number;
pageSize: number;
}) => Promise<BeerStyleComment[]>;
export type CreateNewBeerStyleComment = (args: {
body: z.infer<typeof CreateCommentValidationSchema>;
userId: string;
beerStyleId: string;
}) => Promise<BeerStyleComment>;

View File

@@ -1,23 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import CreateCommentValidationSchema from '../../schema/CommentSchema/CreateCommentValidationSchema';
interface UpdateBeerStyleCommentByIdParams {
body: z.infer<typeof CreateCommentValidationSchema>;
id: string;
}
const updateBeerStyleCommentById = ({ body, id }: UpdateBeerStyleCommentByIdParams) => {
const { content, rating } = body;
return DBClient.instance.beerStyleComment.update({
where: { id },
data: {
content,
rating,
updatedAt: new Date(),
},
});
};
export default updateBeerStyleCommentById;

View File

@@ -1,39 +0,0 @@
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(),
breweryPostId: z.string().cuid(),
});
const createNewBreweryComment = async ({
content,
rating,
breweryPostId,
userId,
}: z.infer<typeof CreateNewBreweryCommentServiceSchema>): Promise<
z.infer<typeof CommentQueryResult>
> => {
return DBClient.instance.breweryComment.create({
data: {
content,
rating,
breweryPost: { connect: { id: breweryPostId } },
postedBy: { connect: { id: userId } },
},
select: {
id: true,
content: true,
rating: true,
createdAt: true,
updatedAt: true,
postedBy: {
select: { id: true, username: true, createdAt: true, userAvatar: true },
},
},
});
};
export default createNewBreweryComment;

View File

@@ -1,35 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import CommentQueryResult from '../../schema/CommentSchema/CommentQueryResult';
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({
skip,
take: pageSize,
where: { breweryPostId: id },
select: {
id: true,
content: true,
rating: true,
createdAt: true,
updatedAt: true,
postedBy: {
select: { id: true, username: true, createdAt: true, userAvatar: true },
},
},
orderBy: { createdAt: 'desc' },
});
return breweryComments;
};
export default getAllBreweryComments;

View File

@@ -1,9 +0,0 @@
import DBClient from '@/prisma/DBClient';
const getBreweryCommentById = async (id: string) => {
return DBClient.instance.breweryComment.findUnique({
where: { id },
});
};
export default getBreweryCommentById;

View File

@@ -0,0 +1,85 @@
import DBClient from '@/prisma/DBClient';
import {
CreateNewBreweryComment,
FindDeleteBreweryCommentById,
GetAllBreweryComments,
GetBreweryCommentCount,
UpdateBreweryCommentById,
} from './types';
const breweryCommentSelect = {
id: true,
content: true,
rating: true,
createdAt: true,
updatedAt: true,
postedBy: {
select: { id: true, username: true, createdAt: true, userAvatar: true },
},
} as const;
export const updateBreweryCommentById: UpdateBreweryCommentById = ({
breweryCommentId,
body,
}) => {
const { content, rating } = body;
return DBClient.instance.breweryComment.update({
where: { id: breweryCommentId },
data: { content, rating },
select: breweryCommentSelect,
});
};
export const createNewBreweryComment: CreateNewBreweryComment = ({
body,
breweryPostId,
userId,
}) => {
const { content, rating } = body;
return DBClient.instance.breweryComment.create({
data: {
content,
rating,
breweryPost: { connect: { id: breweryPostId } },
postedBy: { connect: { id: userId } },
},
select: breweryCommentSelect,
});
};
export const getAllBreweryComments: GetAllBreweryComments = ({
id,
pageNum,
pageSize,
}) => {
return DBClient.instance.breweryComment.findMany({
skip: (pageNum - 1) * pageSize,
take: pageSize,
where: { breweryPostId: id },
select: breweryCommentSelect,
orderBy: { createdAt: 'desc' },
});
};
export const getBreweryCommentById: FindDeleteBreweryCommentById = ({
breweryCommentId,
}) => {
return DBClient.instance.breweryComment.findUnique({
where: { id: breweryCommentId },
select: breweryCommentSelect,
});
};
export const deleteBreweryCommentByIdService: FindDeleteBreweryCommentById = ({
breweryCommentId,
}) => {
return DBClient.instance.breweryComment.delete({
where: { id: breweryCommentId },
select: breweryCommentSelect,
});
};
export const getBreweryCommentCount: GetBreweryCommentCount = ({ breweryPostId }) => {
return DBClient.instance.breweryComment.count({ where: { breweryPostId } });
};

View File

@@ -0,0 +1,29 @@
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import { z } from 'zod';
type BreweryComment = z.infer<typeof CommentQueryResult>;
export type UpdateBreweryCommentById = (args: {
breweryCommentId: string;
body: z.infer<typeof CreateCommentValidationSchema>;
}) => Promise<BreweryComment>;
export type CreateNewBreweryComment = (args: {
body: z.infer<typeof CreateCommentValidationSchema>;
breweryPostId: string;
userId: string;
}) => Promise<BreweryComment>;
export type GetAllBreweryComments = (args: {
id: string;
pageNum: number;
pageSize: number;
}) => Promise<BreweryComment[]>;
export type FindDeleteBreweryCommentById = (args: {
breweryCommentId: string;
}) => Promise<BreweryComment | null>;
export type GetBreweryCommentCount = (args: { breweryPostId: string }) => Promise<number>;