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

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

parent 454bf036
Branches
No related tags found
2 merge requests!31Develop,!26[#19] 통합 테스트 제작 및 서비스 로직 추가/수정
......@@ -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({
......
......@@ -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);
......
......@@ -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
};
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment