mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Refactor: extract logic out of api route
This commit is contained in:
@@ -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,
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
import getBeerPostsByBeerStyleId from '@/services/BeerPost/getBeerPostsByBeerStyleId';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
@@ -18,24 +18,10 @@ const getAllBeersByBeerStyle = async (
|
|||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
const { page_size, page_num, id } = req.query;
|
const { page_size, page_num, id } = req.query;
|
||||||
|
|
||||||
const beers: z.infer<typeof BeerPostQueryResult>[] =
|
const beers = getBeerPostsByBeerStyleId({
|
||||||
await DBClient.instance.beerPost.findMany({
|
pageNum: parseInt(page_num, 10),
|
||||||
where: { styleId: id },
|
pageSize: parseInt(page_size, 10),
|
||||||
take: parseInt(page_size, 10),
|
styleId: id,
|
||||||
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 } });
|
const pageCount = await DBClient.instance.beerPost.count({ where: { styleId: id } });
|
||||||
|
|||||||
38
src/services/BeerPost/getBeerPostsByBeerStyleId.ts
Normal file
38
src/services/BeerPost/getBeerPostsByBeerStyleId.ts
Normal 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;
|
||||||
38
src/services/BeerPost/getBeerPostsByBreweryId.ts
Normal file
38
src/services/BeerPost/getBeerPostsByBreweryId.ts
Normal 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;
|
||||||
Reference in New Issue
Block a user