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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 } });
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user