From 41a8c0c1343271147e852fca5f6d5060fb5d180d Mon Sep 17 00:00:00 2001 From: minsu Date: Thu, 5 Jun 2025 20:43:19 +0900 Subject: [PATCH] =?UTF-8?q?[Fix]=20=ED=8C=94=EB=A1=9C=EC=9A=B0=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=ED=95=A0=EB=95=8C=20=ED=95=B4=EB=8B=B9=20=EC=9C=A0?= =?UTF-8?q?=EC=A0=80=20=EC=83=81=ED=83=9C=EA=B4=80=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/followController.js | 8 +++- repositorys/followRepository.js | 70 +++++++++++++++++++++++++++------ router/followRouter.js | 12 +++++- services/followService.js | 10 +++-- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/controllers/followController.js b/controllers/followController.js index 1468b03..4982921 100644 --- a/controllers/followController.js +++ b/controllers/followController.js @@ -1,11 +1,13 @@ import followService from '../services/followService.js'; const getFollowers = async (req, res) => { - const { userId } = req.params; + const { id: userId } = req.user; + const { userId: targetUserId } = req.params; const { size, cursor, name } = req.query; try { const followers = await followService.getFollowers( parseInt(userId), + parseInt(targetUserId), parseInt(size) || 10, parseInt(cursor) || 0, name || null @@ -19,11 +21,13 @@ const getFollowers = async (req, res) => { }; const getFollowing = async (req, res) => { - const { userId } = req.params; + const { id: userId } = req.user; + const { userId: targetUserId } = req.params; const { size, cursor, name } = req.query; try { const following = await followService.getFollowing( parseInt(userId), + parseInt(targetUserId), parseInt(size) || 10, parseInt(cursor) || 0, name || null diff --git a/repositorys/followRepository.js b/repositorys/followRepository.js index 88f876c..82be273 100644 --- a/repositorys/followRepository.js +++ b/repositorys/followRepository.js @@ -1,11 +1,11 @@ import prisma from '../config/prisma.js'; -const getFollowers = async (userId, size, cursor, name) => { +const getFollowers = async (userId, targetUserId, size, cursor, name) => { // name이 null이면 검색 조건을 제외 const whereCondition = { - follower_id: userId, + follower_id: targetUserId, ...(name && { - following: { + follower: { nickname: { contains: name, mode: 'insensitive' }, }, }), @@ -31,13 +31,37 @@ const getFollowers = async (userId, size, cursor, name) => { }, }); + // 모든 관계를 한 번에 가져오기 + const followerIds = followers.map((f) => f.following.id); + const [followingRelations, followerRelations] = await Promise.all([ + prisma.follow.findMany({ + where: { + follower_id: userId, + following_id: { in: followerIds }, + }, + }), + prisma.follow.findMany({ + where: { + follower_id: { in: followerIds }, + following_id: userId, + }, + }), + ]); + const followersWithStatus = followers.map((follower) => { const { profile_image, ...rest } = follower.following; + const isFollowing = followingRelations.some( + (rel) => rel.following_id === follower.following.id + ); + const isFollower = followerRelations.some( + (rel) => rel.follower_id === follower.following.id + ); + return { ...rest, profileImage: profile_image, - isFollower: false, - isFollowing: false, + isFollower, + isFollowing, }; }); @@ -52,9 +76,9 @@ const getFollowers = async (userId, size, cursor, name) => { }; }; -const getFollowing = async (userId, size, cursor, name) => { +const getFollowing = async (userId, targetUserId, size, cursor, name) => { const whereCondition = { - follower_id: userId, + following_id: targetUserId, ...(name && { following: { nickname: { contains: name, mode: 'insensitive' }, @@ -69,7 +93,7 @@ const getFollowing = async (userId, size, cursor, name) => { take: size, skip: cursor, include: { - following: { + follower: { select: { id: true, email: true, @@ -80,13 +104,37 @@ const getFollowing = async (userId, size, cursor, name) => { }, }); + // 모든 관계를 한 번에 가져오기 + const followingIds = following.map((f) => f.follower.id); + const [followingRelations, followerRelations] = await Promise.all([ + prisma.follow.findMany({ + where: { + follower_id: userId, + following_id: { in: followingIds }, + }, + }), + prisma.follow.findMany({ + where: { + follower_id: { in: followingIds }, + following_id: userId, + }, + }), + ]); + const followingWithStatus = following.map((item) => { - const { profile_image, ...rest } = item.following; + const { profile_image, ...rest } = item.follower; + const isFollowing = followingRelations.some( + (rel) => rel.following_id === item.follower.id + ); + const isFollower = followerRelations.some( + (rel) => rel.follower_id === item.follower.id + ); + return { ...rest, profileImage: profile_image, - isFollower: false, - isFollowing: false, + isFollower, + isFollowing, }; }); diff --git a/router/followRouter.js b/router/followRouter.js index daa4a77..9e31f31 100644 --- a/router/followRouter.js +++ b/router/followRouter.js @@ -146,7 +146,11 @@ const router = express.Router(); * type: string * example: "서버 에러가 발생했습니다." */ -router.get('/:userId/followers', followController.getFollowers); +router.get( + '/:userId/followers', + jwtToken.accessVerifyToken, + followController.getFollowers +); /** * @swagger @@ -290,7 +294,11 @@ router.get('/:userId/followers', followController.getFollowers); * type: string * example: "서버 에러가 발생했습니다." */ -router.get('/:userId/following', followController.getFollowing); +router.get( + '/:userId/following', + jwtToken.accessVerifyToken, + followController.getFollowing +); /** * @swagger diff --git a/services/followService.js b/services/followService.js index ba3bd6d..3f3bc0f 100644 --- a/services/followService.js +++ b/services/followService.js @@ -1,10 +1,11 @@ import followRepository from '../repositorys/followRepository.js'; import userRepository from '../repositorys/userRepository.js'; -const getFollowers = async (userId, size, cursor, name) => { - await userRepository.getUserInfo(userId); +const getFollowers = async (userId, targetUserId, size, cursor, name) => { + await userRepository.getUserInfo(targetUserId); const followers = await followRepository.getFollowers( userId, + targetUserId, size, cursor, name @@ -12,10 +13,11 @@ const getFollowers = async (userId, size, cursor, name) => { return followers; }; -const getFollowing = async (userId, size, cursor, name) => { - await userRepository.getUserInfo(userId); +const getFollowing = async (userId, targetUserId, size, cursor, name) => { + await userRepository.getUserInfo(targetUserId); const following = await followRepository.getFollowing( userId, + targetUserId, size, cursor, name