mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Refactor: update like services
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import {
|
||||
createBreweryPostLikeService,
|
||||
findBreweryPostLikeService,
|
||||
getBreweryPostLikeCountService,
|
||||
removeBreweryPostLikeService,
|
||||
} from '@/services/likes/brewery-post-like';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse, NextApiRequest } from 'next';
|
||||
import { z } from 'zod';
|
||||
@@ -20,29 +26,31 @@ export const sendBreweryPostLikeRequest = async (
|
||||
throw new ServerError('Could not find a brewery post with that id', 404);
|
||||
}
|
||||
|
||||
const alreadyLiked = await DBClient.instance.breweryPostLike.findFirst({
|
||||
where: { breweryPostId: breweryPost.id, likedById: user.id },
|
||||
const like = await findBreweryPostLikeService({
|
||||
breweryPostId: breweryPost.id,
|
||||
likedById: user.id,
|
||||
});
|
||||
|
||||
const jsonResponse = {
|
||||
success: true as const,
|
||||
message: '',
|
||||
statusCode: 200 as const,
|
||||
};
|
||||
if (like) {
|
||||
await removeBreweryPostLikeService({ breweryPostLikeId: like.id });
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Successfully removed like from brewery post.',
|
||||
statusCode: 200,
|
||||
});
|
||||
|
||||
if (alreadyLiked) {
|
||||
await DBClient.instance.breweryPostLike.delete({
|
||||
where: { id: alreadyLiked.id },
|
||||
});
|
||||
jsonResponse.message = 'Successfully unliked brewery post';
|
||||
} else {
|
||||
await DBClient.instance.breweryPostLike.create({
|
||||
data: { breweryPostId: breweryPost.id, likedById: user.id },
|
||||
});
|
||||
jsonResponse.message = 'Successfully liked brewery post';
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).json(jsonResponse);
|
||||
await createBreweryPostLikeService({
|
||||
breweryPostId: breweryPost.id,
|
||||
likedById: user.id,
|
||||
});
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Successfully liked brewery post.',
|
||||
statusCode: 200,
|
||||
});
|
||||
};
|
||||
|
||||
export const getBreweryPostLikeCount = async (
|
||||
@@ -59,8 +67,8 @@ export const getBreweryPostLikeCount = async (
|
||||
throw new ServerError('Could not find a brewery post with that id', 404);
|
||||
}
|
||||
|
||||
const likeCount = await DBClient.instance.breweryPostLike.count({
|
||||
where: { breweryPostId: breweryPost.id },
|
||||
const likeCount = await getBreweryPostLikeCountService({
|
||||
breweryPostId: breweryPost.id,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
@@ -78,17 +86,15 @@ export const getBreweryPostLikeStatus = async (
|
||||
const user = req.user!;
|
||||
const id = req.query.id as string;
|
||||
|
||||
const alreadyLiked = await DBClient.instance.breweryPostLike.findFirst({
|
||||
where: {
|
||||
breweryPostId: id,
|
||||
likedById: user.id,
|
||||
},
|
||||
const liked = await findBreweryPostLikeService({
|
||||
breweryPostId: id,
|
||||
likedById: user.id,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: alreadyLiked ? 'Brewery post is liked.' : 'Brewery post is not liked.',
|
||||
message: liked ? 'Brewery post is liked.' : 'Brewery post is not liked.',
|
||||
statusCode: 200,
|
||||
payload: { isLiked: !!alreadyLiked },
|
||||
payload: { isLiked: !!liked },
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user