Skip to content

Commit b467330

Browse files
Feat: [FN-120] 나의 그룹 전체 조회
Feat: [FN-120] 나의 그룹 전체 조회
2 parents 28997c8 + 4bfef46 commit b467330

4 files changed

Lines changed: 81 additions & 9 deletions

File tree

src/main/java/project/flipnote/group/controller/GroupController.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,15 @@ public ResponseEntity<CursorPagingResponse<GroupInfo>> findGroup(
9494

9595
return ResponseEntity.ok(res);
9696
}
97+
98+
//내 그룹 전체 조회
99+
@GetMapping("/me")
100+
public ResponseEntity<CursorPagingResponse<GroupInfo>> findMyGroup(
101+
@AuthenticationPrincipal AuthPrinciple authPrinciple,
102+
@Valid @ModelAttribute GroupListRequest req
103+
) {
104+
CursorPagingResponse<GroupInfo> res = groupService.findMyGroup(authPrinciple, req);
105+
106+
return ResponseEntity.ok(res);
107+
}
97108
}

src/main/java/project/flipnote/group/repository/GroupRepositoryCustom.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77

88
public interface GroupRepositoryCustom {
99
List<GroupInfo> findAllByCursor(Long lastId, Category category, int pageSize);
10+
11+
List<GroupInfo> findAllByCursorAndUserId(Long lastId, Category category, int pageSize, Long userId);
1012
}

src/main/java/project/flipnote/group/repository/GroupRepositoryImpl.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import lombok.RequiredArgsConstructor;
1111
import project.flipnote.group.entity.Category;
1212
import project.flipnote.group.entity.QGroup;
13+
import project.flipnote.group.entity.QGroupMember;
1314
import project.flipnote.group.model.GroupInfo;
1415

1516
@RequiredArgsConstructor
@@ -18,6 +19,7 @@ public class GroupRepositoryImpl implements GroupRepositoryCustom {
1819
private final JPAQueryFactory queryFactory;
1920

2021
QGroup group = QGroup.group;
22+
QGroupMember groupMember = QGroupMember.groupMember;
2123

2224
@Override
2325
public List<GroupInfo> findAllByCursor(Long lastId, Category category, int pageSize) {
@@ -47,4 +49,34 @@ public List<GroupInfo> findAllByCursor(Long lastId, Category category, int pageS
4749
.fetch();
4850
}
4951

52+
@Override
53+
public List<GroupInfo> findAllByCursorAndUserId(Long lastId, Category category, int pageSize, Long userId) {
54+
BooleanBuilder where = new BooleanBuilder()
55+
.and(group.deletedAt.isNull());
56+
57+
if (lastId != null) {
58+
where.and(group.id.lt(lastId));
59+
}
60+
61+
if (category != null) {
62+
where.and(group.category.eq(category));
63+
}
64+
65+
return queryFactory.select(Projections.constructor(
66+
GroupInfo.class,
67+
group.id,
68+
group.name,
69+
group.description,
70+
group.category,
71+
group.imageUrl
72+
))
73+
.from(groupMember)
74+
.join(groupMember.group, group)
75+
.on(groupMember.user.id.eq(userId))
76+
.where(where)
77+
.orderBy(group.id.desc())
78+
.limit(pageSize+1)
79+
.fetch();
80+
}
81+
5082
}

src/main/java/project/flipnote/group/service/GroupService.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.stereotype.Service;
77
import org.springframework.transaction.annotation.Transactional;
88

9+
import jakarta.validation.Valid;
910
import lombok.RequiredArgsConstructor;
1011
import lombok.extern.slf4j.Slf4j;
1112
import project.flipnote.common.exception.BizException;
@@ -331,15 +332,7 @@ public CursorPagingResponse<GroupInfo> findGroup(AuthPrinciple authPrinciple, Gr
331332

332333
List<GroupInfo> groups = groupRepository.findAllByCursor(req.getCursorId(), category, req.getSize());
333334

334-
boolean hasNext = groups.size() > req.getSize();
335-
336-
if (hasNext) {
337-
groups = groups.subList(0, req.getSize());
338-
}
339-
340-
Long nextCursor = hasNext ? groups.get(groups.size() - 1).groupId() : null;
341-
342-
return CursorPagingResponse.of(groups, hasNext, nextCursor);
335+
return createGroupInfoCursorPagingResponse(req, groups);
343336
}
344337

345338
private Category convertCategory(String rawCategory) {
@@ -365,4 +358,38 @@ private Category convertCategory(String rawCategory) {
365358
public boolean existsMember(Long groupId, Long userId) {
366359
return groupMemberRepository.existsByGroup_IdAndUser_Id(groupId, userId);
367360
}
361+
362+
/**
363+
* 내가 가입한 그룹 전체 조회하기
364+
*
365+
* @param authPrinciple 회원 accessToken
366+
* @param req 필터링
367+
* @return
368+
*/
369+
public CursorPagingResponse<GroupInfo> findMyGroup(AuthPrinciple authPrinciple, GroupListRequest req) {
370+
//1. 유저 가져오기
371+
UserProfile user = getUser(authPrinciple);
372+
373+
//2. 카테고리 변환
374+
Category category = convertCategory(req.getCategory());
375+
376+
List<GroupInfo> groups = groupRepository.findAllByCursorAndUserId(req.getCursorId(), category, req.getSize(),
377+
user.getId());
378+
379+
return createGroupInfoCursorPagingResponse(req, groups);
380+
}
381+
382+
//리스트 조회시 response 생성
383+
private CursorPagingResponse<GroupInfo> createGroupInfoCursorPagingResponse(GroupListRequest req,
384+
List<GroupInfo> groups) {
385+
boolean hasNext = groups.size() > req.getSize();
386+
387+
if (hasNext) {
388+
groups = groups.subList(0, req.getSize());
389+
}
390+
391+
Long nextCursor = hasNext ? groups.get(groups.size() - 1).groupId() : null;
392+
393+
return CursorPagingResponse.of(groups, hasNext, nextCursor);
394+
}
368395
}

0 commit comments

Comments
 (0)