Refactor and formatting

This commit is contained in:
Aaron William Po
2023-09-29 20:48:19 -04:00
parent 39980eb8c3
commit eb6dbb2115
23 changed files with 152 additions and 128 deletions

View File

@@ -28,13 +28,13 @@ const checkIfCommentOwner = async (
) => {
const { id } = req.query;
const user = req.user!;
const comment = await findBeerCommentById(id);
const comment = await findBeerCommentById({ beerCommentId: id });
if (!comment) {
throw new ServerError('Comment not found', 404);
}
if (comment.postedById !== user.id) {
if (comment.postedBy.id !== user.id) {
throw new ServerError('You are not authorized to modify this comment', 403);
}

View File

@@ -53,10 +53,11 @@ const getAll = async (
// eslint-disable-next-line @typescript-eslint/naming-convention
const { page_size, page_num } = req.query;
const comments = await getAllBeerComments(
{ id: beerPostId },
{ pageSize: parseInt(page_size, 10), pageNum: parseInt(page_num, 10) },
);
const comments = await getAllBeerComments({
beerPostId,
pageNum: parseInt(page_num, 10),
pageSize: parseInt(page_size, 10),
});
const pageCount = await DBClient.instance.beerComment.count({ where: { beerPostId } });

View File

@@ -37,7 +37,7 @@ const sendLikeRequest = async (
};
if (alreadyLiked) {
await removeBeerPostLikeById(alreadyLiked.id);
await removeBeerPostLikeById({ beerLikeId: alreadyLiked.id });
jsonResponse.message = 'Successfully unliked beer post';
} else {
await createBeerPostLike({ id, user });
@@ -53,7 +53,7 @@ const getLikeCount = async (
) => {
const id = req.query.id as string;
const likeCount = await getBeerPostLikeCount(id);
const likeCount = await getBeerPostLikeCount({ beerPostId: id });
res.status(200).json({
success: true,

View File

@@ -1,6 +1,7 @@
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import DBClient from '@/prisma/DBClient';
import getAllBeerPosts from '@/services/BeerPost/getAllBeerPosts';
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiRequest, NextApiResponse } from 'next';
@@ -8,10 +9,7 @@ import { createRouter } from 'next-connect';
import { z } from 'zod';
interface GetBeerPostsRequest extends NextApiRequest {
query: {
page_num: string;
page_size: string;
};
query: z.infer<typeof PaginatedQueryResponseSchema>;
}
const getBeerPosts = async (
@@ -41,10 +39,7 @@ const router = createRouter<
router.get(
validateRequest({
querySchema: z.object({
page_num: z.string().regex(/^\d+$/),
page_size: z.string().regex(/^\d+$/),
}),
querySchema: PaginatedQueryResponseSchema,
}),
getBeerPosts,
);

View File

@@ -1,6 +1,7 @@
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import DBClient from '@/prisma/DBClient';
import getAllBeerStyles from '@/services/BeerStyles/getAllBeerStyles';
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiRequest, NextApiResponse } from 'next';
@@ -8,7 +9,7 @@ import { createRouter } from 'next-connect';
import { z } from 'zod';
interface GetBeerStylesRequest extends NextApiRequest {
query: { page_num: string; page_size: string };
query: z.infer<typeof PaginatedQueryResponseSchema>;
}
const getBeerStyles = async (
@@ -18,7 +19,7 @@ const getBeerStyles = async (
const pageNum = parseInt(req.query.page_num, 10);
const pageSize = parseInt(req.query.page_size, 10);
const beerStyles = await getAllBeerStyles(pageNum, pageSize);
const beerStyles = await getAllBeerStyles({ pageNum, pageSize });
const beerStyleCount = await DBClient.instance.beerStyle.count();
res.setHeader('X-Total-Count', beerStyleCount);
@@ -38,10 +39,7 @@ const router = createRouter<
router.get(
validateRequest({
querySchema: z.object({
page_num: z.string().regex(/^\d+$/),
page_size: z.string().regex(/^\d+$/),
}),
querySchema: PaginatedQueryResponseSchema,
}),
getBeerStyles,
);

View File

@@ -1,6 +1,7 @@
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import DBClient from '@/prisma/DBClient';
import getAllBreweryPosts from '@/services/BreweryPost/getAllBreweryPosts';
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiRequest, NextApiResponse } from 'next';
@@ -8,10 +9,7 @@ import { createRouter } from 'next-connect';
import { z } from 'zod';
interface GetBreweryPostsRequest extends NextApiRequest {
query: {
page_num: string;
page_size: string;
};
query: z.infer<typeof PaginatedQueryResponseSchema>;
}
const getBreweryPosts = async (
@@ -25,7 +23,6 @@ const getBreweryPosts = async (
const breweryPostCount = await DBClient.instance.breweryPost.count();
res.setHeader('X-Total-Count', breweryPostCount);
res.status(200).json({
message: 'Brewery posts retrieved successfully',
statusCode: 200,
@@ -40,12 +37,7 @@ const router = createRouter<
>();
router.get(
validateRequest({
querySchema: z.object({
page_num: z.string().regex(/^\d+$/),
page_size: z.string().regex(/^\d+$/),
}),
}),
validateRequest({ querySchema: PaginatedQueryResponseSchema }),
getBreweryPosts,
);

View File

@@ -1,6 +1,7 @@
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import DBClient from '@/prisma/DBClient';
import BreweryPostMapQueryResult from '@/services/BreweryPost/schema/BreweryPostMapQueryResult';
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiRequest, NextApiResponse } from 'next';
@@ -8,10 +9,7 @@ import { createRouter } from 'next-connect';
import { z } from 'zod';
interface GetBreweryPostsRequest extends NextApiRequest {
query: {
page_num: string;
page_size: string;
};
query: z.infer<typeof PaginatedQueryResponseSchema>;
}
const getBreweryPosts = async (
@@ -28,12 +26,7 @@ const getBreweryPosts = async (
await DBClient.instance.breweryPost.findMany({
select: {
location: {
select: {
coordinates: true,
city: true,
country: true,
stateOrProvince: true,
},
select: { coordinates: true, city: true, country: true, stateOrProvince: true },
},
id: true,
name: true,
@@ -59,12 +52,7 @@ const router = createRouter<
>();
router.get(
validateRequest({
querySchema: z.object({
page_num: z.string().regex(/^\d+$/),
page_size: z.string().regex(/^\d+$/),
}),
}),
validateRequest({ querySchema: PaginatedQueryResponseSchema }),
getBreweryPosts,
);

View File

@@ -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,
},
});
};

View File

@@ -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({
where: { id },
data: {
const editBeerCommentById = async ({
id,
content,
rating,
updatedAt: new Date(),
}: EditBeerCommentByIdArgs): Promise<z.infer<typeof BeerCommentQueryResult>> => {
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 } },
},
});
return updated;
};
export default editBeerCommentById;

View File

@@ -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;

View File

@@ -1,18 +1,23 @@
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,
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: id },
where: { beerPostId },
orderBy: { createdAt: 'desc' },
select: {
id: true,
content: true,
@@ -20,9 +25,7 @@ const getAllBeerComments = async (
createdAt: true,
postedBy: { select: { id: true, username: true, createdAt: true } },
},
orderBy: { createdAt: 'desc' },
});
return beerComments;
};
export default getAllBeerComments;

View File

@@ -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;

View 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;

View File

@@ -0,0 +1,8 @@
import { z } from 'zod';
const CreateCommentValidationSchema = z.object({
userId: z.string().uuid(),
beerPostId: z.string().uuid(),
});
export default CreateCommentValidationSchema;

View File

@@ -19,6 +19,7 @@ const addBeerImageToDB = ({
userId,
}: ProcessImageDataArgs) => {
const beerImagePromises: Promise<BeerImage>[] = [];
files.forEach((file) => {
beerImagePromises.push(
DBClient.instance.beerImage.create({

View File

@@ -5,10 +5,6 @@ import EditBeerPostValidationSchema from './schema/EditBeerPostValidationSchema'
const schema = EditBeerPostValidationSchema.omit({ id: true });
export default async function editBeerPostById(id: string, data: z.infer<typeof schema>) {
const beerPost = await DBClient.instance.beerPost.update({
where: { id },
data,
});
const beerPost = await DBClient.instance.beerPost.update({ where: { id }, data });
return beerPost;
}

View File

@@ -16,6 +16,7 @@ const getBeerRecommendations = async ({
}: GetBeerRecommendationsArgs) => {
const skip = (pageNum - 1) * pageSize;
const take = pageSize;
const beerRecommendations: z.infer<typeof BeerPostQueryResult>[] =
await DBClient.instance.beerPost.findMany({
where: {

View File

@@ -13,6 +13,7 @@ const BeerPostQueryResult = z.object({
style: z.object({ id: z.string(), name: z.string(), description: z.string() }),
postedBy: z.object({ id: z.string(), username: z.string() }),
createdAt: z.coerce.date(),
updatedAt: z.coerce.date().optional(),
});
export default BeerPostQueryResult;

View File

@@ -2,19 +2,14 @@ import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import GetUserSchema from '../User/schema/GetUserSchema';
const createBeerPostLike = async ({
id,
user,
}: {
interface CreateBeerPostLikeArgs {
id: string;
user: z.infer<typeof GetUserSchema>;
}) => {
return DBClient.instance.beerPostLike.create({
data: {
beerPost: { connect: { id } },
likedBy: { connect: { id: user.id } },
},
}
const createBeerPostLike = async ({ id, user }: CreateBeerPostLikeArgs) =>
DBClient.instance.beerPostLike.create({
data: { beerPost: { connect: { id } }, likedBy: { connect: { id: user.id } } },
});
};
export default createBeerPostLike;

View File

@@ -1,11 +1,6 @@
import DBClient from '@/prisma/DBClient';
const getBeerPostLikeCount = async (beerPostId: string) => {
const count = await DBClient.instance.beerPostLike.count({
where: { beerPostId },
});
return count;
};
const getBeerPostLikeCount = async ({ beerPostId }: { beerPostId: string }) =>
DBClient.instance.beerPostLike.count({ where: { beerPostId } });
export default getBeerPostLikeCount;

View File

@@ -1,11 +1,10 @@
import DBClient from '@/prisma/DBClient';
const removeBeerPostLikeById = async (id: string) => {
return DBClient.instance.beerPostLike.delete({
where: {
id,
},
});
};
interface RemoveBeerPostLikeArgs {
beerLikeId: string;
}
const removeBeerPostLikeById = async ({ beerLikeId }: RemoveBeerPostLikeArgs) =>
DBClient.instance.beerPostLike.delete({ where: { id: beerLikeId } });
export default removeBeerPostLikeById;

View File

@@ -2,11 +2,14 @@ import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
const getAllBeerStyles = async (
pageNum: number,
pageSize: number,
): Promise<z.infer<typeof BeerStyleQueryResult>[]> => {
const styles = (await DBClient.instance.beerStyle.findMany({
const getAllBeerStyles = async ({
pageNum,
pageSize,
}: {
pageNum: number;
pageSize: number;
}): Promise<z.infer<typeof BeerStyleQueryResult>[]> =>
DBClient.instance.beerStyle.findMany({
take: pageSize,
skip: (pageNum - 1) * pageSize,
select: {
@@ -20,9 +23,6 @@ const getAllBeerStyles = async (
description: true,
glassware: { select: { id: true, name: true } },
},
})) as z.infer<typeof BeerStyleQueryResult>[];
return styles;
};
}) as ReturnType<typeof getAllBeerStyles>;
export default getAllBeerStyles;

View File

@@ -0,0 +1,8 @@
import { z } from 'zod';
const PaginatedQueryResponseSchema = z.object({
page_num: z.string(),
page_size: z.string(),
});
export default PaginatedQueryResponseSchema;