diff --git a/backend/src/main/java/com/isp/backend/domain/schedule/controller/ScheduleController.java b/backend/src/main/java/com/isp/backend/domain/schedule/controller/ScheduleController.java index 07380887..4548ce95 100644 --- a/backend/src/main/java/com/isp/backend/domain/schedule/controller/ScheduleController.java +++ b/backend/src/main/java/com/isp/backend/domain/schedule/controller/ScheduleController.java @@ -1,8 +1,9 @@ package com.isp.backend.domain.schedule.controller; +import com.isp.backend.domain.schedule.dto.response.FastestScheduleResponse; import com.isp.backend.domain.schedule.dto.response.ScheduleListResponse; import com.isp.backend.domain.schedule.dto.request.ScheduleSaveRequest; -import com.isp.backend.domain.schedule.service.ScheduleServiceImpl; +import com.isp.backend.domain.schedule.service.ScheduleService; import com.isp.backend.global.security.CustomUserDetails; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -16,13 +17,13 @@ @RequiredArgsConstructor public class ScheduleController { - private final ScheduleServiceImpl scheduleServiceImpl; + private final ScheduleService scheduleService; /** 여행 일정 저장 API **/ @PostMapping() public ResponseEntity saveSchedule(@AuthenticationPrincipal CustomUserDetails customUserDetails, @RequestBody ScheduleSaveRequest requestDTO) { - scheduleServiceImpl.saveSchedule(customUserDetails.getUsername(), requestDTO); + scheduleService.saveSchedule(customUserDetails.getUsername(), requestDTO); return ResponseEntity.ok().build(); } @@ -31,7 +32,7 @@ public ResponseEntity saveSchedule(@AuthenticationPrincipal CustomUserDeta @GetMapping() public ResponseEntity> getScheduleList(@AuthenticationPrincipal CustomUserDetails userDetails) { String memberUid = userDetails.getUsername(); - List scheduleList = scheduleServiceImpl.getScheduleList(memberUid); + List scheduleList = scheduleService.getScheduleList(memberUid); return ResponseEntity.ok(scheduleList); } @@ -41,7 +42,7 @@ public ResponseEntity> getScheduleList(@Authenticatio public ResponseEntity getScheduleDetail(@AuthenticationPrincipal CustomUserDetails userDetails, @PathVariable Long scheduleId) { String memberUid = userDetails.getUsername(); - ScheduleSaveRequest scheduleDetail = scheduleServiceImpl.getScheduleDetail(memberUid, scheduleId); + ScheduleSaveRequest scheduleDetail = scheduleService.getScheduleDetail(memberUid, scheduleId); return ResponseEntity.ok(scheduleDetail); } @@ -51,7 +52,7 @@ public ResponseEntity getScheduleDetail(@AuthenticationPrin public ResponseEntity updateSchedule(@AuthenticationPrincipal CustomUserDetails userDetails, @PathVariable Long scheduleId, @RequestBody ScheduleSaveRequest requestDTO) { - ScheduleSaveRequest updatedSchedule = scheduleServiceImpl.updateSchedule(userDetails.getUsername(), scheduleId, requestDTO); + ScheduleSaveRequest updatedSchedule = scheduleService.updateSchedule(userDetails.getUsername(), scheduleId, requestDTO); return ResponseEntity.ok(updatedSchedule); } @@ -61,9 +62,22 @@ public ResponseEntity updateSchedule(@AuthenticationPrincip public ResponseEntity deleteSchedule(@AuthenticationPrincipal CustomUserDetails userDetails, @PathVariable Long scheduleId) { String memberUid = userDetails.getUsername(); - scheduleServiceImpl.deleteSchedule(memberUid, scheduleId); + scheduleService.deleteSchedule(memberUid, scheduleId); return ResponseEntity.ok().build(); } + /** 나의 가까운 일정 조회 API **/ + @GetMapping("/dday") + public ResponseEntity getFastestSchedule(@AuthenticationPrincipal CustomUserDetails userDetails) { + String memberUid = userDetails.getUsername(); + FastestScheduleResponse fastestScheduleResponse = scheduleService.getFastestSchedule(memberUid); + return ResponseEntity.ok(fastestScheduleResponse); + } + + + /** 내가 최근 생성한 5개 일정 조회 API **/ +// public ResponseEntity<> + + } \ No newline at end of file diff --git a/backend/src/main/java/com/isp/backend/domain/schedule/dto/response/FastestScheduleResponse.java b/backend/src/main/java/com/isp/backend/domain/schedule/dto/response/FastestScheduleResponse.java new file mode 100644 index 00000000..37ed6fbb --- /dev/null +++ b/backend/src/main/java/com/isp/backend/domain/schedule/dto/response/FastestScheduleResponse.java @@ -0,0 +1,22 @@ +package com.isp.backend.domain.schedule.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class FastestScheduleResponse { + + private Long id; + + private String scheduleName; // 여행지 이름 + + private String dday; // 디데이 + + private String imageUrl; // 이미지 url + +} diff --git a/backend/src/main/java/com/isp/backend/domain/schedule/mapper/ScheduleMapper.java b/backend/src/main/java/com/isp/backend/domain/schedule/mapper/ScheduleMapper.java index e7a6902b..5e7c7462 100644 --- a/backend/src/main/java/com/isp/backend/domain/schedule/mapper/ScheduleMapper.java +++ b/backend/src/main/java/com/isp/backend/domain/schedule/mapper/ScheduleMapper.java @@ -4,12 +4,15 @@ import com.isp.backend.domain.member.entity.Member; import com.isp.backend.domain.schedule.dto.request.DailySchedule; import com.isp.backend.domain.schedule.dto.request.ScheduleDetail; +import com.isp.backend.domain.schedule.dto.response.FastestScheduleResponse; import com.isp.backend.domain.schedule.dto.response.ScheduleListResponse; import com.isp.backend.domain.schedule.dto.request.ScheduleSaveRequest; import com.isp.backend.domain.schedule.entity.Schedule; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; import java.util.Comparator; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -143,6 +146,33 @@ public List updateS } + /** + * D-day + **/ + public FastestScheduleResponse toFastestScheduleDTO(Schedule schedule) { + FastestScheduleResponse response = new FastestScheduleResponse(); + response.setId(schedule.getId()); + response.setScheduleName(schedule.getScheduleName()); + response.setDday(calculateDday(schedule.getStartDate())); + String imageUrl = schedule.getCountry().getImageUrl(); + response.setImageUrl(imageUrl); + + return response; + } + + + /** + * D-day 계산 메서드 + **/ + private String calculateDday(String startDateString) { + LocalDate today = LocalDate.now(); + LocalDate startDate = LocalDate.parse(startDateString); + long dday = ChronoUnit.DAYS.between(today, startDate) + 1; // 오늘-시작일까지의 날짜 수 계산 + return Long.toString(dday); + } + + + } diff --git a/backend/src/main/java/com/isp/backend/domain/schedule/service/ScheduleService.java b/backend/src/main/java/com/isp/backend/domain/schedule/service/ScheduleService.java index 6c2e5682..9dcf87a2 100644 --- a/backend/src/main/java/com/isp/backend/domain/schedule/service/ScheduleService.java +++ b/backend/src/main/java/com/isp/backend/domain/schedule/service/ScheduleService.java @@ -2,6 +2,7 @@ import com.isp.backend.domain.country.entity.Country; import com.isp.backend.domain.member.entity.Member; +import com.isp.backend.domain.schedule.dto.response.FastestScheduleResponse; import com.isp.backend.domain.schedule.dto.response.ScheduleListResponse; import com.isp.backend.domain.schedule.dto.request.ScheduleSaveRequest; import com.isp.backend.domain.schedule.entity.Schedule; @@ -18,6 +19,8 @@ public interface ScheduleService { void deleteSchedule(String uid, Long scheduleId); + FastestScheduleResponse getFastestSchedule(String uid); + ScheduleSaveRequest updateSchedule(String uid, Long scheduleId, ScheduleSaveRequest updateRequestDTO); void calculateTotalPrice(Schedule schedule); diff --git a/backend/src/main/java/com/isp/backend/domain/schedule/service/ScheduleServiceImpl.java b/backend/src/main/java/com/isp/backend/domain/schedule/service/ScheduleServiceImpl.java index cff485cd..72617171 100644 --- a/backend/src/main/java/com/isp/backend/domain/schedule/service/ScheduleServiceImpl.java +++ b/backend/src/main/java/com/isp/backend/domain/schedule/service/ScheduleServiceImpl.java @@ -5,6 +5,7 @@ import com.isp.backend.domain.member.dto.response.MemberDetailResponse; import com.isp.backend.domain.member.entity.Member; import com.isp.backend.domain.member.repository.MemberRepository; +import com.isp.backend.domain.schedule.dto.response.FastestScheduleResponse; import com.isp.backend.domain.schedule.dto.response.ScheduleListResponse; import com.isp.backend.domain.schedule.dto.request.ScheduleSaveRequest; import com.isp.backend.domain.schedule.entity.Schedule; @@ -21,7 +22,11 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import java.util.Comparator; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Slf4j @@ -127,6 +132,31 @@ public void deleteSchedule(String uid, Long scheduleId) { scheduleRepository.save(findSchedule); } + /** 나의 여행 D-day 출력 **/ + public FastestScheduleResponse getFastestSchedule(String uid) { + Member findMember = validateUserCheck(uid); + List schedules = scheduleRepository.findSchedulesByMember(findMember); + + // 가장 가까운 여행 일정 찾기 + LocalDate today = LocalDate.now(); + Optional closestScheduleOptional = findClosestSchedule(schedules, today); + + if (closestScheduleOptional.isPresent()) { + Schedule closestSchedule = closestScheduleOptional.get(); + return scheduleMapper.toFastestScheduleDTO(closestSchedule); + } else { + throw new ScheduleNotFoundException(); + } + } + + + /** 가장 가까운 일정 찾기 **/ + private Optional findClosestSchedule(List schedules, LocalDate today) { + return schedules.stream() + .filter(schedule -> LocalDate.parse(schedule.getStartDate()).isAfter(today)) // 오늘 이후의 일정 + .min(Comparator.comparing(s -> LocalDate.parse(s.getStartDate()))); // 시작일이 가장 빠른 순으로 정렬 + } + /** 여행 일정 총 경비 계산 **/ @Override