Skip to content

Commit

Permalink
[feat] #172 - promotion 필드 추가 및 지난 공연 캐러샐 자동 삭제 구현, response에 dueDate…
Browse files Browse the repository at this point in the history
… 추가 (#175)

* [#172] feat(BeatApplication): cron 관련 annotation 추가

* [#172] feat(Promotion): promotion entity에 추가로 필요한 필드 추가

* [#172] feat(PromotionService): 지나간 공연에 해당하는 promotion 캐러셀 삭제하는 로직 구현

* [#172] feat(PerformanceDetailResponse): 공연 상세 정보 조회 response에 minDueDate 추가

* [#172] feat(PerformanceDetailResponse): 공연 상세 정보 조회 response에 schedule별 dueDate 추가

* [#172] feat(BookingPerformanceDetailSchedule): 예매하기에 필요한 공연 상세 정보 조회 response에 schedule별 dueDate 추가

* [#172] feat(MakerPerformanceDetail): 등록한 공연 조회 response에 MinDueDate 추가

* [#172] feat(PerformanceService): response에 추가한 필드 반영 및 등록한 공연 조회 정렬 로직 구현

* [#172] refactor(PromotionService): checkAndDeleteInvalidPromotions 성능 및 확장성 고려한 refactor
  • Loading branch information
hyerinhwang-sailin authored Aug 16, 2024
1 parent dae27a6 commit fe0bb64
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 32 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/beat/BeatApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableFeignClients
@EnableScheduling
@ImportAutoConfiguration({FeignAutoConfiguration.class})
public class BeatApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -58,11 +59,17 @@ public PerformanceDetailResponse getPerformanceDetail(Long performanceId) {
.orElseThrow(() -> new NotFoundException(PerformanceErrorCode.PERFORMANCE_NOT_FOUND));

List<PerformanceDetailSchedule> scheduleList = scheduleRepository.findByPerformanceId(performanceId).stream()
.map(schedule -> PerformanceDetailSchedule.of(
schedule.getId(),
schedule.getPerformanceDate(),
schedule.getScheduleNumber().name()
)).collect(Collectors.toList());
.map(schedule -> {
int dueDate = scheduleService.calculateDueDate(schedule);
return PerformanceDetailSchedule.of(
schedule.getId(),
schedule.getPerformanceDate(),
schedule.getScheduleNumber().name(),
dueDate
);
}).collect(Collectors.toList());

int minDueDate = scheduleService.getMinDueDate(scheduleRepository.findAllByPerformanceId(performanceId));

List<PerformanceDetailCast> castList = castRepository.findByPerformanceId(performanceId).stream()
.map(cast -> PerformanceDetailCast.of(
Expand Down Expand Up @@ -95,7 +102,8 @@ public PerformanceDetailResponse getPerformanceDetail(Long performanceId) {
performance.getPerformanceContact(),
performance.getPerformanceTeamName(),
castList,
staffList
staffList,
minDueDate
);
}

Expand All @@ -107,12 +115,14 @@ public BookingPerformanceDetailResponse getBookingPerformanceDetail(Long perform
List<BookingPerformanceDetailSchedule> scheduleList = scheduleRepository.findByPerformanceId(performanceId).stream()
.map(schedule -> {
scheduleService.updateBookingStatus(schedule);
int dueDate = scheduleService.calculateDueDate(schedule);
return BookingPerformanceDetailSchedule.of(
schedule.getId(),
schedule.getPerformanceDate(),
schedule.getScheduleNumber().name(),
scheduleService.getAvailableTicketCount(schedule),
schedule.isBooking()
schedule.isBooking(),
dueDate
);
}).collect(Collectors.toList());

Expand Down Expand Up @@ -206,18 +216,40 @@ public MakerPerformanceResponse getMemberPerformances(Long memberId) {
List<Performance> performances = performanceRepository.findByUsersId(user.getId());

List<MakerPerformanceDetail> performanceDetails = performances.stream()
.map(performance -> MakerPerformanceDetail.of(
performance.getId(),
performance.getGenre().name(),
performance.getPerformanceTitle(),
performance.getPosterImage(),
performance.getPerformancePeriod()
))
.map(performance -> {
List<Schedule> schedules = scheduleRepository.findByPerformanceId(performance.getId());
int minDueDate = scheduleService.getMinDueDate(schedules);

return MakerPerformanceDetail.of(
performance.getId(),
performance.getGenre().name(),
performance.getPerformanceTitle(),
performance.getPosterImage(),
performance.getPerformancePeriod(),
minDueDate
);
})
.collect(Collectors.toList());

return MakerPerformanceResponse.of(user.getId(), performanceDetails);
// 양수 minDueDate 정렬
List<MakerPerformanceDetail> positiveDueDates = performanceDetails.stream()
.filter(detail -> detail.minDueDate() >= 0)
.sorted(Comparator.comparingInt(MakerPerformanceDetail::minDueDate))
.collect(Collectors.toList());

// 음수 minDueDate 정렬
List<MakerPerformanceDetail> negativeDueDates = performanceDetails.stream()
.filter(detail -> detail.minDueDate() < 0)
.sorted(Comparator.comparingInt(MakerPerformanceDetail::minDueDate).reversed())
.collect(Collectors.toList());

// 병합된 리스트
positiveDueDates.addAll(negativeDueDates);

return MakerPerformanceResponse.of(user.getId(), positiveDueDates);
}


@Transactional
public PerformanceEditResponse getPerformanceEdit(Long memberId, Long performanceId) {
Member member = memberRepository.findById(memberId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ public record BookingPerformanceDetailSchedule(
LocalDateTime performanceDate,
String scheduleNumber,
int availableTicketCount,
boolean isBooking
boolean isBooking,
int dueDate
) {
public static BookingPerformanceDetailSchedule of(Long scheduleId, LocalDateTime performanceDate, String scheduleNumber, int availableTicketCount, boolean isBooking) {
return new BookingPerformanceDetailSchedule(scheduleId, performanceDate, scheduleNumber, availableTicketCount, isBooking);
public static BookingPerformanceDetailSchedule of(Long scheduleId, LocalDateTime performanceDate, String scheduleNumber, int availableTicketCount, boolean isBooking, int dueDate) {
return new BookingPerformanceDetailSchedule(scheduleId, performanceDate, scheduleNumber, availableTicketCount, isBooking, dueDate);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ public record MakerPerformanceDetail(
String genre,
String performanceTitle,
String posterImage,
String performancePeriod
String performancePeriod,
int minDueDate
) {
public static MakerPerformanceDetail of(
Long performanceId,
String genre,
String performanceTitle,
String posterImage,
String performancePeriod) {
return new MakerPerformanceDetail(performanceId, genre, performanceTitle, posterImage, performancePeriod);
String performancePeriod,
int minDueDate) { // minDueDate 매개변수 추가
return new MakerPerformanceDetail(performanceId, genre, performanceTitle, posterImage, performancePeriod, minDueDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public record PerformanceDetailResponse(
String performanceContact,
String performanceTeamName,
List<PerformanceDetailCast> castList,
List<PerformanceDetailStaff> staffList
List<PerformanceDetailStaff> staffList,
int minDueDate
) {
public static PerformanceDetailResponse of(
Long performanceId,
Expand All @@ -34,10 +35,26 @@ public static PerformanceDetailResponse of(
String performanceContact,
String performanceTeamName,
List<PerformanceDetailCast> castList,
List<PerformanceDetailStaff> staffList
List<PerformanceDetailStaff> staffList,
int minDueDate
) {
return new PerformanceDetailResponse(performanceId, performanceTitle, performancePeriod, scheduleList, ticketPrice, genre, posterImage, runningTime, performanceVenue, performanceDescription, performanceAttentionNote, performanceContact, performanceTeamName, castList, staffList);
return new PerformanceDetailResponse(
performanceId,
performanceTitle,
performancePeriod,
scheduleList,
ticketPrice,
genre,
posterImage,
runningTime,
performanceVenue,
performanceDescription,
performanceAttentionNote,
performanceContact,
performanceTeamName,
castList,
staffList,
minDueDate
);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
public record PerformanceDetailSchedule(
Long scheduleId,
LocalDateTime performanceDate,
String scheduleNumber
String scheduleNumber,
int dueDate
) {
public static PerformanceDetailSchedule of(Long scheduleId, LocalDateTime performanceDate, String scheduleNumber) {
return new PerformanceDetailSchedule(scheduleId, performanceDate, scheduleNumber);
public static PerformanceDetailSchedule of(Long scheduleId, LocalDateTime performanceDate, String scheduleNumber, int dueDate) {
return new PerformanceDetailSchedule(scheduleId, performanceDate, scheduleNumber, dueDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.beat.domain.promotion.application;

import com.beat.domain.performance.domain.Performance;
import com.beat.domain.promotion.dao.PromotionRepository;
import com.beat.domain.promotion.domain.Promotion;
import com.beat.domain.schedule.application.ScheduleService;
import com.beat.domain.schedule.dao.ScheduleRepository;
import com.beat.domain.schedule.domain.Schedule;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PromotionService {

private final PromotionRepository promotionRepository;
private final ScheduleRepository scheduleRepository;
private final ScheduleService scheduleService;

@Scheduled(cron = "1 0 0 * * ?")
@Transactional
public void checkAndDeleteInvalidPromotions() {
List<Promotion> promotions = promotionRepository.findAll();

for (Promotion promotion : promotions) {
Performance performance = promotion.getPerformance();

if (performance == null) {
return;
}

List<Schedule> schedules = scheduleRepository.findByPerformanceId(performance.getId());
int minDueDate = scheduleService.getMinDueDate(schedules);

if (minDueDate < 0) {
promotionRepository.delete(promotion);
}
}
}
}
16 changes: 13 additions & 3 deletions src/main/java/com/beat/domain/promotion/domain/Promotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,29 @@ public class Promotion {
private String promotionPhoto;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "performance_id", nullable = false)
@JoinColumn(name = "performance_id", nullable = true)
private Performance performance;

@Column(nullable = true)
private String redirectUrl;

@Column(nullable = false)
private boolean isExternal;

@Builder
public Promotion(String promotionPhoto, Performance performance) {
public Promotion(String promotionPhoto, Performance performance, String redirectUrl, boolean isExternal) {
this.promotionPhoto = promotionPhoto;
this.performance = performance;
this.redirectUrl = redirectUrl;
this.isExternal = isExternal;
}

public static Promotion create(String promotionPhoto, Performance performance) {
public static Promotion create(String promotionPhoto, Performance performance, String redirectUrl, boolean isExternal) {
return Promotion.builder()
.promotionPhoto(promotionPhoto)
.performance(performance)
.redirectUrl(redirectUrl)
.isExternal(isExternal)
.build();
}
}

0 comments on commit fe0bb64

Please sign in to comment.