continue extracting user controllers out of routes

This commit is contained in:
Aaron William Po
2023-12-06 20:30:11 -05:00
parent 2ff39613cd
commit c7d5c65ffb
31 changed files with 584 additions and 543 deletions

View File

@@ -0,0 +1,36 @@
import DBClient from '@/prisma/DBClient';
import getAllBreweryPostsByPostedById from '@/services/BreweryPost/getAllBreweryPostsByPostedById';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next';
import { z } from 'zod';
import { GetPostsByUserIdRequest } from '../types';
// eslint-disable-next-line import/prefer-default-export
export const getBreweryPostsByUserId = async (
req: GetPostsByUserIdRequest,
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,
});
};