mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
feat: begin work on beer type page and associated api routes
This commit is contained in:
@@ -53,7 +53,7 @@ const editComment = async (
|
||||
id,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Comment updated successfully',
|
||||
statusCode: 200,
|
||||
|
||||
82
src/pages/api/beers/types/create.ts
Normal file
82
src/pages/api/beers/types/create.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
|
||||
const BeerTypeValidationSchema = z.object({
|
||||
id: z.string().cuid(),
|
||||
name: z.string(),
|
||||
postedBy: z.object({
|
||||
id: z.string().cuid(),
|
||||
username: z.string(),
|
||||
}),
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date().nullable(),
|
||||
});
|
||||
|
||||
const CreateBeerTypeValidationSchema = BeerTypeValidationSchema.omit({
|
||||
id: true,
|
||||
postedBy: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
});
|
||||
|
||||
interface CreateBeerTypeRequest extends UserExtendedNextApiRequest {
|
||||
body: z.infer<typeof CreateBeerTypeValidationSchema>;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
interface GetBeerTypeRequest extends NextApiRequest {
|
||||
query: {
|
||||
id: string;
|
||||
};
|
||||
}
|
||||
|
||||
const createBeerType = async (
|
||||
req: CreateBeerTypeRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const user = req.user!;
|
||||
const { name } = req.body;
|
||||
|
||||
const newBeerType = await DBClient.instance.beerType.create({
|
||||
data: {
|
||||
name,
|
||||
postedBy: { connect: { id: user.id } },
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
postedBy: { select: { id: true, username: true } },
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Beer posts retrieved successfully',
|
||||
statusCode: 200,
|
||||
payload: newBeerType,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
const router = createRouter<
|
||||
CreateBeerTypeRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.get(
|
||||
validateRequest({ bodySchema: CreateBeerTypeValidationSchema }),
|
||||
getCurrentUser,
|
||||
createBeerType,
|
||||
);
|
||||
|
||||
const handler = router.handler();
|
||||
|
||||
export default handler;
|
||||
51
src/pages/api/beers/types/index.ts
Normal file
51
src/pages/api/beers/types/index.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import getAllBeerTypes from '@/services/BeerTypes/getAllBeerTypes';
|
||||
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
|
||||
interface GetBeerTypesRequest extends NextApiRequest {
|
||||
query: { page_num: string; page_size: string };
|
||||
}
|
||||
|
||||
const getBeerTypes = async (
|
||||
req: GetBeerTypesRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const pageNum = parseInt(req.query.page_num, 10);
|
||||
const pageSize = parseInt(req.query.page_size, 10);
|
||||
|
||||
const beerTypes = await getAllBeerTypes(pageNum, pageSize);
|
||||
const beerTypeCount = await DBClient.instance.beerType.count();
|
||||
|
||||
res.setHeader('X-Total-Count', beerTypeCount);
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Beer types retrieved successfully',
|
||||
statusCode: 200,
|
||||
payload: beerTypes,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
const router = createRouter<
|
||||
GetBeerTypesRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.get(
|
||||
validateRequest({
|
||||
querySchema: z.object({
|
||||
page_num: z.string().regex(/^\d+$/),
|
||||
page_size: z.string().regex(/^\d+$/),
|
||||
}),
|
||||
}),
|
||||
getBeerTypes,
|
||||
);
|
||||
|
||||
const handler = router.handler();
|
||||
|
||||
export default handler;
|
||||
Reference in New Issue
Block a user