Skip to content

Commit

Permalink
Merge pull request #155 from TrainingDiary/fix/existing-schedule-load
Browse files Browse the repository at this point in the history
일정 열기 및 일정 등록 시 기존 일정을 가져올 때, 해당 트레이너의 일정만 가져오도록 함
  • Loading branch information
marcel1315 authored Jul 26, 2024
2 parents 6b4fde0 + ae8a83f commit 9b9bbdc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,42 @@
import java.util.Set;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ScheduleRepository extends JpaRepository<ScheduleEntity, Long>,
ScheduleRepositoryCustom {

@Query("select s.startAt "
+ "from schedule s "
+ "where s.startAt >= ?1 "
+ "and s.startAt <= ?2")
Set<LocalDateTime> findScheduleDatesByDates(LocalDateTime startAt1, LocalDateTime startAt2);
+ "where s.trainer.id = :id "
+ "and s.startAt >= :startAt1 "
+ "and s.startAt <= :startAt2")
Set<LocalDateTime> findScheduleDatesByDates(
@Param("id") long id,
@Param("startAt1") LocalDateTime startAt1,
@Param("startAt2") LocalDateTime startAt2
);

@Query("select s "
+ "from schedule s "
+ "where s.startAt >= ?1 "
+ "and s.startAt <= ?2")
List<ScheduleEntity> findByDates(LocalDateTime startAt1, LocalDateTime startAt2);
+ "where s.trainer.id = :id "
+ "and s.startAt >= :startAt1 "
+ "and s.startAt <= :startAt2")
List<ScheduleEntity> findByDates(
@Param("id") long id,
@Param("startAt1") LocalDateTime startAt1,
@Param("startAt2") LocalDateTime startAt2
);

@Query("select s "
+ "from schedule s "
+ "left join fetch s.trainer "
+ "left join fetch s.ptContract "
+ "left join fetch s.ptContract.trainee "
+ "where s.startAt >= ?1 "
+ "and s.startAt <= ?2")
List<ScheduleEntity> findByDatesWithDetails(LocalDateTime startAt1, LocalDateTime startAt2);
+ "where s.startAt >= :startAt1 "
+ "and s.startAt <= :startAt2")
List<ScheduleEntity> findByDatesWithDetails(
@Param("startAt1") LocalDateTime startAt1,
@Param("startAt2") LocalDateTime startAt2
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void createSchedule(OpenScheduleRequestDto dto) {

List<LocalDateTime> requestedStartTimes = getRequestedTimes(dto.getDateTimes());
Set<LocalDateTime> existings = scheduleRepository.findScheduleDatesByDates(
trainer.getId(),
getEarliest(requestedStartTimes),
getLatest(requestedStartTimes)
);
Expand Down Expand Up @@ -106,6 +107,7 @@ public RegisterScheduleResponseDto registerSchedule(RegisterScheduleRequestDto d

List<LocalDateTime> requestedStartTimes = getRequestedTimes(dto.getDateTimes());
Set<LocalDateTime> existingStartTimes = scheduleRepository.findScheduleDatesByDates(
trainer.getId(),
getEarliest(requestedStartTimes),
getLatest(requestedStartTimes)
);
Expand All @@ -123,7 +125,7 @@ public RegisterScheduleResponseDto registerSchedule(RegisterScheduleRequestDto d

// 이미 존재하는 일정은 기존 일정을 사용해서 업데이트
List<ScheduleEntity> existingSchedules = updateExistingSchedules(
existingStartTimes, ptContract
trainer.getId(), existingStartTimes, ptContract
);

// 일정 등록
Expand Down Expand Up @@ -168,11 +170,12 @@ private List<ScheduleEntity> createNewSchedules(
}

private List<ScheduleEntity> updateExistingSchedules(
long trainerId,
Set<LocalDateTime> existingsStartTimes,
PtContractEntity ptContract
) {
return existingsStartTimes.stream()
.map(time -> scheduleRepository.findByDates(time, time).stream()
.map(time -> scheduleRepository.findByDates(trainerId, time, time).stream()
.findFirst()
.orElseThrow(ScheduleNotFoundException::new)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ void openScheduleFail_AlreadyExistSchedule() {
.build();

when(scheduleRepository.findScheduleDatesByDates(
eq(1L),
eq(LocalDateTime.of(2024, 1, 1, 10, 0)),
eq(LocalDateTime.of(2024, 2, 28, 22, 0))
))
Expand Down Expand Up @@ -414,6 +415,7 @@ void registerScheduleSuccess_NoOpen() {
));

when(scheduleRepository.findScheduleDatesByDates(
1L,
LocalDateTime.of(2024, 1, 2, 20, 0),
LocalDateTime.of(2024, 1, 8, 22, 0)
))
Expand Down Expand Up @@ -461,6 +463,7 @@ void registerScheduleSuccess_Open() {
));

when(scheduleRepository.findScheduleDatesByDates(
1L,
LocalDateTime.of(2024, 1, 2, 20, 0),
LocalDateTime.of(2024, 1, 8, 22, 0)
))
Expand All @@ -469,6 +472,7 @@ void registerScheduleSuccess_Open() {
));

when(scheduleRepository.findByDates(
1L,
LocalDateTime.of(2024, 1, 2, 20, 0),
LocalDateTime.of(2024, 1, 2, 20, 0)
))
Expand Down Expand Up @@ -521,6 +525,7 @@ void registerScheduleFail_ScheduleExistButNotOpen() {
));

when(scheduleRepository.findScheduleDatesByDates(
1L,
LocalDateTime.of(2024, 1, 2, 20, 0),
LocalDateTime.of(2024, 1, 8, 22, 0)
))
Expand All @@ -529,6 +534,7 @@ void registerScheduleFail_ScheduleExistButNotOpen() {
));

when(scheduleRepository.findByDates(
1L,
LocalDateTime.of(2024, 1, 2, 20, 0),
LocalDateTime.of(2024, 1, 2, 20, 0)
))
Expand Down Expand Up @@ -591,6 +597,7 @@ void registerScheduleFail_SessionNotEnough() {
));

when(scheduleRepository.findScheduleDatesByDates(
1L,
LocalDateTime.of(2024, 1, 2, 20, 0),
LocalDateTime.of(2024, 1, 8, 22, 0)
))
Expand Down

0 comments on commit 9b9bbdc

Please sign in to comment.