diff --git a/controllers/friendController.js b/controllers/friendController.js index 8f385c7cf1a01cdfbf55ab5afea27df9a193ecbe..c3eb2515eb46ef2a7eadebe5f0294f6698db3536 100644 --- a/controllers/friendController.js +++ b/controllers/friendController.js @@ -137,15 +137,27 @@ class friendController { /** * 친구 목록 조회 - * GET /api/friend/all/:offset + * GET /api/friend/all?page=1&size=20 */ async getFriendList(req, res) { try { const userId = req.user.id; - const friends = await FriendService.getFriendList(userId,20,req.param); + const page = parseInt(req.query.page) || 0; + const size = parseInt(req.query.size) || 20; + + const friends = await FriendService.getFriendList(userId, { + limit: size, + offset: page * size + }); + return res.status(200).json({ success: true, - data: friends + data: { + content: friends, + page: page, + size: size, + hasNext: friends.length === size + } }); } catch (error) { return res.status(500).json({ diff --git a/routes/friend.js b/routes/friend.js index 53145a9930403881d542d7fad15a27d9d485edbc..6061959eff8d2a4fffd78ebba2226deef8982816 100644 --- a/routes/friend.js +++ b/routes/friend.js @@ -37,7 +37,7 @@ router.post('/request/:friendId/reject', FriendController.rejectRequest); /** * 친구 목록 조회 - * GET /api/friend/all + * GET /api/friend/all?page=1&size=20 */ router.get('/all', FriendController.getFriendList); diff --git a/services/friendService.js b/services/friendService.js index bb995bb9ef69f4221f2ff40dbc452f21ba75fba4..d35b3c700c9b4b1a1b7a18326eafb2eace829478 100644 --- a/services/friendService.js +++ b/services/friendService.js @@ -191,7 +191,9 @@ class FriendService { * @param {number} offset - 페이징 오프셋 * @returns {Promise<Array<FriendListDTO>>} - 친구 목록 DTO 배열 */ - async getFriendList(userId, limit = 20, offset = 0) { + async getFriendList(userId, pagination) { + const { limit = 20, offset = 0 } = pagination; + const friends = await Friend.findAll({ where: { [Op.or]: [ @@ -212,13 +214,20 @@ class FriendService { attributes: ['id', 'name', 'email'] } ], - order: [['id', 'ASC']], - limit, + order: [['id', 'ASC']], + limit: limit + 1, // 다음 페이지 존재 여부 확인을 위해 1개 더 조회 offset }); - - - return friends.map(friend => new FriendListDTO(friend, userId)); + + const hasNext = friends.length > limit; + const content = friends.slice(0, limit).map(friend => new FriendListDTO(friend, userId)); + + return { + content, + page: offset / limit, + size: limit, + hasNext + }; } /**