mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Update next-connect, begin work on cloud img upload
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import validateRequest from '@/config/zod/middleware/validateRequest';
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import NextConnectConfig from '@/config/nextConnect/NextConnectConfig';
|
||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||
import createNewBeerComment from '@/services/BeerComment/createNewBeerComment';
|
||||
import { BeerCommentQueryResultT } from '@/services/BeerComment/schema/BeerCommentQueryResult';
|
||||
import BeerCommentValidationSchema from '@/services/BeerComment/schema/CreateBeerCommentValidationSchema';
|
||||
|
||||
import nextConnect from 'next-connect';
|
||||
import { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import getCurrentUser from '@/config/auth/middleware/getCurrentUser';
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import { NextApiResponse } from 'next';
|
||||
|
||||
interface CreateCommentRequest extends UserExtendedNextApiRequest {
|
||||
@@ -36,10 +36,16 @@ const createComment = async (
|
||||
});
|
||||
};
|
||||
|
||||
const handler = nextConnect(NextConnectConfig).post(
|
||||
const router = createRouter<
|
||||
CreateCommentRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.post(
|
||||
validateRequest({ bodySchema: BeerCommentValidationSchema }),
|
||||
getCurrentUser,
|
||||
createComment,
|
||||
);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
export default handler;
|
||||
|
||||
77
pages/api/beers/[id]/images/index.ts
Normal file
77
pages/api/beers/[id]/images/index.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import { NextHandler, createRouter, expressWrapper } from 'next-connect';
|
||||
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
||||
|
||||
import multer from 'multer';
|
||||
|
||||
import cloudinaryConfig from '@/config/cloudinary';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { z } from 'zod';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
|
||||
const { storage } = cloudinaryConfig;
|
||||
|
||||
const fileFilter: multer.Options['fileFilter'] = (req, file, cb) => {
|
||||
if (
|
||||
file.mimetype === 'image/png' ||
|
||||
file.mimetype === 'image/jpg' ||
|
||||
file.mimetype === 'image/jpeg'
|
||||
) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(null, false);
|
||||
}
|
||||
};
|
||||
|
||||
const uploadMiddleware = multer({ storage, fileFilter }).array('images');
|
||||
|
||||
interface UploadBeerPostImagesRequest extends UserExtendedNextApiRequest {
|
||||
files?:
|
||||
| Express.Multer.File[]
|
||||
| {
|
||||
[fieldname: string]: Express.Multer.File[];
|
||||
};
|
||||
|
||||
query: {
|
||||
id: string;
|
||||
};
|
||||
|
||||
// beerPost?: BeerPostQueryResult;
|
||||
}
|
||||
|
||||
const checkIfBeerPostOwner = async (
|
||||
req: UploadBeerPostImagesRequest,
|
||||
res: NextApiResponse,
|
||||
next: NextHandler,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
const user = req.user!;
|
||||
const beerPost = await getBeerPostById(id);
|
||||
|
||||
if (!beerPost) {
|
||||
throw new ServerError('Beer post not found', 404);
|
||||
}
|
||||
|
||||
if (beerPost.postedBy.id !== user.id) {
|
||||
throw new ServerError('You are not authorized to edit this beer post', 403);
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
const router = createRouter<
|
||||
UploadBeerPostImagesRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
// @ts-expect-error
|
||||
router.post(getCurrentUser, expressWrapper(uploadMiddleware), checkIfBeerPostOwner);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
export default handler;
|
||||
|
||||
export const config = { api: { bodyParser: false } };
|
||||
@@ -1,16 +1,16 @@
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import validateRequest from '@/config/zod/middleware/validateRequest';
|
||||
import getCurrentUser from '@/config/auth/middleware/getCurrentUser';
|
||||
import NextConnectConfig from '@/config/nextConnect/NextConnectConfig';
|
||||
import nextConnect from 'next-connect';
|
||||
import { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import { NextApiResponse } from 'next';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import createBeerPostLike from '@/services/BeerPostLike/createBeerPostLike';
|
||||
import removeBeerPostLikeById from '@/services/BeerPostLike/removeBeerPostLikeById';
|
||||
import findBeerPostLikeById from '@/services/BeerPostLike/findBeerPostLikeById';
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||
|
||||
const sendLikeRequest = async (
|
||||
req: UserExtendedNextApiRequest,
|
||||
@@ -43,7 +43,12 @@ const sendLikeRequest = async (
|
||||
res.status(200).json(jsonResponse);
|
||||
};
|
||||
|
||||
const handler = nextConnect(NextConnectConfig).post(
|
||||
const router = createRouter<
|
||||
UserExtendedNextApiRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.post(
|
||||
getCurrentUser,
|
||||
validateRequest({
|
||||
querySchema: z.object({
|
||||
@@ -53,4 +58,5 @@ const handler = nextConnect(NextConnectConfig).post(
|
||||
sendLikeRequest,
|
||||
);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
export default handler;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import getCurrentUser from '@/config/auth/middleware/getCurrentUser';
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import NextConnectConfig from '@/config/nextConnect/NextConnectConfig';
|
||||
import validateRequest from '@/config/zod/middleware/validateRequest';
|
||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import nextConnect from 'next-connect';
|
||||
import { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
|
||||
const checkIfLiked = async (
|
||||
@@ -30,10 +30,20 @@ const checkIfLiked = async (
|
||||
});
|
||||
};
|
||||
|
||||
const handler = nextConnect(NextConnectConfig).get(
|
||||
const router = createRouter<
|
||||
UserExtendedNextApiRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.get(
|
||||
getCurrentUser,
|
||||
validateRequest({ querySchema: z.object({ id: z.string().uuid() }) }),
|
||||
validateRequest({
|
||||
querySchema: z.object({
|
||||
id: z.string().uuid(),
|
||||
}),
|
||||
}),
|
||||
checkIfLiked,
|
||||
);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
export default handler;
|
||||
|
||||
Reference in New Issue
Block a user