Skip to content

Commit

Permalink
Merge pull request #77 from tukcomCD2024/backend/feature/76-schedule-…
Browse files Browse the repository at this point in the history
…dday

[Backend] feat : 나의 여행 스케줄 D-day API 추가
  • Loading branch information
Dayon-Hong authored May 15, 2024
2 parents 1fda51b + 99fff48 commit 48030c2
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,13 +17,13 @@
@RequiredArgsConstructor
public class ScheduleController {

private final ScheduleServiceImpl scheduleServiceImpl;
private final ScheduleService scheduleService;

/** 여행 일정 저장 API **/
@PostMapping()
public ResponseEntity<Void> saveSchedule(@AuthenticationPrincipal CustomUserDetails customUserDetails,
@RequestBody ScheduleSaveRequest requestDTO) {
scheduleServiceImpl.saveSchedule(customUserDetails.getUsername(), requestDTO);
scheduleService.saveSchedule(customUserDetails.getUsername(), requestDTO);
return ResponseEntity.ok().build();
}

Expand All @@ -31,7 +32,7 @@ public ResponseEntity<Void> saveSchedule(@AuthenticationPrincipal CustomUserDeta
@GetMapping()
public ResponseEntity<List<ScheduleListResponse>> getScheduleList(@AuthenticationPrincipal CustomUserDetails userDetails) {
String memberUid = userDetails.getUsername();
List<ScheduleListResponse> scheduleList = scheduleServiceImpl.getScheduleList(memberUid);
List<ScheduleListResponse> scheduleList = scheduleService.getScheduleList(memberUid);
return ResponseEntity.ok(scheduleList);
}

Expand All @@ -41,7 +42,7 @@ public ResponseEntity<List<ScheduleListResponse>> getScheduleList(@Authenticatio
public ResponseEntity<ScheduleSaveRequest> 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);
}

Expand All @@ -51,7 +52,7 @@ public ResponseEntity<ScheduleSaveRequest> getScheduleDetail(@AuthenticationPrin
public ResponseEntity<ScheduleSaveRequest> 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);
}

Expand All @@ -61,9 +62,22 @@ public ResponseEntity<ScheduleSaveRequest> updateSchedule(@AuthenticationPrincip
public ResponseEntity<Void> 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<FastestScheduleResponse> getFastestSchedule(@AuthenticationPrincipal CustomUserDetails userDetails) {
String memberUid = userDetails.getUsername();
FastestScheduleResponse fastestScheduleResponse = scheduleService.getFastestSchedule(memberUid);
return ResponseEntity.ok(fastestScheduleResponse);
}


/** 내가 최근 생성한 5개 일정 조회 API **/
// public ResponseEntity<>


}
Original file line number Diff line number Diff line change
@@ -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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,6 +146,33 @@ public List<com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail> 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);
}



}


Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<Schedule> schedules = scheduleRepository.findSchedulesByMember(findMember);

// 가장 가까운 여행 일정 찾기
LocalDate today = LocalDate.now();
Optional<Schedule> closestScheduleOptional = findClosestSchedule(schedules, today);

if (closestScheduleOptional.isPresent()) {
Schedule closestSchedule = closestScheduleOptional.get();
return scheduleMapper.toFastestScheduleDTO(closestSchedule);
} else {
throw new ScheduleNotFoundException();
}
}


/** 가장 가까운 일정 찾기 **/
private Optional<Schedule> findClosestSchedule(List<Schedule> schedules, LocalDate today) {
return schedules.stream()
.filter(schedule -> LocalDate.parse(schedule.getStartDate()).isAfter(today)) // 오늘 이후의 일정
.min(Comparator.comparing(s -> LocalDate.parse(s.getStartDate()))); // 시작일이 가장 빠른 순으로 정렬
}


/** 여행 일정 총 경비 계산 **/
@Override
Expand Down

0 comments on commit 48030c2

Please sign in to comment.