Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions controllers/followController.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
70 changes: 59 additions & 11 deletions repositorys/followRepository.js
Original file line number Diff line number Diff line change
@@ -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' },
},
}),
Expand All @@ -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,
};
});

Expand All @@ -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' },
Expand All @@ -69,7 +93,7 @@ const getFollowing = async (userId, size, cursor, name) => {
take: size,
skip: cursor,
include: {
following: {
follower: {
select: {
id: true,
email: true,
Expand All @@ -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,
};
});

Expand Down
12 changes: 10 additions & 2 deletions router/followRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions services/followService.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
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
);
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
Expand Down
Loading