misc: Rename beer type to beer style

This commit is contained in:
Aaron William Po
2023-09-18 15:44:56 -04:00
parent 6b8682e686
commit af09928c3c
30 changed files with 1192 additions and 183 deletions

View File

@@ -17,14 +17,14 @@ const createBeerPost = async (
req: CreateBeerPostRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const { name, description, typeId, abv, ibu, breweryId } = req.body;
const { name, description, styleId: typeId, abv, ibu, breweryId } = req.body;
const newBeerPost = await createNewBeerPost({
name,
description,
abv,
ibu,
typeId,
styleId: typeId,
breweryId,
userId: req.user!.id,
});

View File

@@ -29,16 +29,15 @@ const search = async (req: SearchAPIRequest, res: NextApiResponse) => {
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
type: { select: { name: true, id: true } },
style: { select: { name: true, id: true, description: true } },
beerImages: { select: { alt: true, path: true, caption: true, id: true } },
},
where: {
OR: [
{ name: { contains: query, mode: 'insensitive' } },
{ description: { contains: query, mode: 'insensitive' } },
{ brewery: { name: { contains: query, mode: 'insensitive' } } },
{ type: { name: { contains: query, mode: 'insensitive' } } },
{ style: { name: { contains: query, mode: 'insensitive' } } },
],
},
});

View File

@@ -8,44 +8,46 @@ import { NextApiRequest, NextApiResponse } from 'next';
import { createRouter } from 'next-connect';
import { z } from 'zod';
const BeerTypeValidationSchema = z.object({
const BeerStyleValidationSchema = z.object({
id: z.string().cuid(),
name: z.string(),
postedBy: z.object({
id: z.string().cuid(),
username: z.string(),
}),
description: z.string(),
createdAt: z.date(),
updatedAt: z.date().nullable(),
});
const CreateBeerTypeValidationSchema = BeerTypeValidationSchema.omit({
const CreateBeerStyleValidationSchema = BeerStyleValidationSchema.omit({
id: true,
postedBy: true,
createdAt: true,
updatedAt: true,
});
interface CreateBeerTypeRequest extends UserExtendedNextApiRequest {
body: z.infer<typeof CreateBeerTypeValidationSchema>;
interface CreateBeerStyleRequest extends UserExtendedNextApiRequest {
body: z.infer<typeof CreateBeerStyleValidationSchema>;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface GetBeerTypeRequest extends NextApiRequest {
interface GetBeerStyleRequest extends NextApiRequest {
query: {
id: string;
};
}
const createBeerType = async (
req: CreateBeerTypeRequest,
const createBeerStyle = async (
req: CreateBeerStyleRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const user = req.user!;
const { name } = req.body;
const { name, description } = req.body;
const newBeerType = await DBClient.instance.beerType.create({
const newBeerStyle = await DBClient.instance.beerStyle.create({
data: {
description,
name,
postedBy: { connect: { id: user.id } },
},
@@ -61,20 +63,20 @@ const createBeerType = async (
res.status(200).json({
message: 'Beer posts retrieved successfully',
statusCode: 200,
payload: newBeerType,
payload: newBeerStyle,
success: true,
});
};
const router = createRouter<
CreateBeerTypeRequest,
CreateBeerStyleRequest,
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
>();
router.get(
validateRequest({ bodySchema: CreateBeerTypeValidationSchema }),
validateRequest({ bodySchema: CreateBeerStyleValidationSchema }),
getCurrentUser,
createBeerType,
createBeerStyle,
);
const handler = router.handler();

View File

@@ -1,38 +1,38 @@
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import DBClient from '@/prisma/DBClient';
import getAllBeerTypes from '@/services/BeerTypes/getAllBeerTypes';
import getAllBeerStyles from '@/services/BeerStyles/getAllBeerStyles';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiRequest, NextApiResponse } from 'next';
import { createRouter } from 'next-connect';
import { z } from 'zod';
interface GetBeerTypesRequest extends NextApiRequest {
interface GetBeerStylesRequest extends NextApiRequest {
query: { page_num: string; page_size: string };
}
const getBeerTypes = async (
req: GetBeerTypesRequest,
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 beerTypes = await getAllBeerTypes(pageNum, pageSize);
const beerTypeCount = await DBClient.instance.beerType.count();
const beerStyles = await getAllBeerStyles(pageNum, pageSize);
const beerStyleCount = await DBClient.instance.beerStyle.count();
res.setHeader('X-Total-Count', beerTypeCount);
res.setHeader('X-Total-Count', beerStyleCount);
res.status(200).json({
message: 'Beer types retrieved successfully',
statusCode: 200,
payload: beerTypes,
payload: beerStyles,
success: true,
});
};
const router = createRouter<
GetBeerTypesRequest,
GetBeerStylesRequest,
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
>();
@@ -43,7 +43,7 @@ router.get(
page_size: z.string().regex(/^\d+$/),
}),
}),
getBeerTypes,
getBeerStyles,
);
const handler = router.handler();

View File

@@ -32,7 +32,7 @@ const getAllBeersByBrewery = async (
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
type: { select: { name: true, id: true } },
style: { select: { name: true, id: true, description: true } },
beerImages: { select: { alt: true, path: true, caption: true, id: true } },
},
});