mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
refactor: finish extracting comment controller logic out of router
This commit is contained in:
@@ -6,7 +6,13 @@ import APIResponseValidationSchema from '@/validation/APIResponseValidationSchem
|
|||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { NextHandler } from 'next-connect';
|
import { NextHandler } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { CommentRequest, EditCommentRequest } from '../requestTypes';
|
import createNewBeerComment from '@/services/BeerComment/createNewBeerComment';
|
||||||
|
import getAllBeerComments from '@/services/BeerComment/getAllBeerComments';
|
||||||
|
import {
|
||||||
|
CommentRequest,
|
||||||
|
EditAndCreateCommentRequest,
|
||||||
|
GetAllCommentsRequest,
|
||||||
|
} from '../types';
|
||||||
|
|
||||||
export const checkIfBeerCommentOwner = async <T extends CommentRequest>(
|
export const checkIfBeerCommentOwner = async <T extends CommentRequest>(
|
||||||
req: T,
|
req: T,
|
||||||
@@ -29,7 +35,7 @@ export const checkIfBeerCommentOwner = async <T extends CommentRequest>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const editBeerPostComment = async (
|
export const editBeerPostComment = async (
|
||||||
req: EditCommentRequest,
|
req: EditAndCreateCommentRequest,
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
) => {
|
) => {
|
||||||
const { id } = req.query;
|
const { id } = req.query;
|
||||||
@@ -64,3 +70,52 @@ export const deleteBeerPostComment = async (
|
|||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const createBeerPostComment = async (
|
||||||
|
req: EditAndCreateCommentRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const { content, rating } = req.body;
|
||||||
|
|
||||||
|
const beerPostId = req.query.id;
|
||||||
|
|
||||||
|
const newBeerComment = await createNewBeerComment({
|
||||||
|
content,
|
||||||
|
rating,
|
||||||
|
beerPostId,
|
||||||
|
userId: req.user!.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(201).json({
|
||||||
|
message: 'Beer comment created successfully',
|
||||||
|
statusCode: 201,
|
||||||
|
payload: newBeerComment,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAllBeerPostComments = async (
|
||||||
|
req: GetAllCommentsRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const beerPostId = req.query.id;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
|
const { page_size, page_num } = req.query;
|
||||||
|
|
||||||
|
const comments = await getAllBeerComments({
|
||||||
|
beerPostId,
|
||||||
|
pageNum: parseInt(page_num, 10),
|
||||||
|
pageSize: parseInt(page_size, 10),
|
||||||
|
});
|
||||||
|
|
||||||
|
const count = await DBClient.instance.beerComment.count({ where: { beerPostId } });
|
||||||
|
|
||||||
|
res.setHeader('X-Total-Count', count);
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Beer comments fetched successfully',
|
||||||
|
statusCode: 200,
|
||||||
|
payload: comments,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -5,7 +5,16 @@ import APIResponseValidationSchema from '@/validation/APIResponseValidationSchem
|
|||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { NextHandler } from 'next-connect';
|
import { NextHandler } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { CommentRequest, EditCommentRequest } from '../requestTypes';
|
|
||||||
|
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
|
||||||
|
import createNewBeerStyleComment from '@/services/BeerStyleComment/createNewBeerStyleComment';
|
||||||
|
import getAllBeerStyleComments from '@/services/BeerStyleComment/getAllBeerStyleComments';
|
||||||
|
|
||||||
|
import {
|
||||||
|
CommentRequest,
|
||||||
|
EditAndCreateCommentRequest,
|
||||||
|
GetAllCommentsRequest,
|
||||||
|
} from '../types';
|
||||||
|
|
||||||
export const checkIfBeerStyleCommentOwner = async <
|
export const checkIfBeerStyleCommentOwner = async <
|
||||||
CommentRequestType extends CommentRequest,
|
CommentRequestType extends CommentRequest,
|
||||||
@@ -35,7 +44,7 @@ export const checkIfBeerStyleCommentOwner = async <
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const editBeerStyleComment = async (
|
export const editBeerStyleComment = async (
|
||||||
req: EditCommentRequest,
|
req: EditAndCreateCommentRequest,
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
) => {
|
) => {
|
||||||
const updated = await updateBeerStyleCommentById({
|
const updated = await updateBeerStyleCommentById({
|
||||||
@@ -65,3 +74,53 @@ export const deleteBeerStyleComment = async (
|
|||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const createComment = async (
|
||||||
|
req: EditAndCreateCommentRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const { content, rating } = req.body;
|
||||||
|
|
||||||
|
const newBeerStyleComment: z.infer<typeof CommentQueryResult> =
|
||||||
|
await createNewBeerStyleComment({
|
||||||
|
content,
|
||||||
|
rating,
|
||||||
|
beerStyleId: req.query.id,
|
||||||
|
userId: req.user!.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(201).json({
|
||||||
|
message: 'Beer comment created successfully',
|
||||||
|
statusCode: 201,
|
||||||
|
payload: newBeerStyleComment,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAll = async (
|
||||||
|
req: GetAllCommentsRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const beerStyleId = req.query.id;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
|
const { page_size, page_num } = req.query;
|
||||||
|
|
||||||
|
const comments = await getAllBeerStyleComments({
|
||||||
|
beerStyleId,
|
||||||
|
pageNum: parseInt(page_num, 10),
|
||||||
|
pageSize: parseInt(page_size, 10),
|
||||||
|
});
|
||||||
|
|
||||||
|
const count = await DBClient.instance.beerStyleComment.count({
|
||||||
|
where: { beerStyleId },
|
||||||
|
});
|
||||||
|
|
||||||
|
res.setHeader('X-Total-Count', count);
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Beer comments fetched successfully',
|
||||||
|
statusCode: 200,
|
||||||
|
payload: comments,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -5,7 +5,14 @@ import APIResponseValidationSchema from '@/validation/APIResponseValidationSchem
|
|||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { NextHandler } from 'next-connect';
|
import { NextHandler } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { CommentRequest, EditCommentRequest } from '../requestTypes';
|
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
|
||||||
|
import createNewBreweryComment from '@/services/BreweryComment/createNewBreweryComment';
|
||||||
|
import getAllBreweryComments from '@/services/BreweryComment/getAllBreweryComments';
|
||||||
|
import {
|
||||||
|
CommentRequest,
|
||||||
|
EditAndCreateCommentRequest,
|
||||||
|
GetAllCommentsRequest,
|
||||||
|
} from '../types';
|
||||||
|
|
||||||
export const checkIfBreweryCommentOwner = async <
|
export const checkIfBreweryCommentOwner = async <
|
||||||
CommentRequestType extends CommentRequest,
|
CommentRequestType extends CommentRequest,
|
||||||
@@ -30,7 +37,7 @@ export const checkIfBreweryCommentOwner = async <
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const editBreweryPostComment = async (
|
export const editBreweryPostComment = async (
|
||||||
req: EditCommentRequest,
|
req: EditAndCreateCommentRequest,
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
) => {
|
) => {
|
||||||
const { id } = req.query;
|
const { id } = req.query;
|
||||||
@@ -66,3 +73,57 @@ export const deleteBreweryPostComment = async (
|
|||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const createComment = async (
|
||||||
|
req: EditAndCreateCommentRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const { content, rating } = req.body;
|
||||||
|
|
||||||
|
const breweryPostId = req.query.id;
|
||||||
|
|
||||||
|
const user = req.user!;
|
||||||
|
|
||||||
|
const newBreweryComment: z.infer<typeof CommentQueryResult> =
|
||||||
|
await createNewBreweryComment({
|
||||||
|
content,
|
||||||
|
rating,
|
||||||
|
breweryPostId,
|
||||||
|
userId: user.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(201).json({
|
||||||
|
message: 'Beer comment created successfully',
|
||||||
|
statusCode: 201,
|
||||||
|
payload: newBreweryComment,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAll = async (
|
||||||
|
req: GetAllCommentsRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const breweryPostId = req.query.id;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
|
const { page_size, page_num } = req.query;
|
||||||
|
|
||||||
|
const comments = await getAllBreweryComments({
|
||||||
|
id: breweryPostId,
|
||||||
|
pageNum: parseInt(page_num, 10),
|
||||||
|
pageSize: parseInt(page_size, 10),
|
||||||
|
});
|
||||||
|
|
||||||
|
const count = await DBClient.instance.breweryComment.count({
|
||||||
|
where: { breweryPostId },
|
||||||
|
});
|
||||||
|
|
||||||
|
res.setHeader('X-Total-Count', count);
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Beer comments fetched successfully',
|
||||||
|
statusCode: 200,
|
||||||
|
payload: comments,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -6,6 +6,10 @@ export interface CommentRequest extends UserExtendedNextApiRequest {
|
|||||||
query: { id: string };
|
query: { id: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EditCommentRequest extends CommentRequest {
|
export interface EditAndCreateCommentRequest extends CommentRequest {
|
||||||
body: z.infer<typeof CreateCommentValidationSchema>;
|
body: z.infer<typeof CreateCommentValidationSchema>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GetAllCommentsRequest extends UserExtendedNextApiRequest {
|
||||||
|
query: { id: string; page_size: string; page_num: string };
|
||||||
|
}
|
||||||
@@ -8,12 +8,12 @@ import { NextApiResponse } from 'next';
|
|||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { CommentRequest } from '@/controllers/requestTypes';
|
import { CommentRequest } from '@/controllers/comments/types';
|
||||||
import {
|
import {
|
||||||
checkIfBeerCommentOwner,
|
checkIfBeerCommentOwner,
|
||||||
deleteBeerPostComment,
|
deleteBeerPostComment,
|
||||||
editBeerPostComment,
|
editBeerPostComment,
|
||||||
} from '@/controllers/beerComments';
|
} from '@/controllers/comments/beerComments';
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
CommentRequest,
|
CommentRequest,
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import {
|
|||||||
checkIfBeerStyleCommentOwner,
|
checkIfBeerStyleCommentOwner,
|
||||||
deleteBeerStyleComment,
|
deleteBeerStyleComment,
|
||||||
editBeerStyleComment,
|
editBeerStyleComment,
|
||||||
} from '@/controllers/beerStyleComments';
|
} from '@/controllers/comments/beerStyleComments';
|
||||||
import { CommentRequest } from '@/controllers/requestTypes';
|
import { CommentRequest } from '@/controllers/comments/types';
|
||||||
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
||||||
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
|
|||||||
@@ -1,78 +1,19 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import getAllBeerComments from '@/services/BeerComment/getAllBeerComments';
|
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
|
||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
import createNewBeerComment from '@/services/BeerComment/createNewBeerComment';
|
|
||||||
|
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
|
|
||||||
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
||||||
|
import {
|
||||||
interface CreateCommentRequest extends UserExtendedNextApiRequest {
|
createBeerPostComment,
|
||||||
body: z.infer<typeof CreateCommentValidationSchema>;
|
getAllBeerPostComments,
|
||||||
query: { id: string };
|
} from '@/controllers/comments/beerComments';
|
||||||
}
|
|
||||||
|
|
||||||
interface GetAllCommentsRequest extends UserExtendedNextApiRequest {
|
|
||||||
query: { id: string; page_size: string; page_num: string };
|
|
||||||
}
|
|
||||||
|
|
||||||
const createComment = async (
|
|
||||||
req: CreateCommentRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const { content, rating } = req.body;
|
|
||||||
|
|
||||||
const beerPostId = req.query.id;
|
|
||||||
|
|
||||||
const newBeerComment = await createNewBeerComment({
|
|
||||||
content,
|
|
||||||
rating,
|
|
||||||
beerPostId,
|
|
||||||
userId: req.user!.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
message: 'Beer comment created successfully',
|
|
||||||
statusCode: 201,
|
|
||||||
payload: newBeerComment,
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const getAll = async (
|
|
||||||
req: GetAllCommentsRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const beerPostId = req.query.id;
|
|
||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
||||||
const { page_size, page_num } = req.query;
|
|
||||||
|
|
||||||
const comments = await getAllBeerComments({
|
|
||||||
beerPostId,
|
|
||||||
pageNum: parseInt(page_num, 10),
|
|
||||||
pageSize: parseInt(page_size, 10),
|
|
||||||
});
|
|
||||||
|
|
||||||
const count = await DBClient.instance.beerComment.count({ where: { beerPostId } });
|
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', count);
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
message: 'Beer comments fetched successfully',
|
|
||||||
statusCode: 200,
|
|
||||||
payload: comments,
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
// I don't want to use any, but I can't figure out how to get the types to work
|
// @TODO: Fix this any type
|
||||||
any,
|
any,
|
||||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||||
>();
|
>();
|
||||||
@@ -83,7 +24,7 @@ router.post(
|
|||||||
querySchema: z.object({ id: z.string().cuid() }),
|
querySchema: z.object({ id: z.string().cuid() }),
|
||||||
}),
|
}),
|
||||||
getCurrentUser,
|
getCurrentUser,
|
||||||
createComment,
|
createBeerPostComment,
|
||||||
);
|
);
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
@@ -94,7 +35,7 @@ router.get(
|
|||||||
page_num: z.coerce.number().int().positive(),
|
page_num: z.coerce.number().int().positive(),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
getAll,
|
getAllBeerPostComments,
|
||||||
);
|
);
|
||||||
|
|
||||||
const handler = router.handler(NextConnectOptions);
|
const handler = router.handler(NextConnectOptions);
|
||||||
|
|||||||
@@ -1,77 +1,13 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
|
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
|
||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
|
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
|
|
||||||
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
||||||
import createNewBeerStyleComment from '@/services/BeerStyleComment/createNewBeerStyleComment';
|
import { createComment, getAll } from '@/controllers/comments/beerStyleComments';
|
||||||
import getAllBeerStyleComments from '@/services/BeerStyleComment/getAllBeerStyleComments';
|
|
||||||
|
|
||||||
interface CreateCommentRequest extends UserExtendedNextApiRequest {
|
|
||||||
body: z.infer<typeof CreateCommentValidationSchema>;
|
|
||||||
query: { id: string };
|
|
||||||
}
|
|
||||||
|
|
||||||
interface GetAllCommentsRequest extends UserExtendedNextApiRequest {
|
|
||||||
query: { id: string; page_size: string; page_num: string };
|
|
||||||
}
|
|
||||||
|
|
||||||
const createComment = async (
|
|
||||||
req: CreateCommentRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const { content, rating } = req.body;
|
|
||||||
|
|
||||||
const newBeerStyleComment: z.infer<typeof CommentQueryResult> =
|
|
||||||
await createNewBeerStyleComment({
|
|
||||||
content,
|
|
||||||
rating,
|
|
||||||
beerStyleId: req.query.id,
|
|
||||||
userId: req.user!.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
message: 'Beer comment created successfully',
|
|
||||||
statusCode: 201,
|
|
||||||
payload: newBeerStyleComment,
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const getAll = async (
|
|
||||||
req: GetAllCommentsRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const beerStyleId = req.query.id;
|
|
||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
||||||
const { page_size, page_num } = req.query;
|
|
||||||
|
|
||||||
const comments = await getAllBeerStyleComments({
|
|
||||||
beerStyleId,
|
|
||||||
pageNum: parseInt(page_num, 10),
|
|
||||||
pageSize: parseInt(page_size, 10),
|
|
||||||
});
|
|
||||||
|
|
||||||
const count = await DBClient.instance.beerStyleComment.count({
|
|
||||||
where: { beerStyleId },
|
|
||||||
});
|
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', count);
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
message: 'Beer comments fetched successfully',
|
|
||||||
statusCode: 200,
|
|
||||||
payload: comments,
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
// I don't want to use any, but I can't figure out how to get the types to work
|
// I don't want to use any, but I can't figure out how to get the types to work
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
|
|
||||||
import createNewBeerComment from '@/services/BeerComment/createNewBeerComment';
|
|
||||||
|
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
|
||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
|
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
@@ -13,73 +7,8 @@ import { z } from 'zod';
|
|||||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
|
|
||||||
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
|
|
||||||
import getAllBreweryComments from '@/services/BreweryComment/getAllBreweryComments';
|
|
||||||
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
||||||
import createNewBreweryComment from '@/services/BreweryComment/createNewBreweryComment';
|
import { createComment, getAll } from '@/controllers/comments/breweryComments';
|
||||||
|
|
||||||
interface CreateCommentRequest extends UserExtendedNextApiRequest {
|
|
||||||
body: z.infer<typeof CreateCommentValidationSchema>;
|
|
||||||
query: { id: string };
|
|
||||||
}
|
|
||||||
|
|
||||||
interface GetAllCommentsRequest extends UserExtendedNextApiRequest {
|
|
||||||
query: { id: string; page_size: string; page_num: string };
|
|
||||||
}
|
|
||||||
|
|
||||||
const createComment = async (
|
|
||||||
req: CreateCommentRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const { content, rating } = req.body;
|
|
||||||
|
|
||||||
const breweryPostId = req.query.id;
|
|
||||||
|
|
||||||
const user = req.user!;
|
|
||||||
|
|
||||||
const newBreweryComment: z.infer<typeof CommentQueryResult> =
|
|
||||||
await createNewBreweryComment({
|
|
||||||
content,
|
|
||||||
rating,
|
|
||||||
breweryPostId,
|
|
||||||
userId: user.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
message: 'Beer comment created successfully',
|
|
||||||
statusCode: 201,
|
|
||||||
payload: newBreweryComment,
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const getAll = async (
|
|
||||||
req: GetAllCommentsRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const breweryPostId = req.query.id;
|
|
||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
||||||
const { page_size, page_num } = req.query;
|
|
||||||
|
|
||||||
const comments = await getAllBreweryComments({
|
|
||||||
id: breweryPostId,
|
|
||||||
pageNum: parseInt(page_num, 10),
|
|
||||||
pageSize: parseInt(page_size, 10),
|
|
||||||
});
|
|
||||||
|
|
||||||
const count = await DBClient.instance.breweryComment.count({
|
|
||||||
where: { breweryPostId },
|
|
||||||
});
|
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', count);
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
message: 'Beer comments fetched successfully',
|
|
||||||
statusCode: 200,
|
|
||||||
payload: comments,
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
// I don't want to use any, but I can't figure out how to get the types to work
|
// I don't want to use any, but I can't figure out how to get the types to work
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
import { checkIfBeerCommentOwner } from '@/controllers/beerComments';
|
|
||||||
import {
|
import {
|
||||||
|
checkIfBreweryCommentOwner,
|
||||||
deleteBreweryPostComment,
|
deleteBreweryPostComment,
|
||||||
editBreweryPostComment,
|
editBreweryPostComment,
|
||||||
} from '@/controllers/breweryComments';
|
} from '@/controllers/comments/breweryComments';
|
||||||
import { CommentRequest } from '@/controllers/requestTypes';
|
import { CommentRequest } from '@/controllers/comments/types';
|
||||||
|
|
||||||
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ router
|
|||||||
querySchema: z.object({ id: z.string().cuid() }),
|
querySchema: z.object({ id: z.string().cuid() }),
|
||||||
}),
|
}),
|
||||||
getCurrentUser,
|
getCurrentUser,
|
||||||
checkIfBeerCommentOwner,
|
checkIfBreweryCommentOwner,
|
||||||
deleteBreweryPostComment,
|
deleteBreweryPostComment,
|
||||||
)
|
)
|
||||||
.put(
|
.put(
|
||||||
@@ -35,7 +36,7 @@ router
|
|||||||
bodySchema: CreateCommentValidationSchema,
|
bodySchema: CreateCommentValidationSchema,
|
||||||
}),
|
}),
|
||||||
getCurrentUser,
|
getCurrentUser,
|
||||||
checkIfBeerCommentOwner,
|
checkIfBreweryCommentOwner,
|
||||||
editBreweryPostComment,
|
editBreweryPostComment,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user