Skip to content

Commit

Permalink
fix: groupby와 sorting의 역할을 querydsl로 이전 (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
baebae02 authored Apr 4, 2024
2 parents 55ecf99 + ca173af commit 912120c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import com.uspray.uspray.DTO.group.response.GroupResponseDto;
import com.uspray.uspray.DTO.group.response.QGroupMemberResponseDto;
import com.uspray.uspray.DTO.group.response.QGroupResponseDto;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
Expand All @@ -25,7 +27,7 @@ public class GroupRepositoryImpl implements GroupRepositoryCustom {

@Override
public List<GroupResponseDto> findGroupListByMemberId(String userId) {
return queryFactory
List<GroupResponseDto> result = queryFactory
.select(new QGroupResponseDto(
group.id,
group.name,
Expand All @@ -42,6 +44,17 @@ public List<GroupResponseDto> findGroupListByMemberId(String userId) {
.groupBy(group.id, group.name, group.leader.userId, groupPray.content)
.orderBy(groupPray.createdAt.max().desc())
.fetch();
return result.stream()
.collect(Collectors.groupingBy(GroupResponseDto::getId))
.values().stream()
.map(groupResponseDtos -> groupResponseDtos.stream()
.max(Comparator.comparing(GroupResponseDto::getUpdatedAt))
.orElseThrow(() -> new IllegalArgumentException("그룹이 존재하지 않습니다.")))
.sorted(
Comparator.comparing(GroupResponseDto::getUpdatedAt,
Comparator.nullsFirst(Comparator.naturalOrder()))
.reversed()) // 최신 업데이트 날짜로 정렬
.collect(Collectors.toList());
}

@Override
Expand Down
13 changes: 1 addition & 12 deletions src/main/java/com/uspray/uspray/service/GroupService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.uspray.uspray.DTO.group.response.GroupResponseDto;
import com.uspray.uspray.infrastructure.GroupRepository;
import com.uspray.uspray.util.MaskingUtil;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -21,16 +19,7 @@ public class GroupService {
@Transactional(readOnly = true)
public GroupListResponseDto getGroupList(String username) {
List<GroupResponseDto> groupList = groupRepository.findGroupListByMemberId(username);
List<GroupResponseDto> filterMostRecentUpdatedAt =
groupList.stream()
.collect(Collectors.groupingBy(GroupResponseDto::getId))
.values().stream()
.map(groupResponseDtos -> groupResponseDtos.stream()
.max(Comparator.comparing(GroupResponseDto::getUpdatedAt))
.orElseThrow(() -> new IllegalArgumentException("그룹이 존재하지 않습니다.")))
.collect(Collectors.toList());

return new GroupListResponseDto(filterMostRecentUpdatedAt);
return new GroupListResponseDto(groupList);
}

@Transactional(readOnly = true)
Expand Down

0 comments on commit 912120c

Please sign in to comment.