From f220cfc56ec07c8c718a9c2d27e24cd2fbc1741e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=EC=A1=B0=EB=8C=80=ED=9D=AC?= <joedaehui@ajou.ac.kr>
Date: Sat, 16 Nov 2024 13:37:54 +0900
Subject: [PATCH] =?UTF-8?q?fix:=20reqeustId=20->=20friendId=EB=A1=9C=20?=
 =?UTF-8?q?=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=ED=8C=8C=EB=9D=BC=EB=AF=B8?=
 =?UTF-8?q?=ED=84=B0=20=EC=88=9C=EC=84=9C=20=EB=B3=80=EA=B2=BD=20(#8)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app.js                          |  4 +-
 controllers/friendController.js | 16 ++++----
 routes/friend.js                | 12 +++---
 services/friendService.js       | 69 +++++++++++++++++++++------------
 4 files changed, 61 insertions(+), 40 deletions(-)

diff --git a/app.js b/app.js
index 0e427cf..dcc56c0 100644
--- a/app.js
+++ b/app.js
@@ -44,8 +44,8 @@ const scheduleRoutes = require('./routes/schedule');
 app.use('/api/schedule', scheduleRoutes);
 
 // Friend 라우터
-const friendRoutes = require('./routes/friendRoutes');
-app.use('/api/friends', friendRoutes);
+const friendRoutes = require('./routes/friend');
+app.use('/api/friend', friendRoutes);
 
 
 
diff --git a/controllers/friendController.js b/controllers/friendController.js
index d4601d6..707de61 100644
--- a/controllers/friendController.js
+++ b/controllers/friendController.js
@@ -3,12 +3,12 @@ const FriendService = require('../services/friendService');
 class friendController {
     /**
      * 친구 요청 보내기
-     * POST /api/friend/request
+     * POST /api/friend/request/:friendId
      */
     async sendRequest(req, res) {
         try {
             const userId = req.user.id;
-            const { friendId } = req.body;
+            const { friendId } = req.params;
 
             const request = await FriendService.sendFriendRequest(userId, friendId);
             return res.status(201).json({
@@ -76,14 +76,14 @@ class friendController {
     
     /**
      * 친구 요청 수락
-     * POST /api/friend/request/:requestId/accept
+     * POST /api/friend/request/:friendId/accept
      */
     async acceptRequest(req, res) {
         try {
             const userId = req.user.id;
-            const { requestId } = req.params;
+            const { friendId } = req.params;
 
-            const result = await FriendService.acceptFriendRequest(requestId, userId);
+            const result = await FriendService.acceptFriendRequest(userId, friendId);
 
             return res.status(200).json({
                 success: true,
@@ -102,14 +102,14 @@ class friendController {
 
     /**
      * 친구 요청 거절
-     * POST /api/friend/request/:requestId/reject
+     * POST /api/friend/request/:friendId/reject
      */
     async rejectRequest(req, res) {
         try {
             const userId = req.user.id;
-            const { requesId } = req.params;
+            const { friendId } = req.params;
 
-            const result = await FriendService.rejectFriendRequest(requesId, userId);
+            const result = await FriendService.rejectFriendRequest(userId, friendId);
 
             return res.status(200).json({
                 success: true,
diff --git a/routes/friend.js b/routes/friend.js
index 9786c35..782540b 100644
--- a/routes/friend.js
+++ b/routes/friend.js
@@ -7,9 +7,9 @@ router.use(isLoggedIn);
 
 /**
  * 친구 요청 보내기
- * POST /api/friend/request
+ * POST /api/friend/request/:friendId
  */
-router.post('/request', FriendController.sendRequest);
+router.post('/request/:friendId', FriendController.sendRequest);
 
 /**
  * 받은 친구 요청 목록 조회
@@ -25,15 +25,15 @@ router.get('/requests/sent', FriendController.getSentRequests);
 
 /**
  * 친구 요청 수락
- * POST /api/friend/request/:requestId/accept
+ * POST /api/friend/request/:friendId/accept
  */
-router.post('/request/:requestId/accept', FriendController.acceptRequest);
+router.post('/request/:friendId/accept', FriendController.acceptRequest);
 
 /**
  * 친구 요청 거절
- * POST /api/friend/request/:requestId/reject
+ * POST /api/friend/request/:friendId/reject
  */
-router.post('/request/:requestId/reject', FriendController.rejectRequest);
+router.post('/request/:friendId/reject', FriendController.rejectRequest);
 
 /**
  * 친구 목록 조회
diff --git a/services/friendService.js b/services/friendService.js
index c548646..e16e4fc 100644
--- a/services/friendService.js
+++ b/services/friendService.js
@@ -75,7 +75,7 @@ class friendService {
             },
             include: [{
                 model: User,
-                as: 'friend',
+                as: 'user',
                 attributes: ['id', 'name', 'email']
             }]
         });
@@ -84,38 +84,38 @@ class friendService {
     /**
      * 친구 요청 수락
      */
-    async acceptFriendRequest(requestId, userId) {
+    async acceptFriendRequest(userId, friendId) {
         const request = await Friend.findOne({
             where: {
-                id: requestId,
+                user_id: friendId,
                 friend_id: userId,
                 status: 'PENDING'
             }
         });
-
+    
         if (!request) {
             throw new Error('Friend request not found');
         }
-
-        return request.update({ status: 'ACCEPTED'});
+    
+        return request.update({ status: 'ACCEPTED' });
     }
 
     /**
      * 친구 요청 거절
      */
-    async rejectFriendRequest(requestId, userId) {
+    async rejectFriendRequest(userId, friendId) { 
         const result = await Friend.destroy({
             where: {
-                id: requestId,
-                friend_id: userId,
+                user_id: friendId,  
+                friend_id: userId,  
                 status: 'PENDING'
             }
         });
-
+    
         if (!result) {
-            throw new Error('Friend reqeust not found');
+            throw new Error('Friend request not found');
         }
-
+    
         return result;
     }
 
@@ -123,23 +123,44 @@ class friendService {
      * 친구 목록 조회
      */
     async getFriendList(userId) {
-        return Friend.findAll({
+        const friends = await Friend.findAll({
             where: {
                 [Op.or]: [
-                    {user_id: userId},
-                    {friend_id: userId}
+                    { user_id: userId },
+                    { friend_id: userId }
                 ],
                 status: 'ACCEPTED'
+            }
+        });
+    
+        const friendIds = friends.map(friend => 
+            friend.user_id === userId ? friend.friend_id : friend.user_id
+        );
+    
+        const friendUsers = await User.findAll({
+            where: {
+                id: friendIds
             },
-            include: [{
-                model: User,
-                as: 'friend',
-                attributes: ['id', 'name', 'email']
-            }, {
-                model: User,
-                as: 'user',
-                attributes: ['id', 'name', 'email']
-            }]
+            attributes: ['id', 'name', 'email']
+        });
+    
+        const friendsMap = friendUsers.reduce((map, user) => {
+            map[user.id] = user;
+            return map;
+        }, {});
+    
+        return friends.map(friend => {
+            const isSender = friend.user_id === userId;
+            const friendId = isSender ? friend.friend_id : friend.user_id;
+            
+            return {
+                id: friend.id,
+                status: friend.status,
+                createdAt: friend.createdAt,
+                updatedAt: friend.updatedAt,
+                friendInfo: friendsMap[friendId],
+                relationshipType: isSender ? 'sent' : 'received'
+            };
         });
     }
 
-- 
GitLab