diff --git a/src/main/java/project/backend/domain/culturalevent/controller/CulturalEventController.java b/src/main/java/project/backend/domain/culturalevent/controller/CulturalEventController.java index e0d0275..4ea4e69 100644 --- a/src/main/java/project/backend/domain/culturalevent/controller/CulturalEventController.java +++ b/src/main/java/project/backend/domain/culturalevent/controller/CulturalEventController.java @@ -34,19 +34,20 @@ public class CulturalEventController { private final MemberJwtService memberJwtService; @ApiOperation(value = "문화생활 리스트 조회", - notes = " - ordering : point(인기순)") + notes = " - ordering : ticketOpenDate(마감 다가온 순) | -point(인기순)") @GetMapping public ResponseEntity getCulturalEventList( - @RequestParam() CategoryTitle type, + @RequestParam(required = false) CategoryTitle type, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size, - @RequestParam(required = false) String ordering + @RequestParam(required = false) String ordering, + @RequestParam(required = false) Boolean isOpened ) { // Member Member member = memberJwtService.getMember(); // Response - List culturalEventList = culturalEventService.getCulturalEventList(type, page, size, ordering); + List culturalEventList = culturalEventService.getCulturalEventList(page, size, type, ordering, isOpened); List culturalEventResponseDtoList = culturalEventMapper .culturalEventToCulturalEventListDtos(culturalEventList); culturalEventResponseDtoList.forEach(dto -> { diff --git a/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepository.java b/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepository.java index 49de24d..80460c2 100644 --- a/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepository.java +++ b/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepository.java @@ -9,9 +9,8 @@ import java.util.Optional; -public interface CulturalEventRepository extends JpaRepository { +public interface CulturalEventRepository extends JpaRepository, CulturalEventRepositoryCustom { Optional findFirstByTitle(String title); Page findAll(Pageable pageable); - Page findAllByCulturalEventCategory(Pageable pageable, CulturalEventCategory culturalEventCategory); } diff --git a/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepositoryCustom.java b/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepositoryCustom.java new file mode 100644 index 0000000..0ef8bf1 --- /dev/null +++ b/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepositoryCustom.java @@ -0,0 +1,15 @@ +package project.backend.domain.culturalevent.repository; + +import project.backend.domain.culturalevent.entity.CulturalEvent; +import project.backend.domain.culturalevnetcategory.entity.CategoryTitle; +import project.backend.domain.member.dto.MemberStatisticsResponseDto; +import project.backend.domain.member.dto.MemberYearStatisticsResponseDto; +import project.backend.domain.member.entity.Member; +import project.backend.domain.ticket.entity.Ticket; + +import java.time.LocalDateTime; +import java.util.List; + +public interface CulturalEventRepositoryCustom { + List getCulturalEventList(int page, int size, CategoryTitle category, String ordering, Boolean isOpened); +} diff --git a/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepositoryImpl.java b/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepositoryImpl.java new file mode 100644 index 0000000..853b957 --- /dev/null +++ b/src/main/java/project/backend/domain/culturalevent/repository/CulturalEventRepositoryImpl.java @@ -0,0 +1,57 @@ +package project.backend.domain.culturalevent.repository; + +import com.querydsl.jpa.impl.JPAQuery; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import project.backend.domain.culturalevent.entity.CulturalEvent; +import project.backend.domain.culturalevnetcategory.entity.CategoryTitle; + +import java.time.LocalDateTime; +import java.util.List; + + +import static project.backend.domain.culturalevent.entity.QCulturalEvent.culturalEvent; + +@RequiredArgsConstructor +public class CulturalEventRepositoryImpl implements CulturalEventRepositoryCustom { + private final JPAQueryFactory queryFactory; + + @Override + public List getCulturalEventList(int page, int size, CategoryTitle category, String ordering, Boolean isOpened) { + LocalDateTime now = LocalDateTime.now(); + JPAQuery culturalEventJPAQuery = queryFactory.selectFrom(culturalEvent); + + // ordering 있을 경우 + if (ordering != null) { + if (ordering.equals("-point")) { + culturalEventJPAQuery.orderBy(culturalEvent.point.desc()); + } if (ordering.equals("ticketOpenDate")) { + culturalEventJPAQuery.orderBy(culturalEvent.ticketOpenDate.asc()); + } + } + + // isOpened 있을 경우 + if (isOpened != null) { + if (isOpened) { + culturalEventJPAQuery.where(culturalEvent.ticketOpenDate.before(now)); + } else { + culturalEventJPAQuery.where(culturalEvent.ticketOpenDate.after(now)); + } + } + + // category 있을 경우 + if (!(category == CategoryTitle.ALL || category == null)) { + culturalEventJPAQuery.where(culturalEvent.culturalEventCategory.title.eq(category)); + } + + // page, size 적용 + Pageable pageable = PageRequest.of(page, size); + culturalEventJPAQuery.offset(pageable.getOffset()); + culturalEventJPAQuery.limit(pageable.getPageSize()); + + // Result + return culturalEventJPAQuery.fetch(); + } +} diff --git a/src/main/java/project/backend/domain/culturalevent/service/CulturalEventService.java b/src/main/java/project/backend/domain/culturalevent/service/CulturalEventService.java index fb46c04..4ac547b 100644 --- a/src/main/java/project/backend/domain/culturalevent/service/CulturalEventService.java +++ b/src/main/java/project/backend/domain/culturalevent/service/CulturalEventService.java @@ -36,14 +36,8 @@ public class CulturalEventService { private final MemberJwtService memberJwtService; private final CulturalEventVisitRepository culturalEventVisitRepository; - public List getCulturalEventList(CategoryTitle type, int page, int size, String ordering) { - Pageable pageable = PageRequest.of(page, size, createSort(ordering)); - if (type == CategoryTitle.ALL || type == null) { - return culturalEventRepository.findAll(pageable).getContent(); - } else { - CulturalEventCategory culturalEventCategory = culturalEventCategoryService.verifiedCulturalEventCategoryByTitle(type); - return culturalEventRepository.findAllByCulturalEventCategory(pageable, culturalEventCategory).getContent(); - } + public List getCulturalEventList(int page, int size, CategoryTitle category, String ordering, Boolean isOpened) { + return culturalEventRepository.getCulturalEventList(page, size, category, ordering, isOpened); } public CulturalEvent getCulturalEvent(Long id) {