mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
misc: Rename beer type to beer style
This commit is contained in:
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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' } } },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
@@ -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 } },
|
||||
},
|
||||
});
|
||||
|
||||
@@ -37,6 +37,7 @@ const EditBeerPostPage: NextPage<EditPageProps> = ({ beerPost }) => {
|
||||
ibu: beerPost.ibu,
|
||||
description: beerPost.description,
|
||||
id: beerPost.id,
|
||||
styleId: beerPost.style.id,
|
||||
}}
|
||||
/>
|
||||
</FormPageLayout>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import BeerStyleCard from '@/components/BeerStyle/BeerStyleCard';
|
||||
import LoadingCard from '@/components/ui/LoadingCard';
|
||||
import Spinner from '@/components/ui/Spinner';
|
||||
import useBeerTypes from '@/hooks/data-fetching/beer-types/useBeerTypes';
|
||||
import useBeerStyles from '@/hooks/data-fetching/beer-styles/useBeerStyles';
|
||||
|
||||
import { NextPage } from 'next';
|
||||
import Head from 'next/head';
|
||||
@@ -8,25 +9,22 @@ import { MutableRefObject, useRef } from 'react';
|
||||
import { FaArrowUp } from 'react-icons/fa';
|
||||
import { useInView } from 'react-intersection-observer';
|
||||
|
||||
const BeerTypePage: NextPage = () => {
|
||||
const BeerStylePage: NextPage = () => {
|
||||
const PAGE_SIZE = 20;
|
||||
const pageRef: MutableRefObject<HTMLDivElement | null> = useRef(null);
|
||||
|
||||
const { beerTypes, isLoading, isLoadingMore, isAtEnd, size, setSize, error } =
|
||||
useBeerTypes({
|
||||
pageSize: PAGE_SIZE,
|
||||
});
|
||||
const { beerStyles, isLoading, isLoadingMore, isAtEnd, size, setSize } = useBeerStyles({
|
||||
pageSize: PAGE_SIZE,
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { ref: lastBeerTypeRef } = useInView({
|
||||
const { ref: lastBeerStyleRef } = useInView({
|
||||
onChange: (visible) => {
|
||||
if (!visible || isAtEnd) return;
|
||||
setSize(size + 1);
|
||||
},
|
||||
});
|
||||
|
||||
console.log(error);
|
||||
console.log(beerTypes);
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
@@ -41,19 +39,19 @@ const BeerTypePage: NextPage = () => {
|
||||
<header className="my-10 flex justify-between lg:flex-row">
|
||||
<div>
|
||||
<h1 className="text-4xl font-bold lg:text-6xl">The Biergarten App</h1>
|
||||
<h2 className="text-2xl font-bold lg:text-4xl">Beers</h2>
|
||||
<h2 className="text-2xl font-bold lg:text-4xl">Types</h2>
|
||||
</div>
|
||||
</header>
|
||||
<div className="grid gap-6 xl:grid-cols-2">
|
||||
{!!beerTypes.length && !isLoading && (
|
||||
{!!beerStyles.length && !isLoading && (
|
||||
<>
|
||||
{beerTypes.map((beerType, i) => {
|
||||
{beerStyles.map((beerStyle, i) => {
|
||||
return (
|
||||
<div
|
||||
key={beerType.id}
|
||||
ref={beerTypes.length === i + 1 ? lastBeerTypeRef : undefined}
|
||||
key={beerStyle.id}
|
||||
ref={beerStyles.length === i + 1 ? lastBeerStyleRef : undefined}
|
||||
>
|
||||
{beerType.name}
|
||||
<BeerStyleCard beerStyle={beerStyle} />
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
@@ -101,4 +99,4 @@ const BeerTypePage: NextPage = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default BeerTypePage;
|
||||
export default BeerStylePage;
|
||||
@@ -4,7 +4,7 @@ import FormPageLayout from '@/components/ui/forms/FormPageLayout';
|
||||
import withPageAuthRequired from '@/util/withPageAuthRequired';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import BreweryPostQueryResult from '@/services/BreweryPost/schema/BreweryPostQueryResult';
|
||||
import { BeerType } from '@prisma/client';
|
||||
import { BeerStyle } from '@prisma/client';
|
||||
import { NextPage } from 'next';
|
||||
import { BiBeer } from 'react-icons/bi';
|
||||
import { z } from 'zod';
|
||||
@@ -12,10 +12,10 @@ import getBreweryPostById from '@/services/BreweryPost/getBreweryPostById';
|
||||
|
||||
interface CreateBeerPageProps {
|
||||
brewery: z.infer<typeof BreweryPostQueryResult>;
|
||||
types: BeerType[];
|
||||
styles: BeerStyle[];
|
||||
}
|
||||
|
||||
const CreateBeerPost: NextPage<CreateBeerPageProps> = ({ brewery, types }) => {
|
||||
const CreateBeerPost: NextPage<CreateBeerPageProps> = ({ brewery, styles }) => {
|
||||
return (
|
||||
<FormPageLayout
|
||||
headingText="Create a new beer"
|
||||
@@ -23,7 +23,7 @@ const CreateBeerPost: NextPage<CreateBeerPageProps> = ({ brewery, types }) => {
|
||||
backLink="/beers"
|
||||
backLinkText="Back to beers"
|
||||
>
|
||||
<CreateBeerPostForm brewery={brewery} types={types} />
|
||||
<CreateBeerPostForm brewery={brewery} styles={styles} />
|
||||
</FormPageLayout>
|
||||
);
|
||||
};
|
||||
@@ -33,12 +33,12 @@ export const getServerSideProps = withPageAuthRequired<CreateBeerPageProps>(
|
||||
const id = context.params?.id as string;
|
||||
|
||||
const breweryPost = await getBreweryPostById(id);
|
||||
const beerTypes = await DBClient.instance.beerType.findMany();
|
||||
const beerStyles = await DBClient.instance.beerStyle.findMany();
|
||||
|
||||
return {
|
||||
props: {
|
||||
brewery: JSON.parse(JSON.stringify(breweryPost)),
|
||||
types: JSON.parse(JSON.stringify(beerTypes)),
|
||||
styles: JSON.parse(JSON.stringify(beerStyles)),
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user