Refactor: extract logic out of api route

This commit is contained in:
Aaron William Po
2023-10-30 12:37:29 -04:00
parent 8093817ccc
commit 51c29702d3
4 changed files with 127 additions and 20 deletions

View File

@@ -0,0 +1,45 @@
import DBClient from '@/prisma/DBClient';
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next';
import { z } from 'zod';
import { GetAllBeersByBeerStyleRequest } from '.';
export const getAllBeersByBeerStyle = async (
req: GetAllBeersByBeerStyleRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { page_size, page_num, id } = req.query;
const beers: z.infer<typeof BeerPostQueryResult>[] =
await DBClient.instance.beerPost.findMany({
where: { styleId: id },
take: parseInt(page_size, 10),
skip: parseInt(page_num, 10) * parseInt(page_size, 10),
select: {
id: true,
name: true,
ibu: true,
abv: true,
createdAt: true,
updatedAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
style: { select: { name: true, id: true, description: true } },
beerImages: { select: { alt: true, path: true, caption: true, id: true } },
},
});
const pageCount = await DBClient.instance.beerPost.count({ where: { styleId: id } });
res.setHeader('X-Total-Count', pageCount);
res.status(200).json({
message: 'Beers fetched successfully',
statusCode: 200,
payload: beers,
success: true,
});
};

View File

@@ -1,7 +1,7 @@
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import DBClient from '@/prisma/DBClient';
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import getBeerPostsByBeerStyleId from '@/services/BeerPost/getBeerPostsByBeerStyleId';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiRequest, NextApiResponse } from 'next';
import { createRouter } from 'next-connect';
@@ -18,25 +18,11 @@ const getAllBeersByBeerStyle = async (
// eslint-disable-next-line @typescript-eslint/naming-convention
const { page_size, page_num, id } = req.query;
const beers: z.infer<typeof BeerPostQueryResult>[] =
await DBClient.instance.beerPost.findMany({
where: { styleId: id },
take: parseInt(page_size, 10),
skip: parseInt(page_num, 10) * parseInt(page_size, 10),
select: {
id: true,
name: true,
ibu: true,
abv: true,
createdAt: true,
updatedAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
style: { select: { name: true, id: true, description: true } },
beerImages: { select: { alt: true, path: true, caption: true, id: true } },
},
});
const beers = getBeerPostsByBeerStyleId({
pageNum: parseInt(page_num, 10),
pageSize: parseInt(page_size, 10),
styleId: id,
});
const pageCount = await DBClient.instance.beerPost.count({ where: { styleId: id } });

View File

@@ -0,0 +1,38 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerPostQueryResult from './schema/BeerPostQueryResult';
interface GetBeerPostsByBeerStyleIdArgs {
styleId: string;
pageSize: number;
pageNum: number;
}
const getBeerPostsByBeerStyleId = async ({
pageNum,
pageSize,
styleId,
}: GetBeerPostsByBeerStyleIdArgs): Promise<z.infer<typeof BeerPostQueryResult>[]> => {
const beers = await DBClient.instance.beerPost.findMany({
where: { styleId },
take: pageSize,
skip: pageNum * pageSize,
select: {
id: true,
name: true,
ibu: true,
abv: true,
createdAt: true,
updatedAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
style: { select: { name: true, id: true, description: true } },
beerImages: { select: { alt: true, path: true, caption: true, id: true } },
},
});
return beers;
};
export default getBeerPostsByBeerStyleId;

View File

@@ -0,0 +1,38 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerPostQueryResult from './schema/BeerPostQueryResult';
interface GetBeerPostsByBeerStyleIdArgs {
breweryId: string;
pageSize: number;
pageNum: number;
}
const getBeerPostsByBeerStyleId = async ({
pageNum,
pageSize,
breweryId,
}: GetBeerPostsByBeerStyleIdArgs): Promise<z.infer<typeof BeerPostQueryResult>[]> => {
const beers = await DBClient.instance.beerPost.findMany({
where: { breweryId },
take: pageSize,
skip: pageNum * pageSize,
select: {
id: true,
name: true,
ibu: true,
abv: true,
createdAt: true,
updatedAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
style: { select: { name: true, id: true, description: true } },
beerImages: { select: { alt: true, path: true, caption: true, id: true } },
},
});
return beers;
};
export default getBeerPostsByBeerStyleId;