Skip to content
Snippets Groups Projects
Commit 10a6b1cc authored by 조대희's avatar 조대희
Browse files

refactor: friend pagination 방식 수정 (#19)

parent 322334e3
No related branches found
No related tags found
No related merge requests found
...@@ -137,15 +137,27 @@ class friendController { ...@@ -137,15 +137,27 @@ class friendController {
/** /**
* 친구 목록 조회 * 친구 목록 조회
* GET /api/friend/all/:offset * GET /api/friend/all?page=1&size=20
*/ */
async getFriendList(req, res) { async getFriendList(req, res) {
try { try {
const userId = req.user.id; 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({ return res.status(200).json({
success: true, success: true,
data: friends data: {
content: friends,
page: page,
size: size,
hasNext: friends.length === size
}
}); });
} catch (error) { } catch (error) {
return res.status(500).json({ return res.status(500).json({
......
...@@ -37,7 +37,7 @@ router.post('/request/:friendId/reject', FriendController.rejectRequest); ...@@ -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); router.get('/all', FriendController.getFriendList);
......
...@@ -191,7 +191,9 @@ class FriendService { ...@@ -191,7 +191,9 @@ class FriendService {
* @param {number} offset - 페이징 오프셋 * @param {number} offset - 페이징 오프셋
* @returns {Promise<Array<FriendListDTO>>} - 친구 목록 DTO 배열 * @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({ const friends = await Friend.findAll({
where: { where: {
[Op.or]: [ [Op.or]: [
...@@ -213,12 +215,19 @@ class FriendService { ...@@ -213,12 +215,19 @@ class FriendService {
} }
], ],
order: [['id', 'ASC']], order: [['id', 'ASC']],
limit, limit: limit + 1, // 다음 페이지 존재 여부 확인을 위해 1개 더 조회
offset offset
}); });
const hasNext = friends.length > limit;
const content = friends.slice(0, limit).map(friend => new FriendListDTO(friend, userId));
return friends.map(friend => new FriendListDTO(friend, userId)); return {
content,
page: offset / limit,
size: limit,
hasNext
};
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment