mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Refactor: further extract controller logic from routers
This commit is contained in:
@@ -1,39 +1,16 @@
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import getAllBeerStyles from '@/services/BeerStyles/getAllBeerStyles';
|
||||
import { getBeerStyles } from '@/controllers/posts/beerStyles';
|
||||
import { GetAllPostsRequest } from '@/controllers/posts/types';
|
||||
|
||||
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
|
||||
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
|
||||
interface GetBeerStylesRequest extends NextApiRequest {
|
||||
query: z.infer<typeof PaginatedQueryResponseSchema>;
|
||||
}
|
||||
|
||||
const getBeerStyles = async (
|
||||
req: GetBeerStylesRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const pageNum = parseInt(req.query.page_num, 10);
|
||||
const pageSize = parseInt(req.query.page_size, 10);
|
||||
|
||||
const beerStyles = await getAllBeerStyles({ pageNum, pageSize });
|
||||
const beerStyleCount = await DBClient.instance.beerStyle.count();
|
||||
|
||||
res.setHeader('X-Total-Count', beerStyleCount);
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Beer types retrieved successfully',
|
||||
statusCode: 200,
|
||||
payload: beerStyles,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
const router = createRouter<
|
||||
GetBeerStylesRequest,
|
||||
GetAllPostsRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user