Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,15 @@ public ResponseEntity<CursorPagingResponse<GroupInfo>> findGroup(

return ResponseEntity.ok(res);
}

//내 그룹 전체 조회
@GetMapping("/me")
public ResponseEntity<CursorPagingResponse<GroupInfo>> findMyGroup(
@AuthenticationPrincipal AuthPrinciple authPrinciple,
@Valid @ModelAttribute GroupListRequest req
) {
CursorPagingResponse<GroupInfo> res = groupService.findMyGroup(authPrinciple, req);

return ResponseEntity.ok(res);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface GroupRepositoryCustom {
List<GroupInfo> findAllByCursor(Long lastId, Category category, int pageSize);

List<GroupInfo> findAllByCursorAndUserId(Long lastId, Category category, int pageSize, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.RequiredArgsConstructor;
import project.flipnote.group.entity.Category;
import project.flipnote.group.entity.QGroup;
import project.flipnote.group.entity.QGroupMember;
import project.flipnote.group.model.GroupInfo;

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

QGroup group = QGroup.group;
QGroupMember groupMember = QGroupMember.groupMember;

@Override
public List<GroupInfo> findAllByCursor(Long lastId, Category category, int pageSize) {
Expand Down Expand Up @@ -47,4 +49,34 @@ public List<GroupInfo> findAllByCursor(Long lastId, Category category, int pageS
.fetch();
}

@Override
public List<GroupInfo> findAllByCursorAndUserId(Long lastId, Category category, int pageSize, Long userId) {
BooleanBuilder where = new BooleanBuilder()
.and(group.deletedAt.isNull());

if (lastId != null) {
where.and(group.id.lt(lastId));
}

if (category != null) {
where.and(group.category.eq(category));
}

return queryFactory.select(Projections.constructor(
GroupInfo.class,
group.id,
group.name,
group.description,
group.category,
group.imageUrl
))
.from(groupMember)
.join(groupMember.group, group)
.on(groupMember.user.id.eq(userId))
.where(where)
.orderBy(group.id.desc())
.limit(pageSize+1)
.fetch();
}

}
45 changes: 36 additions & 9 deletions src/main/java/project/flipnote/group/service/GroupService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

boolean hasNext = groups.size() > req.getSize();

if (hasNext) {
groups = groups.subList(0, req.getSize());
}

Long nextCursor = hasNext ? groups.get(groups.size() - 1).groupId() : null;

return CursorPagingResponse.of(groups, hasNext, nextCursor);
return createGroupInfoCursorPagingResponse(req, groups);
}

private Category convertCategory(String rawCategory) {
Expand All @@ -365,4 +358,38 @@ private Category convertCategory(String rawCategory) {
public boolean existsMember(Long groupId, Long userId) {
return groupMemberRepository.existsByGroup_IdAndUser_Id(groupId, userId);
}

/**
* 내가 가입한 그룹 전체 조회하기
*
* @param authPrinciple 회원 accessToken
* @param req 필터링
* @return
*/
public CursorPagingResponse<GroupInfo> findMyGroup(AuthPrinciple authPrinciple, GroupListRequest req) {
//1. 유저 가져오기
UserProfile user = getUser(authPrinciple);

//2. 카테고리 변환
Category category = convertCategory(req.getCategory());

List<GroupInfo> groups = groupRepository.findAllByCursorAndUserId(req.getCursorId(), category, req.getSize(),
user.getId());

return createGroupInfoCursorPagingResponse(req, groups);
}

//리스트 조회시 response 생성
private CursorPagingResponse<GroupInfo> createGroupInfoCursorPagingResponse(GroupListRequest req,
List<GroupInfo> groups) {
boolean hasNext = groups.size() > req.getSize();

if (hasNext) {
groups = groups.subList(0, req.getSize());
}

Long nextCursor = hasNext ? groups.get(groups.size() - 1).groupId() : null;

return CursorPagingResponse.of(groups, hasNext, nextCursor);
}
}
Loading