mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Update: add update brewery post
This commit is contained in:
91
src/pages/api/breweries/[id]/index.ts
Normal file
91
src/pages/api/breweries/[id]/index.ts
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
|
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||||
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
|
import { NextApiResponse } from 'next';
|
||||||
|
import { createRouter, NextHandler } from 'next-connect';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import ServerError from '@/config/util/ServerError';
|
||||||
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
import getBreweryPostById from '@/services/BreweryPost/getBreweryPostById';
|
||||||
|
import EditBreweryPostValidationSchema from '@/services/BreweryPost/types/EditBreweryPostValidationSchema';
|
||||||
|
|
||||||
|
interface BreweryPostRequest extends UserExtendedNextApiRequest {
|
||||||
|
query: { id: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
interface EditBreweryPostRequest extends BreweryPostRequest {
|
||||||
|
body: z.infer<typeof EditBreweryPostValidationSchema>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkIfBreweryPostOwner = async (
|
||||||
|
req: BreweryPostRequest,
|
||||||
|
res: NextApiResponse,
|
||||||
|
next: NextHandler,
|
||||||
|
) => {
|
||||||
|
const user = req.user!;
|
||||||
|
const { id } = req.query;
|
||||||
|
|
||||||
|
const breweryPost = await getBreweryPostById(id);
|
||||||
|
if (!breweryPost) {
|
||||||
|
throw new ServerError('Brewery post not found', 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (breweryPost.postedBy.id !== user.id) {
|
||||||
|
throw new ServerError('You are not the owner of this brewery post', 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
||||||
|
const editBreweryPost = async (
|
||||||
|
req: EditBreweryPostRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const {
|
||||||
|
body,
|
||||||
|
query: { id },
|
||||||
|
} = req;
|
||||||
|
|
||||||
|
await DBClient.instance.breweryPost.update({
|
||||||
|
where: { id },
|
||||||
|
data: body,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Brewery post updated successfully',
|
||||||
|
success: true,
|
||||||
|
statusCode: 200,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteBreweryPost = async (req: BreweryPostRequest, res: NextApiResponse) => {
|
||||||
|
const {
|
||||||
|
query: { id },
|
||||||
|
} = req;
|
||||||
|
|
||||||
|
const deleted = await DBClient.instance.beerPost.delete({
|
||||||
|
where: { id },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!deleted) {
|
||||||
|
throw new ServerError('Brewery post not found', 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Brewery post deleted successfully',
|
||||||
|
success: true,
|
||||||
|
statusCode: 200,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const router = createRouter<
|
||||||
|
EditBreweryPostRequest,
|
||||||
|
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||||
|
>();
|
||||||
|
|
||||||
|
router.put(getCurrentUser, checkIfBreweryPostOwner, editBreweryPost);
|
||||||
|
router.delete(getCurrentUser, checkIfBreweryPostOwner, deleteBreweryPost);
|
||||||
|
|
||||||
|
const handler = router.handler(NextConnectOptions);
|
||||||
|
|
||||||
|
export default handler;
|
||||||
@@ -14,7 +14,7 @@ interface EditPageProps {
|
|||||||
beerPost: z.infer<typeof beerPostQueryResult>;
|
beerPost: z.infer<typeof beerPostQueryResult>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const EditPage: NextPage<EditPageProps> = ({ beerPost }) => {
|
const EditBeerPostPage: NextPage<EditPageProps> = ({ beerPost }) => {
|
||||||
const pageTitle = `Edit \u201c${beerPost.name}\u201d`;
|
const pageTitle = `Edit \u201c${beerPost.name}\u201d`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -44,7 +44,7 @@ const EditPage: NextPage<EditPageProps> = ({ beerPost }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default EditPage;
|
export default EditBeerPostPage;
|
||||||
|
|
||||||
export const getServerSideProps = withPageAuthRequired<EditPageProps>(
|
export const getServerSideProps = withPageAuthRequired<EditPageProps>(
|
||||||
async (context, session) => {
|
async (context, session) => {
|
||||||
|
|||||||
55
src/pages/breweries/[id]/edit.tsx
Normal file
55
src/pages/breweries/[id]/edit.tsx
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import FormPageLayout from '@/components/ui/forms/FormPageLayout';
|
||||||
|
import getBreweryPostById from '@/services/BreweryPost/getBreweryPostById';
|
||||||
|
import BreweryPostQueryResult from '@/services/BreweryPost/types/BreweryPostQueryResult';
|
||||||
|
import withPageAuthRequired from '@/util/withPageAuthRequired';
|
||||||
|
import { NextPage } from 'next';
|
||||||
|
import Head from 'next/head';
|
||||||
|
import { BiBeer } from 'react-icons/bi';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
interface EditPageProps {
|
||||||
|
breweryPost: z.infer<typeof BreweryPostQueryResult>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const EditBreweryPostPage: NextPage<EditPageProps> = ({ breweryPost }) => {
|
||||||
|
const pageTitle = `Edit \u201c${breweryPost.name}\u201d`;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Head>
|
||||||
|
<title>{pageTitle}</title>
|
||||||
|
<meta name="description" content={pageTitle} />
|
||||||
|
</Head>
|
||||||
|
|
||||||
|
<FormPageLayout
|
||||||
|
headingText={pageTitle}
|
||||||
|
headingIcon={BiBeer}
|
||||||
|
backLink={`/breweries/${breweryPost.id}`}
|
||||||
|
backLinkText={`Back to "${breweryPost.name}"`}
|
||||||
|
>
|
||||||
|
<></>
|
||||||
|
</FormPageLayout>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EditBreweryPostPage;
|
||||||
|
|
||||||
|
export const getServerSideProps = withPageAuthRequired<EditPageProps>(
|
||||||
|
async (context, session) => {
|
||||||
|
const breweryPostId = context.params?.id as string;
|
||||||
|
const breweryPost = await getBreweryPostById(breweryPostId);
|
||||||
|
|
||||||
|
const { id: userId } = session;
|
||||||
|
|
||||||
|
if (!breweryPost) {
|
||||||
|
return { notFound: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
const isBreweryPostOwner = breweryPost.postedBy.id === userId;
|
||||||
|
|
||||||
|
return isBreweryPostOwner
|
||||||
|
? { props: { breweryPost: JSON.parse(JSON.stringify(breweryPost)) } }
|
||||||
|
: { redirect: { destination: `/breweries/${breweryPostId}`, permanent: false } };
|
||||||
|
},
|
||||||
|
);
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import CreateBreweryPostSchema from './CreateBreweryPostSchema';
|
||||||
|
|
||||||
|
const EditBreweryPostValidationSchema = CreateBreweryPostSchema.extend({
|
||||||
|
id: z.string().cuid(),
|
||||||
|
}).omit({
|
||||||
|
address: true,
|
||||||
|
city: true,
|
||||||
|
region: true,
|
||||||
|
country: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default EditBreweryPostValidationSchema;
|
||||||
Reference in New Issue
Block a user