Feat: add user posts functionality for account page

This commit is contained in:
Aaron William Po
2023-11-26 21:45:03 -05:00
parent c0d90a84d8
commit 00e980d71e
10 changed files with 552 additions and 3 deletions

View File

@@ -0,0 +1,58 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { createRouter } from 'next-connect';
import { z } from 'zod';
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import DBClient from '@/prisma/DBClient';
import getBeerPostsByPostedById from '@/services/BeerPost/getBeerPostsByPostedById';
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
interface GetBeerPostsRequest extends NextApiRequest {
query: {
page_num: string;
page_size: string;
id: string;
};
}
const getBeerPostsByUserId = async (
req: GetBeerPostsRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const pageNum = parseInt(req.query.page_num, 10);
const pageSize = parseInt(req.query.page_size, 10);
const { id } = req.query;
const beerPosts = await getBeerPostsByPostedById({ pageNum, pageSize, postedById: id });
const beerPostCount = await DBClient.instance.beerPost.count({
where: { postedBy: { id } },
});
res.setHeader('X-Total-Count', beerPostCount);
res.status(200).json({
message: `Beer posts by user ${id} fetched successfully`,
statusCode: 200,
payload: beerPosts,
success: true,
});
};
const router = createRouter<
GetBeerPostsRequest,
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
>();
router.get(
validateRequest({
querySchema: PaginatedQueryResponseSchema.extend({ id: z.string().cuid() }),
}),
getBeerPostsByUserId,
);
const handler = router.handler();
export default handler;

View File

@@ -0,0 +1,62 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { createRouter } from 'next-connect';
import { z } from 'zod';
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import DBClient from '@/prisma/DBClient';
import getAllBreweryPostsByPostedById from '@/services/BreweryPost/getAllBreweryPostsByPostedById';
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
interface GetBreweryPostsRequest extends NextApiRequest {
query: {
page_num: string;
page_size: string;
id: string;
};
}
const getBreweryPostsByUserId = async (
req: GetBreweryPostsRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const pageNum = parseInt(req.query.page_num, 10);
const pageSize = parseInt(req.query.page_size, 10);
const { id } = req.query;
const breweryPosts = await getAllBreweryPostsByPostedById({
pageNum,
pageSize,
postedById: id,
});
const breweryPostCount = await DBClient.instance.breweryPost.count({
where: { postedBy: { id } },
});
res.setHeader('X-Total-Count', breweryPostCount);
res.status(200).json({
message: `Brewery posts by user ${id} fetched successfully`,
statusCode: 200,
payload: breweryPosts,
success: true,
});
};
const router = createRouter<
GetBreweryPostsRequest,
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
>();
router.get(
validateRequest({
querySchema: PaginatedQueryResponseSchema.extend({ id: z.string().cuid() }),
}),
getBreweryPostsByUserId,
);
const handler = router.handler();
export default handler;