From 15b43cd9258f2f57e859d3505d2e3ca3292b556f Mon Sep 17 00:00:00 2001 From: Eunji <129590633+bianbbc87@users.noreply.github.com> Date: Sun, 24 Nov 2024 05:23:14 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Refactor/#143=20-=20=EA=B0=80?= =?UTF-8?q?=EA=B2=8C=20=EC=83=81=EC=84=B8=20=EC=A1=B0=ED=9A=8C,=20?= =?UTF-8?q?=EC=98=A8=EA=B8=B0=20=EC=9A=B0=ED=8E=B8=ED=95=A8,=20=EC=8B=9D?= =?UTF-8?q?=EA=B6=8C=20=EC=A1=B0=ED=9A=8C=20=EC=A0=95=EB=A0=AC=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20(#144)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻️ refactor/#143 : 가게 히스토리 조회 정렬 픽스 Signed-off-by: EunJiJung * ♻️ refactor/#143 : 온기 우편함 생성일별 조회로 변경 Signed-off-by: EunJiJung * ♻️ refactor/#143 : 티켓 만료일 기준 빠른순 정렬 Signed-off-by: EunJiJung --------- Signed-off-by: EunJiJung --- .../service/ReadStoreDetailService.java | 9 ++- .../mysql/StoreHistoryRepository.java | 5 +- .../ReadOnjungEventOverviewService.java | 73 +++++++++---------- .../service/ReadTicketService.java | 2 +- .../repository/mysql/TicketRepository.java | 2 +- 5 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/daon/onjung/account/application/service/ReadStoreDetailService.java b/src/main/java/com/daon/onjung/account/application/service/ReadStoreDetailService.java index 7bbdc6f..34942cb 100644 --- a/src/main/java/com/daon/onjung/account/application/service/ReadStoreDetailService.java +++ b/src/main/java/com/daon/onjung/account/application/service/ReadStoreDetailService.java @@ -69,7 +69,7 @@ public ReadStoreDetailResponseDto execute(Long id) { totalShareCount ); - List storeHistories = storeHistoryRepository.findByStoreOrderByActionDateDesc(store); + List storeHistories = storeHistoryRepository.findByStoreSortedByActionDate(store); // 그룹화: actionDate를 기준으로 그룹화 Map> groupedByYearMonth = storeHistories.stream() @@ -79,11 +79,13 @@ public ReadStoreDetailResponseDto execute(Long id) { List storeHistoryDtos = groupedByYearMonth.entrySet().stream() - .sorted(Map.Entry.comparingByKey()) + .sorted((e1, e2) -> e2.getKey().compareTo(e1.getKey())) // 키(yearMonth)를 기준으로 내림차순 정렬 .map(entry -> { String yearMonth = entry.getKey(); + + // 그룹화된 리스트를 actionDate 기준으로 내림차순 정렬 List infos = entry.getValue().stream() - .sorted(Comparator.comparing(StoreHistory::getActionDate)) // 그룹 안에서 시간순 정렬 + .sorted((sh1, sh2) -> sh2.getActionDate().compareTo(sh1.getActionDate())) // actionDate 기준 정렬 .map(ReadStoreDetailResponseDto.StoreHistoryDto::fromEntity) // StoreHistory -> StoreHistoryDto 변환 .flatMap(dto -> dto.getStoreHistoryInfo().stream()) // StoreHistoryInfo만 추출 .toList(); @@ -95,6 +97,7 @@ public ReadStoreDetailResponseDto execute(Long id) { }) .toList(); + return ReadStoreDetailResponseDto.fromEntity(storeInfoDto, eventInfoDto, onjungInfoDto, storeHistoryDtos); } diff --git a/src/main/java/com/daon/onjung/account/repository/mysql/StoreHistoryRepository.java b/src/main/java/com/daon/onjung/account/repository/mysql/StoreHistoryRepository.java index 7215959..451793d 100644 --- a/src/main/java/com/daon/onjung/account/repository/mysql/StoreHistoryRepository.java +++ b/src/main/java/com/daon/onjung/account/repository/mysql/StoreHistoryRepository.java @@ -2,12 +2,15 @@ import com.daon.onjung.account.domain.Store; import com.daon.onjung.account.domain.StoreHistory; +import io.lettuce.core.dynamic.annotation.Param; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface StoreHistoryRepository extends JpaRepository { - List findByStoreOrderByActionDateDesc(Store store); + @Query("SELECT sh FROM StoreHistory sh WHERE sh.store = :store ORDER BY sh.actionDate DESC") + List findByStoreSortedByActionDate(@Param("store") Store store); } diff --git a/src/main/java/com/daon/onjung/event/application/service/ReadOnjungEventOverviewService.java b/src/main/java/com/daon/onjung/event/application/service/ReadOnjungEventOverviewService.java index 0b91918..640e6c9 100644 --- a/src/main/java/com/daon/onjung/event/application/service/ReadOnjungEventOverviewService.java +++ b/src/main/java/com/daon/onjung/event/application/service/ReadOnjungEventOverviewService.java @@ -1,7 +1,6 @@ package com.daon.onjung.event.application.service; import com.daon.onjung.account.domain.User; -import com.daon.onjung.account.repository.mysql.StoreRepository; import com.daon.onjung.account.repository.mysql.UserRepository; import com.daon.onjung.core.exception.error.ErrorCode; import com.daon.onjung.core.exception.type.CommonException; @@ -64,64 +63,64 @@ public ReadOnjungEventOverviewResponseDto execute( Onjung onjung = onjungService.createOnjung(donations, receipts, shares); - List sortedOnjungByCreatedAtDesc = onjungService.sortOnjungByCreatedAtDesc(onjung); + List sortedOnjungByCreatedAtDesc = onjungService.sortOnjungByCreatedAt(onjung); List eventDtos = sortedOnjungByCreatedAtDesc.stream() .map(entity -> { if (entity instanceof Donation donation) { - // donation 날짜가 포함된 이벤트 가져오기 + // donation 날짜가 포함된 이벤트 가져오기 Event event = eventRepository.findTopEventByStoreAndLocalDate(donation.getStore().getId(), donation.getCreatedAt().toLocalDate()) .stream() .findFirst() // 첫 번째 값만 가져옴 .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); return ReadOnjungEventOverviewResponseDto.EventDto.fromEntity( - ReadOnjungEventOverviewResponseDto.EventDto.StoreInfoDto.fromEntity( - donation.getStore().getId(), - donation.getStore().getLogoImgUrl(), - donation.getStore().getTitle(), - donation.getStore().getName() - ), - DateTimeUtil.convertLocalDateTimeToSHORTKORString(donation.getCreatedAt()), - EOnjungType.fromString("DONATION"), - event.getStatus(), - DateTimeUtil.convertLocalDatesToDotSeparatedDatePeriod(event.getStartDate(), event.getEndDate()), - DateTimeUtil.convertLocalDateToDotSeparatedDateTime(event.getStoreDeliveryDate()), - DateTimeUtil.convertLocalDateToDotSeparatedDateTime(event.getTicketIssueDate()), - DateTimeUtil.convertLocalDateToDotSeparatedDateTime(event.getReportDate()) - ); + ReadOnjungEventOverviewResponseDto.EventDto.StoreInfoDto.fromEntity( + donation.getStore().getId(), + donation.getStore().getLogoImgUrl(), + donation.getStore().getTitle(), + donation.getStore().getName() + ), + DateTimeUtil.convertLocalDateTimeToSHORTKORString(donation.getCreatedAt()), + EOnjungType.fromString("DONATION"), + event.getStatus(), + DateTimeUtil.convertLocalDatesToDotSeparatedDatePeriod(event.getStartDate(), event.getEndDate()), + DateTimeUtil.convertLocalDateToDotSeparatedDateTime(event.getStoreDeliveryDate()), + DateTimeUtil.convertLocalDateToDotSeparatedDateTime(event.getTicketIssueDate()), + DateTimeUtil.convertLocalDateToDotSeparatedDateTime(event.getReportDate()) + ); } else if (entity instanceof Receipt receipt) { return ReadOnjungEventOverviewResponseDto.EventDto.fromEntity( - ReadOnjungEventOverviewResponseDto.EventDto.StoreInfoDto.fromEntity( - receipt.getStore().getId(), - receipt.getStore().getLogoImgUrl(), - receipt.getStore().getTitle(), - receipt.getStore().getName() - ), - DateTimeUtil.convertLocalDateTimeToSHORTKORString(receipt.getCreatedAt()), - EOnjungType.fromString("RECEIPT"), + ReadOnjungEventOverviewResponseDto.EventDto.StoreInfoDto.fromEntity( + receipt.getStore().getId(), + receipt.getStore().getLogoImgUrl(), + receipt.getStore().getTitle(), + receipt.getStore().getName() + ), + DateTimeUtil.convertLocalDateTimeToSHORTKORString(receipt.getPaymentDate().atTime(0, 0)), + EOnjungType.fromString("RECEIPT"), EStatus.COMPLETED, DateTimeUtil.convertLocalDateToDotSeparatedDateTime(receipt.getPaymentDate()), - null, - null, - null - ); + null, + null, + null + ); } else if (entity instanceof Share share) { return ReadOnjungEventOverviewResponseDto.EventDto.fromEntity( - ReadOnjungEventOverviewResponseDto.EventDto.StoreInfoDto.fromEntity( - share.getStore().getId(), - share.getStore().getLogoImgUrl(), - share.getStore().getTitle(), - share.getStore().getName() - ), + ReadOnjungEventOverviewResponseDto.EventDto.StoreInfoDto.fromEntity( + share.getStore().getId(), + share.getStore().getLogoImgUrl(), + share.getStore().getTitle(), + share.getStore().getName() + ), DateTimeUtil.convertLocalDateTimeToSHORTKORString(share.getCreatedAt().atTime(0, 0)), - EOnjungType.fromString("SHARE"), + EOnjungType.fromString("SHARE"), EStatus.COMPLETED, DateTimeUtil.convertLocalDateToDotSeparatedDateTime(share.getCreatedAt()), null, null, null - ); + ); } throw new CommonException(ErrorCode.INVALID_ARGUMENT); }) diff --git a/src/main/java/com/daon/onjung/event/application/service/ReadTicketService.java b/src/main/java/com/daon/onjung/event/application/service/ReadTicketService.java index 43c7b56..4155bd5 100644 --- a/src/main/java/com/daon/onjung/event/application/service/ReadTicketService.java +++ b/src/main/java/com/daon/onjung/event/application/service/ReadTicketService.java @@ -40,7 +40,7 @@ public ReadTicketResponseDto execute( User user = userRepository.findById(accountId) .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); - Page ticketsPage = ticketRepository.findByUserOrderByExpirationDateDesc(user, pageable); + Page ticketsPage = ticketRepository.findByUserOrderByExpirationDateAsc(user, pageable); // dto로 변환 List ticketDtos = ticketsPage.stream() diff --git a/src/main/java/com/daon/onjung/event/repository/mysql/TicketRepository.java b/src/main/java/com/daon/onjung/event/repository/mysql/TicketRepository.java index 24d72df..3b7a71a 100644 --- a/src/main/java/com/daon/onjung/event/repository/mysql/TicketRepository.java +++ b/src/main/java/com/daon/onjung/event/repository/mysql/TicketRepository.java @@ -11,7 +11,7 @@ public interface TicketRepository extends JpaRepository { // user로 Ticket 조회, expirationDate를 ASC로 정렬 - Page findByUserOrderByExpirationDateDesc(User user, Pageable pageable); + Page findByUserOrderByExpirationDateAsc(User user, Pageable pageable); // user로 Ticket 수 조회 Long countByUser(User user);