This commit is contained in:
Aaron William Po
2023-10-07 13:27:01 -04:00
parent 5b287ed2ac
commit 2ee12d351f
21 changed files with 346 additions and 210 deletions

View File

@@ -45,12 +45,7 @@ const editBeerPost = async (
req: EditBeerPostRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const {
body,
query: { id },
} = req;
await editBeerPostById(id, body);
await editBeerPostById({ id: req.query.id, data: req.body });
res.status(200).json({
message: 'Beer post updated successfully',
@@ -62,8 +57,7 @@ const editBeerPost = async (
const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
const { id } = req.query;
const deleted = deleteBeerPostById(id);
const deleted = deleteBeerPostById({ beerPostId: id });
if (!deleted) {
throw new ServerError('Beer post not found', 404);
}
@@ -74,26 +68,28 @@ const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
statusCode: 200,
});
};
const router = createRouter<
EditBeerPostRequest,
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
>();
router.put(
validateRequest({
bodySchema: EditBeerPostValidationSchema,
querySchema: z.object({ id: z.string() }),
}),
getCurrentUser,
checkIfBeerPostOwner,
editBeerPost,
);
router.delete(
validateRequest({ querySchema: z.object({ id: z.string() }) }),
getCurrentUser,
checkIfBeerPostOwner,
deleteBeerPost,
);
router
.put(
validateRequest({
bodySchema: EditBeerPostValidationSchema,
querySchema: z.object({ id: z.string() }),
}),
getCurrentUser,
checkIfBeerPostOwner,
editBeerPost,
)
.delete(
validateRequest({ querySchema: z.object({ id: z.string() }) }),
getCurrentUser,
checkIfBeerPostOwner,
deleteBeerPost,
);
const handler = router.handler(NextConnectOptions);

View File

@@ -19,7 +19,7 @@ const getBeerPosts = async (
const pageNum = parseInt(req.query.page_num, 10);
const pageSize = parseInt(req.query.page_size, 10);
const beerPosts = await getAllBeerPosts(pageNum, pageSize);
const beerPosts = await getAllBeerPosts({ pageNum, pageSize });
const beerPostCount = await DBClient.instance.beerPost.count();
res.setHeader('X-Total-Count', beerPostCount);

View File

@@ -26,6 +26,7 @@ const search = async (req: SearchAPIRequest, res: NextApiResponse) => {
ibu: true,
abv: true,
createdAt: true,
updatedAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },

View File

@@ -29,6 +29,7 @@ const getAllBeersByBrewery = async (
ibu: true,
abv: true,
createdAt: true,
updatedAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },

View File

@@ -19,7 +19,7 @@ const getBreweryPosts = async (
const pageNum = parseInt(req.query.page_num, 10);
const pageSize = parseInt(req.query.page_size, 10);
const breweryPosts = await getAllBreweryPosts(pageNum, pageSize);
const breweryPosts = await getAllBreweryPosts({ pageNum, pageSize });
const breweryPostCount = await DBClient.instance.breweryPost.count();
res.setHeader('X-Total-Count', breweryPostCount);