mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Refactor: move service logic out of api routes and into separate files
This commit is contained in:
@@ -2,25 +2,20 @@ import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
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 { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import findBeerPostLikeById from '@/services/BeerPostLike/findBeerPostLikeById';
|
||||
|
||||
const checkIfLiked = async (
|
||||
req: UserExtendedNextApiRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const user = req.user!;
|
||||
const id = req.query.id as string;
|
||||
const beerPostId = req.query.id as string;
|
||||
|
||||
const alreadyLiked = await DBClient.instance.beerPostLike.findFirst({
|
||||
where: {
|
||||
beerPostId: id,
|
||||
likedById: user.id,
|
||||
},
|
||||
});
|
||||
const alreadyLiked = await findBeerPostLikeById({ beerPostId, likedById: user.id });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
@@ -37,11 +32,7 @@ const router = createRouter<
|
||||
|
||||
router.get(
|
||||
getCurrentUser,
|
||||
validateRequest({
|
||||
querySchema: z.object({
|
||||
id: z.string().uuid(),
|
||||
}),
|
||||
}),
|
||||
validateRequest({ querySchema: z.object({ id: z.string().uuid() }) }),
|
||||
checkIfLiked,
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user