mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
73
src/pages/api/beers/styles/[id]/beers/index.ts
Normal file
73
src/pages/api/beers/styles/[id]/beers/index.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
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 APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
|
||||
interface GetAllBeersByBeerStyleRequest extends NextApiRequest {
|
||||
query: { page_size: string; page_num: string; id: string };
|
||||
}
|
||||
|
||||
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: { breweryId: id },
|
||||
});
|
||||
|
||||
res.setHeader('X-Total-Count', pageCount);
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Beers fetched successfully',
|
||||
statusCode: 200,
|
||||
payload: beers,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
const router = createRouter<
|
||||
GetAllBeersByBeerStyleRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.get(
|
||||
validateRequest({
|
||||
querySchema: z.object({
|
||||
page_size: z.string().min(1),
|
||||
page_num: z.string().min(1),
|
||||
id: z.string().min(1),
|
||||
}),
|
||||
}),
|
||||
getAllBeersByBeerStyle,
|
||||
);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
|
||||
export default handler;
|
||||
@@ -9,6 +9,7 @@ import getBeerStyleById from '@/services/BeerStyles/getBeerStyleById';
|
||||
import BeerStyleHeader from '@/components/BeerStyleById/BeerStyleHeader';
|
||||
import BeerStyleQueryResult from '@/services/BeerStyles/schema/BeerStyleQueryResult';
|
||||
import BeerStyleCommentSection from '@/components/BeerStyleById/BeerStyleCommentSection';
|
||||
import BeerStyleBeerSection from '@/components/BeerStyleById/BeerStyleBeerSection';
|
||||
|
||||
interface BeerStylePageProps {
|
||||
beerStyle: z.infer<typeof BeerStyleQueryResult>;
|
||||
@@ -33,7 +34,9 @@ const BeerStyleByIdPage: NextPage<BeerStylePageProps> = ({ beerStyle }) => {
|
||||
<div className="w-[60%]">
|
||||
<BeerStyleCommentSection beerStyle={beerStyle} />
|
||||
</div>
|
||||
<div className="w-[40%]">{/* Beers of this style go here */}</div>
|
||||
<div className="w-[40%]">
|
||||
<BeerStyleBeerSection beerStyle={beerStyle} />
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<Tab.Group>
|
||||
@@ -49,7 +52,9 @@ const BeerStyleByIdPage: NextPage<BeerStylePageProps> = ({ beerStyle }) => {
|
||||
<Tab.Panel>
|
||||
<BeerStyleCommentSection beerStyle={beerStyle} />
|
||||
</Tab.Panel>
|
||||
<Tab.Panel>{/* Beers of this style go here */}</Tab.Panel>
|
||||
<Tab.Panel>
|
||||
<BeerStyleBeerSection beerStyle={beerStyle} />
|
||||
</Tab.Panel>
|
||||
</Tab.Panels>
|
||||
</Tab.Group>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user