mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Extract beer post, beer image, and beer like controller logic
This commit is contained in:
34
src/controllers/beerImages/index.ts
Normal file
34
src/controllers/beerImages/index.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import ServerError from '@/config/util/ServerError';
|
||||||
|
import addBeerImageToDB from '@/services/BeerImage/addBeerImageToDB';
|
||||||
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
|
import { NextApiResponse } from 'next';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { UploadBeerPostImagesRequest } from './types';
|
||||||
|
|
||||||
|
// eslint-disable-next-line import/prefer-default-export
|
||||||
|
export const processBeerImageData = async (
|
||||||
|
req: UploadBeerPostImagesRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const { files, user, body } = req;
|
||||||
|
|
||||||
|
if (!files || !files.length) {
|
||||||
|
throw new ServerError('No images uploaded', 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
const beerImages = await addBeerImageToDB({
|
||||||
|
alt: body.alt,
|
||||||
|
caption: body.caption,
|
||||||
|
beerPostId: req.query.id,
|
||||||
|
userId: user!.id,
|
||||||
|
files,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: `Successfully uploaded ${beerImages.length} image${
|
||||||
|
beerImages.length > 1 ? 's' : ''
|
||||||
|
}`,
|
||||||
|
statusCode: 200,
|
||||||
|
});
|
||||||
|
};
|
||||||
9
src/controllers/beerImages/types/index.ts
Normal file
9
src/controllers/beerImages/types/index.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||||
|
import ImageMetadataValidationSchema from '@/services/schema/ImageSchema/ImageMetadataValidationSchema';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
export interface UploadBeerPostImagesRequest extends UserExtendedNextApiRequest {
|
||||||
|
files?: Express.Multer.File[];
|
||||||
|
query: { id: string };
|
||||||
|
body: z.infer<typeof ImageMetadataValidationSchema>;
|
||||||
|
}
|
||||||
77
src/controllers/beerPostLikes/index.ts
Normal file
77
src/controllers/beerPostLikes/index.ts
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import { UserExtendedNextApiRequest } from "@/config/auth/types";
|
||||||
|
import ServerError from "@/config/util/ServerError";
|
||||||
|
import getBeerPostById from "@/services/BeerPost/getBeerPostById";
|
||||||
|
import createBeerPostLike from "@/services/BeerPostLike/createBeerPostLike";
|
||||||
|
import findBeerPostLikeById from "@/services/BeerPostLike/findBeerPostLikeById";
|
||||||
|
import getBeerPostLikeCount from "@/services/BeerPostLike/getBeerPostLikeCount";
|
||||||
|
import removeBeerPostLikeById from "@/services/BeerPostLike/removeBeerPostLikeById";
|
||||||
|
import APIResponseValidationSchema from "@/validation/APIResponseValidationSchema";
|
||||||
|
import { NextApiResponse, NextApiRequest } from "next";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const sendLikeRequest = async (
|
||||||
|
req: UserExtendedNextApiRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const user = req.user!;
|
||||||
|
const id = req.query.id as string;
|
||||||
|
|
||||||
|
const beer = await getBeerPostById(id);
|
||||||
|
if (!beer) {
|
||||||
|
throw new ServerError('Could not find a beer post with that id', 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
const alreadyLiked = await findBeerPostLikeById({
|
||||||
|
beerPostId: beer.id,
|
||||||
|
likedById: user.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
const jsonResponse = {
|
||||||
|
success: true as const,
|
||||||
|
message: '',
|
||||||
|
statusCode: 200 as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (alreadyLiked) {
|
||||||
|
await removeBeerPostLikeById({ beerLikeId: alreadyLiked.id });
|
||||||
|
jsonResponse.message = 'Successfully unliked beer post';
|
||||||
|
} else {
|
||||||
|
await createBeerPostLike({ id, user });
|
||||||
|
jsonResponse.message = 'Successfully liked beer post';
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json(jsonResponse);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getLikeCount = async (
|
||||||
|
req: NextApiRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const id = req.query.id as string;
|
||||||
|
|
||||||
|
const likeCount = await getBeerPostLikeCount({ beerPostId: id });
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: 'Successfully retrieved like count.',
|
||||||
|
statusCode: 200,
|
||||||
|
payload: { likeCount },
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const checkIfLiked = async (
|
||||||
|
req: UserExtendedNextApiRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const user = req.user!;
|
||||||
|
const beerPostId = req.query.id as string;
|
||||||
|
|
||||||
|
const alreadyLiked = await findBeerPostLikeById({ beerPostId, likedById: user.id });
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: alreadyLiked ? 'Beer post is liked.' : 'Beer post is not liked.',
|
||||||
|
statusCode: 200,
|
||||||
|
payload: { isLiked: !!alreadyLiked },
|
||||||
|
});
|
||||||
|
};
|
||||||
143
src/controllers/beerPosts/index.ts
Normal file
143
src/controllers/beerPosts/index.ts
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
import ServerError from '@/config/util/ServerError';
|
||||||
|
import deleteBeerPostById from '@/services/BeerPost/deleteBeerPostById';
|
||||||
|
import editBeerPostById from '@/services/BeerPost/editBeerPostById';
|
||||||
|
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
||||||
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
|
import { NextApiResponse } from 'next';
|
||||||
|
import { NextHandler } from 'next-connect';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import getBeerRecommendations from '@/services/BeerPost/getBeerRecommendations';
|
||||||
|
import getAllBeerPosts from '@/services/BeerPost/getAllBeerPosts';
|
||||||
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
import createNewBeerPost from '@/services/BeerPost/createNewBeerPost';
|
||||||
|
import {
|
||||||
|
BeerPostRequest,
|
||||||
|
CreateBeerPostRequest,
|
||||||
|
EditBeerPostRequest,
|
||||||
|
GetAllBeerPostsRequest,
|
||||||
|
GetBeerRecommendationsRequest,
|
||||||
|
} from './types';
|
||||||
|
|
||||||
|
export const checkIfBeerPostOwner = async <BeerPostRequestType extends BeerPostRequest>(
|
||||||
|
req: BeerPostRequestType,
|
||||||
|
res: NextApiResponse,
|
||||||
|
next: NextHandler,
|
||||||
|
) => {
|
||||||
|
const { user, query } = req;
|
||||||
|
const { id } = query;
|
||||||
|
|
||||||
|
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 cannot edit that beer post.', 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
return next();
|
||||||
|
};
|
||||||
|
|
||||||
|
export const editBeerPost = async (
|
||||||
|
req: EditBeerPostRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
await editBeerPostById({ id: req.query.id, data: req.body });
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Beer post updated successfully',
|
||||||
|
success: true,
|
||||||
|
statusCode: 200,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
|
||||||
|
const { id } = req.query;
|
||||||
|
|
||||||
|
const deleted = await deleteBeerPostById({ beerPostId: id });
|
||||||
|
if (!deleted) {
|
||||||
|
throw new ServerError('Beer post not found', 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Beer post deleted successfully',
|
||||||
|
success: true,
|
||||||
|
statusCode: 200,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getBeerPostRecommendations = async (
|
||||||
|
req: GetBeerRecommendationsRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const { id } = req.query;
|
||||||
|
|
||||||
|
const beerPost = await getBeerPostById(id);
|
||||||
|
|
||||||
|
if (!beerPost) {
|
||||||
|
throw new ServerError('Beer post not found', 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
const pageNum = parseInt(req.query.page_num as string, 10);
|
||||||
|
const pageSize = parseInt(req.query.page_size as string, 10);
|
||||||
|
|
||||||
|
const { count, beerRecommendations } = await getBeerRecommendations({
|
||||||
|
beerPost,
|
||||||
|
pageNum,
|
||||||
|
pageSize,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.setHeader('X-Total-Count', count);
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: 'Recommendations fetched successfully',
|
||||||
|
statusCode: 200,
|
||||||
|
payload: beerRecommendations,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getBeerPosts = async (
|
||||||
|
req: GetAllBeerPostsRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const pageNum = parseInt(req.query.page_num, 10);
|
||||||
|
const pageSize = parseInt(req.query.page_size, 10);
|
||||||
|
|
||||||
|
const beerPosts = await getAllBeerPosts({ pageNum, pageSize });
|
||||||
|
|
||||||
|
const beerPostCount = await DBClient.instance.beerPost.count();
|
||||||
|
|
||||||
|
res.setHeader('X-Total-Count', beerPostCount);
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Beer posts retrieved successfully',
|
||||||
|
statusCode: 200,
|
||||||
|
payload: beerPosts,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createBeerPost = async (
|
||||||
|
req: CreateBeerPostRequest,
|
||||||
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
|
) => {
|
||||||
|
const { name, description, styleId: typeId, abv, ibu, breweryId } = req.body;
|
||||||
|
|
||||||
|
const newBeerPost = await createNewBeerPost({
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
abv,
|
||||||
|
ibu,
|
||||||
|
styleId: typeId,
|
||||||
|
breweryId,
|
||||||
|
userId: req.user!.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(201).json({
|
||||||
|
message: 'Beer post created successfully',
|
||||||
|
statusCode: 201,
|
||||||
|
payload: newBeerPost,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
25
src/controllers/beerPosts/types/index.ts
Normal file
25
src/controllers/beerPosts/types/index.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||||
|
import CreateBeerPostValidationSchema from '@/services/BeerPost/schema/CreateBeerPostValidationSchema';
|
||||||
|
import EditBeerPostValidationSchema from '@/services/BeerPost/schema/EditBeerPostValidationSchema';
|
||||||
|
import { NextApiRequest } from 'next';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
export interface BeerPostRequest extends UserExtendedNextApiRequest {
|
||||||
|
query: { id: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface EditBeerPostRequest extends BeerPostRequest {
|
||||||
|
body: z.infer<typeof EditBeerPostValidationSchema>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GetAllBeerPostsRequest extends NextApiRequest {
|
||||||
|
query: { page_num: string; page_size: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GetBeerRecommendationsRequest extends BeerPostRequest {
|
||||||
|
query: { id: string; page_num: string; page_size: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CreateBeerPostRequest extends UserExtendedNextApiRequest {
|
||||||
|
body: z.infer<typeof CreateBeerPostValidationSchema>;
|
||||||
|
}
|
||||||
@@ -1,50 +1,16 @@
|
|||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
|
|
||||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
|
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import ServerError from '@/config/util/ServerError';
|
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import addBeerImageToDB from '@/services/BeerImage/addBeerImageToDB';
|
|
||||||
import ImageMetadataValidationSchema from '@/services/schema/ImageSchema/ImageMetadataValidationSchema';
|
import ImageMetadataValidationSchema from '@/services/schema/ImageSchema/ImageMetadataValidationSchema';
|
||||||
import { uploadMiddlewareMultiple } from '@/config/multer/uploadMiddleware';
|
import { uploadMiddlewareMultiple } from '@/config/multer/uploadMiddleware';
|
||||||
|
import { UploadBeerPostImagesRequest } from '@/controllers/beerImages/types';
|
||||||
interface UploadBeerPostImagesRequest extends UserExtendedNextApiRequest {
|
import { processBeerImageData } from '@/controllers/beerImages';
|
||||||
files?: Express.Multer.File[];
|
|
||||||
query: { id: string };
|
|
||||||
body: z.infer<typeof ImageMetadataValidationSchema>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const processImageData = async (
|
|
||||||
req: UploadBeerPostImagesRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const { files, user, body } = req;
|
|
||||||
|
|
||||||
if (!files || !files.length) {
|
|
||||||
throw new ServerError('No images uploaded', 400);
|
|
||||||
}
|
|
||||||
|
|
||||||
const beerImages = await addBeerImageToDB({
|
|
||||||
alt: body.alt,
|
|
||||||
caption: body.caption,
|
|
||||||
beerPostId: req.query.id,
|
|
||||||
userId: user!.id,
|
|
||||||
files,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: `Successfully uploaded ${beerImages.length} image${
|
|
||||||
beerImages.length > 1 ? 's' : ''
|
|
||||||
}`,
|
|
||||||
statusCode: 200,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
UploadBeerPostImagesRequest,
|
UploadBeerPostImagesRequest,
|
||||||
@@ -56,7 +22,7 @@ router.post(
|
|||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
uploadMiddlewareMultiple,
|
uploadMiddlewareMultiple,
|
||||||
validateRequest({ bodySchema: ImageMetadataValidationSchema }),
|
validateRequest({ bodySchema: ImageMetadataValidationSchema }),
|
||||||
processImageData,
|
processBeerImageData,
|
||||||
);
|
);
|
||||||
|
|
||||||
const handler = router.handler(NextConnectOptions);
|
const handler = router.handler(NextConnectOptions);
|
||||||
|
|||||||
@@ -1,73 +1,21 @@
|
|||||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
|
||||||
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
|
||||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
|
||||||
import editBeerPostById from '@/services/BeerPost/editBeerPostById';
|
|
||||||
import EditBeerPostValidationSchema from '@/services/BeerPost/schema/EditBeerPostValidationSchema';
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { createRouter, NextHandler } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import ServerError from '@/config/util/ServerError';
|
|
||||||
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
import deleteBeerPostById from '@/services/BeerPost/deleteBeerPostById';
|
|
||||||
|
|
||||||
interface BeerPostRequest extends UserExtendedNextApiRequest {
|
import { EditBeerPostRequest } from '@/controllers/beerPosts/types';
|
||||||
query: { id: string };
|
import {
|
||||||
}
|
checkIfBeerPostOwner,
|
||||||
|
editBeerPost,
|
||||||
|
deleteBeerPost,
|
||||||
|
} from '@/controllers/beerPosts';
|
||||||
|
|
||||||
interface EditBeerPostRequest extends BeerPostRequest {
|
import EditBeerPostValidationSchema from '@/services/BeerPost/schema/EditBeerPostValidationSchema';
|
||||||
body: z.infer<typeof EditBeerPostValidationSchema>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const checkIfBeerPostOwner = async <BeerPostRequestType extends BeerPostRequest>(
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
req: BeerPostRequestType,
|
|
||||||
res: NextApiResponse,
|
|
||||||
next: NextHandler,
|
|
||||||
) => {
|
|
||||||
const { user, query } = req;
|
|
||||||
const { id } = query;
|
|
||||||
|
|
||||||
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 cannot edit that beer post.', 403);
|
|
||||||
}
|
|
||||||
|
|
||||||
return next();
|
|
||||||
};
|
|
||||||
|
|
||||||
const editBeerPost = async (
|
|
||||||
req: EditBeerPostRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
await editBeerPostById({ id: req.query.id, data: req.body });
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
message: 'Beer post updated successfully',
|
|
||||||
success: true,
|
|
||||||
statusCode: 200,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
|
|
||||||
const { id } = req.query;
|
|
||||||
|
|
||||||
const deleted = await deleteBeerPostById({ beerPostId: id });
|
|
||||||
if (!deleted) {
|
|
||||||
throw new ServerError('Beer post not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
message: 'Beer post deleted successfully',
|
|
||||||
success: true,
|
|
||||||
statusCode: 200,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
EditBeerPostRequest,
|
EditBeerPostRequest,
|
||||||
|
|||||||
@@ -1,67 +1,12 @@
|
|||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
|
||||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
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 getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
import getBeerPostLikeCount from '@/services/BeerPostLike/getBeerPostLikeCount';
|
import { sendLikeRequest, getLikeCount } from '@/controllers/beerPostLikes';
|
||||||
|
|
||||||
const sendLikeRequest = async (
|
|
||||||
req: UserExtendedNextApiRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const user = req.user!;
|
|
||||||
const id = req.query.id as string;
|
|
||||||
|
|
||||||
const beer = await getBeerPostById(id);
|
|
||||||
if (!beer) {
|
|
||||||
throw new ServerError('Could not find a beer post with that id', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
const alreadyLiked = await findBeerPostLikeById({
|
|
||||||
beerPostId: beer.id,
|
|
||||||
likedById: user.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
const jsonResponse = {
|
|
||||||
success: true as const,
|
|
||||||
message: '',
|
|
||||||
statusCode: 200 as const,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (alreadyLiked) {
|
|
||||||
await removeBeerPostLikeById({ beerLikeId: alreadyLiked.id });
|
|
||||||
jsonResponse.message = 'Successfully unliked beer post';
|
|
||||||
} else {
|
|
||||||
await createBeerPostLike({ id, user });
|
|
||||||
jsonResponse.message = 'Successfully liked beer post';
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).json(jsonResponse);
|
|
||||||
};
|
|
||||||
|
|
||||||
const getLikeCount = async (
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const id = req.query.id as string;
|
|
||||||
|
|
||||||
const likeCount = await getBeerPostLikeCount({ beerPostId: id });
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: 'Successfully retrieved like count.',
|
|
||||||
statusCode: 200,
|
|
||||||
payload: { likeCount },
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
UserExtendedNextApiRequest,
|
UserExtendedNextApiRequest,
|
||||||
|
|||||||
@@ -6,24 +6,8 @@ import APIResponseValidationSchema from '@/validation/APIResponseValidationSchem
|
|||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import findBeerPostLikeById from '@/services/BeerPostLike/findBeerPostLikeById';
|
|
||||||
|
|
||||||
const checkIfLiked = async (
|
import { checkIfLiked } from '@/controllers/beerPostLikes';
|
||||||
req: UserExtendedNextApiRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const user = req.user!;
|
|
||||||
const beerPostId = req.query.id as string;
|
|
||||||
|
|
||||||
const alreadyLiked = await findBeerPostLikeById({ beerPostId, likedById: user.id });
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: alreadyLiked ? 'Beer post is liked.' : 'Beer post is not liked.',
|
|
||||||
statusCode: 200,
|
|
||||||
payload: { isLiked: !!alreadyLiked },
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
UserExtendedNextApiRequest,
|
UserExtendedNextApiRequest,
|
||||||
|
|||||||
@@ -1,52 +1,17 @@
|
|||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import ServerError from '@/config/util/ServerError';
|
import { getBeerPostRecommendations } from '@/controllers/beerPosts';
|
||||||
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
import { GetBeerRecommendationsRequest } from '@/controllers/beerPosts/types';
|
||||||
import getBeerRecommendations from '@/services/BeerPost/getBeerRecommendations';
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
interface BeerPostRequest extends NextApiRequest {
|
|
||||||
query: { id: string; page_num: string; page_size: string };
|
|
||||||
}
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
BeerPostRequest,
|
GetBeerRecommendationsRequest,
|
||||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||||
>();
|
>();
|
||||||
|
|
||||||
const getBeerRecommendationsRequest = async (
|
|
||||||
req: BeerPostRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const { id } = req.query;
|
|
||||||
|
|
||||||
const beerPost = await getBeerPostById(id);
|
|
||||||
|
|
||||||
if (!beerPost) {
|
|
||||||
throw new ServerError('Beer post not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
const pageNum = parseInt(req.query.page_num as string, 10);
|
|
||||||
const pageSize = parseInt(req.query.page_size as string, 10);
|
|
||||||
|
|
||||||
const { count, beerRecommendations } = await getBeerRecommendations({
|
|
||||||
beerPost,
|
|
||||||
pageNum,
|
|
||||||
pageSize,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', count);
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: 'Recommendations fetched successfully',
|
|
||||||
statusCode: 200,
|
|
||||||
payload: beerRecommendations,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
validateRequest({
|
validateRequest({
|
||||||
querySchema: z.object({
|
querySchema: z.object({
|
||||||
@@ -55,7 +20,7 @@ router.get(
|
|||||||
page_size: z.string().regex(/^[0-9]+$/),
|
page_size: z.string().regex(/^[0-9]+$/),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
getBeerRecommendationsRequest,
|
getBeerPostRecommendations,
|
||||||
);
|
);
|
||||||
|
|
||||||
const handler = router.handler(NextConnectOptions);
|
const handler = router.handler(NextConnectOptions);
|
||||||
|
|||||||
@@ -1,41 +1,14 @@
|
|||||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import createNewBeerPost from '@/services/BeerPost/createNewBeerPost';
|
|
||||||
import CreateBeerPostValidationSchema from '@/services/BeerPost/schema/CreateBeerPostValidationSchema';
|
import CreateBeerPostValidationSchema from '@/services/BeerPost/schema/CreateBeerPostValidationSchema';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
|
import { createBeerPost } from '@/controllers/beerPosts';
|
||||||
interface CreateBeerPostRequest extends UserExtendedNextApiRequest {
|
import { CreateBeerPostRequest } from '@/controllers/beerPosts/types';
|
||||||
body: z.infer<typeof CreateBeerPostValidationSchema>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const createBeerPost = async (
|
|
||||||
req: CreateBeerPostRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const { name, description, styleId: typeId, abv, ibu, breweryId } = req.body;
|
|
||||||
|
|
||||||
const newBeerPost = await createNewBeerPost({
|
|
||||||
name,
|
|
||||||
description,
|
|
||||||
abv,
|
|
||||||
ibu,
|
|
||||||
styleId: typeId,
|
|
||||||
breweryId,
|
|
||||||
userId: req.user!.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
message: 'Beer post created successfully',
|
|
||||||
statusCode: 201,
|
|
||||||
payload: newBeerPost,
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
CreateBeerPostRequest,
|
CreateBeerPostRequest,
|
||||||
|
|||||||
@@ -1,40 +1,15 @@
|
|||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import DBClient from '@/prisma/DBClient';
|
import { getBeerPosts } from '@/controllers/beerPosts';
|
||||||
import getAllBeerPosts from '@/services/BeerPost/getAllBeerPosts';
|
import { GetAllBeerPostsRequest } from '@/controllers/beerPosts/types';
|
||||||
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
|
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
|
||||||
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { createRouter } from 'next-connect';
|
import { createRouter } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
interface GetBeerPostsRequest extends NextApiRequest {
|
|
||||||
query: z.infer<typeof PaginatedQueryResponseSchema>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const getBeerPosts = async (
|
|
||||||
req: GetBeerPostsRequest,
|
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
|
||||||
) => {
|
|
||||||
const pageNum = parseInt(req.query.page_num, 10);
|
|
||||||
const pageSize = parseInt(req.query.page_size, 10);
|
|
||||||
|
|
||||||
const beerPosts = await getAllBeerPosts({ pageNum, pageSize });
|
|
||||||
|
|
||||||
const beerPostCount = await DBClient.instance.beerPost.count();
|
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', beerPostCount);
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
message: 'Beer posts retrieved successfully',
|
|
||||||
statusCode: 200,
|
|
||||||
payload: beerPosts,
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const router = createRouter<
|
const router = createRouter<
|
||||||
GetBeerPostsRequest,
|
GetAllBeerPostsRequest,
|
||||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||||
>();
|
>();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user