Skip to content

Commit

Permalink
🔨 Refactor/#143 - 가게 상세 조회, 온기 우편함, 식권 조회 정렬 변경 (#144)
Browse files Browse the repository at this point in the history
* ♻️ refactor/#143 : 가게 히스토리 조회 정렬 픽스

Signed-off-by: EunJiJung <[email protected]>

* ♻️ refactor/#143 : 온기 우편함 생성일별 조회로 변경

Signed-off-by: EunJiJung <[email protected]>

* ♻️ refactor/#143 : 티켓 만료일 기준 빠른순 정렬

Signed-off-by: EunJiJung <[email protected]>

---------

Signed-off-by: EunJiJung <[email protected]>
  • Loading branch information
bianbbc87 authored Nov 23, 2024
1 parent 3ed9a4b commit 15b43cd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ReadStoreDetailResponseDto execute(Long id) {
totalShareCount
);

List<StoreHistory> storeHistories = storeHistoryRepository.findByStoreOrderByActionDateDesc(store);
List<StoreHistory> storeHistories = storeHistoryRepository.findByStoreSortedByActionDate(store);

// 그룹화: actionDate를 기준으로 그룹화
Map<String, List<StoreHistory>> groupedByYearMonth = storeHistories.stream()
Expand All @@ -79,11 +79,13 @@ public ReadStoreDetailResponseDto execute(Long id) {


List<ReadStoreDetailResponseDto.StoreHistoryDto> 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<ReadStoreDetailResponseDto.StoreHistoryDto.StoreHistoryInfo> 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();
Expand All @@ -95,6 +97,7 @@ public ReadStoreDetailResponseDto execute(Long id) {
})
.toList();


return ReadStoreDetailResponseDto.fromEntity(storeInfoDto, eventInfoDto, onjungInfoDto, storeHistoryDtos);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<StoreHistory, Long> {
List<StoreHistory> findByStoreOrderByActionDateDesc(Store store);
@Query("SELECT sh FROM StoreHistory sh WHERE sh.store = :store ORDER BY sh.actionDate DESC")
List<StoreHistory> findByStoreSortedByActionDate(@Param("store") Store store);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -64,64 +63,64 @@ public ReadOnjungEventOverviewResponseDto execute(

Onjung onjung = onjungService.createOnjung(donations, receipts, shares);

List<Object> sortedOnjungByCreatedAtDesc = onjungService.sortOnjungByCreatedAtDesc(onjung);
List<Object> sortedOnjungByCreatedAtDesc = onjungService.sortOnjungByCreatedAt(onjung);

List<ReadOnjungEventOverviewResponseDto.EventDto> 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);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ReadTicketResponseDto execute(
User user = userRepository.findById(accountId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE));

Page<Ticket> ticketsPage = ticketRepository.findByUserOrderByExpirationDateDesc(user, pageable);
Page<Ticket> ticketsPage = ticketRepository.findByUserOrderByExpirationDateAsc(user, pageable);

// dto로 변환
List<ReadTicketResponseDto.TicketDto> ticketDtos = ticketsPage.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface TicketRepository extends JpaRepository<Ticket, Long> {

// user로 Ticket 조회, expirationDate를 ASC로 정렬
Page<Ticket> findByUserOrderByExpirationDateDesc(User user, Pageable pageable);
Page<Ticket> findByUserOrderByExpirationDateAsc(User user, Pageable pageable);

// user로 Ticket 수 조회
Long countByUser(User user);
Expand Down

0 comments on commit 15b43cd

Please sign in to comment.