mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Refactor: further extract controller logic from routers
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
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";
|
||||
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 getBeerPostLikeCountByBeerPostId from '@/services/BeerPostLike/getBeerPostLikeCount';
|
||||
import removeBeerPostLikeById from '@/services/BeerPostLike/removeBeerPostLikeById';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse, NextApiRequest } from 'next';
|
||||
import { z } from 'zod';
|
||||
import { LikeRequest } from '../types';
|
||||
|
||||
export const sendLikeRequest = async (
|
||||
req: UserExtendedNextApiRequest,
|
||||
export const sendBeerPostLikeRequest = async (
|
||||
req: LikeRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const user = req.user!;
|
||||
@@ -43,13 +44,13 @@ export const sendLikeRequest = async (
|
||||
res.status(200).json(jsonResponse);
|
||||
};
|
||||
|
||||
export const getLikeCount = async (
|
||||
export const getBeerPostLikeCount = async (
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const id = req.query.id as string;
|
||||
|
||||
const likeCount = await getBeerPostLikeCount({ beerPostId: id });
|
||||
const likeCount = await getBeerPostLikeCountByBeerPostId({ beerPostId: id });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
@@ -59,7 +60,7 @@ export const getLikeCount = async (
|
||||
});
|
||||
};
|
||||
|
||||
export const checkIfLiked = async (
|
||||
export const checkIfBeerPostIsLiked = async (
|
||||
req: UserExtendedNextApiRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
76
src/controllers/likes/beerStyleLikes/index.ts
Normal file
76
src/controllers/likes/beerStyleLikes/index.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import createBeerStyleLike from '@/services/BeerStyleLike/createBeerStyleLike';
|
||||
import findBeerStyleLikeById from '@/services/BeerStyleLike/findBeerStyleLikeById';
|
||||
import getBeerStyleLikeCount from '@/services/BeerStyleLike/getBeerStyleLikeCount';
|
||||
import removeBeerStyleLikeById from '@/services/BeerStyleLike/removeBeerStyleLikeById';
|
||||
import getBeerStyleById from '@/services/BeerStyles/getBeerStyleById';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse, NextApiRequest } from 'next';
|
||||
import { z } from 'zod';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import { LikeRequest } from '../types';
|
||||
|
||||
export const sendBeerStyleLikeRequest = async (
|
||||
req: LikeRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const user = req.user!;
|
||||
const { id } = req.query;
|
||||
|
||||
const beerStyle = await getBeerStyleById(id);
|
||||
if (!beerStyle) {
|
||||
throw new ServerError('Could not find a beer style with that id.', 404);
|
||||
}
|
||||
|
||||
const beerStyleLike = await findBeerStyleLikeById({
|
||||
beerStyleId: beerStyle.id,
|
||||
likedById: user.id,
|
||||
});
|
||||
|
||||
if (beerStyleLike) {
|
||||
await removeBeerStyleLikeById({ beerStyleLikeId: beerStyleLike.id });
|
||||
res.status(200).json({
|
||||
message: 'Successfully unliked beer style.',
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
});
|
||||
} else {
|
||||
await createBeerStyleLike({ beerStyleId: beerStyle.id, user });
|
||||
res.status(200).json({
|
||||
message: 'Successfully liked beer style.',
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getBeerStyleLikeCountRequest = async (
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const id = req.query.id as string;
|
||||
const likeCount = await getBeerStyleLikeCount({ beerStyleId: id });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Successfully retrieved like count.',
|
||||
statusCode: 200,
|
||||
payload: { likeCount },
|
||||
});
|
||||
};
|
||||
|
||||
export const checkIfBeerStyleIsLiked = async (
|
||||
req: UserExtendedNextApiRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const user = req.user!;
|
||||
const beerStyleId = req.query.id as string;
|
||||
|
||||
const alreadyLiked = await findBeerStyleLikeById({ beerStyleId, likedById: user.id });
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: alreadyLiked ? 'Beer style is liked.' : 'Beer style is not liked.',
|
||||
statusCode: 200,
|
||||
payload: { isLiked: !!alreadyLiked },
|
||||
});
|
||||
};
|
||||
5
src/controllers/likes/types/index.ts
Normal file
5
src/controllers/likes/types/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
|
||||
export interface LikeRequest extends UserExtendedNextApiRequest {
|
||||
query: { id: string };
|
||||
}
|
||||
116
src/controllers/posts/beerStyles/index.ts
Normal file
116
src/controllers/posts/beerStyles/index.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { NextApiResponse } from 'next';
|
||||
|
||||
import { z } from 'zod';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
|
||||
import getBeerStyleById from '@/services/BeerStyles/getBeerStyleById';
|
||||
import getBeerPostsByBeerStyleId from '@/services/BeerPost/getBeerPostsByBeerStyleId';
|
||||
import getAllBeerStyles from '@/services/BeerStyles/getAllBeerStyles';
|
||||
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import {
|
||||
CreateBeerStyleRequest,
|
||||
GetAllBeersByBeerStyleRequest,
|
||||
GetBeerStyleByIdRequest,
|
||||
} from './types';
|
||||
|
||||
import { GetAllPostsRequest } from '../types';
|
||||
|
||||
export const getBeerStyle = async (
|
||||
req: GetBeerStyleByIdRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { id } = req.query;
|
||||
|
||||
const beerStyle = await getBeerStyleById(id);
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Beer style retrieved successfully.',
|
||||
statusCode: 200,
|
||||
payload: beerStyle,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
export const getAllBeersByBeerStyle = async (
|
||||
req: GetAllBeersByBeerStyleRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
const { page_size, page_num, id } = req.query;
|
||||
|
||||
const beers = await getBeerPostsByBeerStyleId({
|
||||
pageNum: parseInt(page_num, 10),
|
||||
pageSize: parseInt(page_size, 10),
|
||||
styleId: id,
|
||||
});
|
||||
|
||||
const count = await DBClient.instance.beerPost.count({ where: { styleId: id } });
|
||||
|
||||
res.setHeader('X-Total-Count', count);
|
||||
|
||||
res.status(200).json({
|
||||
message: `Beers with style id ${id} retrieved successfully.`,
|
||||
statusCode: 200,
|
||||
payload: beers,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
export const getBeerStyles = async (
|
||||
req: GetAllPostsRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const pageNum = parseInt(req.query.page_num, 10);
|
||||
const pageSize = parseInt(req.query.page_size, 10);
|
||||
|
||||
const beerStyles = await getAllBeerStyles({ pageNum, pageSize });
|
||||
const beerStyleCount = await DBClient.instance.beerStyle.count();
|
||||
|
||||
res.setHeader('X-Total-Count', beerStyleCount);
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Beer styles retrieved successfully.',
|
||||
statusCode: 200,
|
||||
payload: beerStyles,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
export const createBeerStyle = async (
|
||||
req: CreateBeerStyleRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { abvRange, description, glasswareId, ibuRange, name } = req.body;
|
||||
|
||||
const user = req.user!;
|
||||
|
||||
const glassware = await DBClient.instance.glassware.findUnique({
|
||||
where: { id: glasswareId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!glassware) {
|
||||
throw new ServerError('Glassware not found.', 404);
|
||||
}
|
||||
|
||||
const beerStyle = await DBClient.instance.beerStyle.create({
|
||||
data: {
|
||||
abvRange,
|
||||
description,
|
||||
glassware: { connect: { id: glasswareId } },
|
||||
ibuRange,
|
||||
name,
|
||||
postedBy: { connect: { id: user.id } },
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: 'Beer style created successfully.',
|
||||
statusCode: 200,
|
||||
payload: beerStyle,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
17
src/controllers/posts/beerStyles/types/index.ts
Normal file
17
src/controllers/posts/beerStyles/types/index.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { NextApiRequest } from 'next';
|
||||
|
||||
import { GetAllPostsRequest } from '@/controllers/posts/types';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import { z } from 'zod';
|
||||
import CreateBeerStyleValidationSchema from '@/services/BeerStyles/schema/CreateBeerStyleValidationSchema';
|
||||
|
||||
export interface GetBeerStyleByIdRequest extends NextApiRequest {
|
||||
query: { id: string };
|
||||
}
|
||||
|
||||
export interface GetAllBeersByBeerStyleRequest extends GetAllPostsRequest {
|
||||
query: { page_size: string; page_num: string; id: string };
|
||||
}
|
||||
export interface CreateBeerStyleRequest extends UserExtendedNextApiRequest {
|
||||
body: z.infer<typeof CreateBeerStyleValidationSchema>;
|
||||
}
|
||||
5
src/controllers/posts/types/index.ts
Normal file
5
src/controllers/posts/types/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { NextApiRequest } from 'next';
|
||||
|
||||
export interface GetAllPostsRequest extends NextApiRequest {
|
||||
query: { page_size: string; page_num: string };
|
||||
}
|
||||
Reference in New Issue
Block a user