Refactor: update like services

This commit is contained in:
Aaron William Po
2023-12-13 12:38:00 -05:00
parent fdbadb63dc
commit 0de4697e77
25 changed files with 484 additions and 212 deletions

View File

@@ -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 },
});
};