Skip to content

Commit

Permalink
feat : 최근에 생성한 여행 일정
Browse files Browse the repository at this point in the history
  • Loading branch information
Dayon-Hong committed May 28, 2024
1 parent ee8c39b commit e609e4a
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public String generateSkyscannerUrl(SkyScannerRequest request) {
if (request.getTransferCount() == 0) {
url += "&preferdirects=true";
} else if (request.getTransferCount() == 1) {
url += "&preferdirects=false&stops=!direct";
url += "&preferdirects=false&stops=!direct";
} else {
url += "&preferdirects=false&stops=!direct,!oneStop,!oneStop";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.isp.backend.domain.schedule.controller;

import com.isp.backend.domain.schedule.dto.response.FastestScheduleResponse;
import com.isp.backend.domain.schedule.dto.response.LatestCreateResponse;
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.ScheduleService;
Expand Down Expand Up @@ -77,7 +78,14 @@ public ResponseEntity<FastestScheduleResponse> getFastestSchedule(@Authenticatio


/** 내가 최근 생성한 5개 일정 조회 API **/
// public ResponseEntity<>
@GetMapping("/latest")
public ResponseEntity<List<LatestCreateResponse>> getLatestCreatedSchedules(
@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestParam(value = "limit", defaultValue = "6") int limit) { // 출력할 세부 일정 개수 조절
String memberUid = userDetails.getUsername();
List<LatestCreateResponse> responses = scheduleService.getLatestCreatedSchedules(memberUid, limit);
return ResponseEntity.ok(responses);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class DailySchedule {

private String date; // 일정 날짜

private List<ScheduleDetail> schedules; // 해당 날짜의 일정 목록
private List<ScheduleDetailRequest> schedules; // 해당 날짜의 일정 목록

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ScheduleDetail {
public class ScheduleDetailRequest {

private String todo; // 할 일

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class FastestScheduleResponse {

private String scheduleName; // 여행지 이름

private String dday; // 디데이
private String dday;

private String imageUrl; // 이미지 url
private String imageUrl;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.isp.backend.domain.schedule.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class LatestCreateResponse {

private Long id;

private String scheduleName;

private String city; // 여행지 장소

private String imageUrl; // 이미지 url

private List<String> plan ; // 여행 일정 리스트

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.isp.backend.domain.country.entity.Country;
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.request.ScheduleDetailRequest;
import com.isp.backend.domain.schedule.dto.response.FastestScheduleResponse;
import com.isp.backend.domain.schedule.dto.response.LatestCreateResponse;
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 com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -25,7 +27,7 @@ public class ScheduleMapper {
/**
* 일정 저장
**/
// 여행 일정 요청 DTO -> 엔티티로 변환
// 여행 일정 요청 DTO -> 엔티티로 변환
public Schedule toScheduleEntity(ScheduleSaveRequest scheduleSaveRequest, Member member, Country country) {
// 여행 일정 엔티티 생성
Schedule schedule = Schedule.builder()
Expand All @@ -37,11 +39,11 @@ public Schedule toScheduleEntity(ScheduleSaveRequest scheduleSaveRequest, Member
.build();

// 여행 일정 세부 항목 리스트 생성
List<com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail> scheduleDetails = scheduleSaveRequest.getDailySchedules().stream()
List<ScheduleDetail> scheduleDetails = scheduleSaveRequest.getDailySchedules().stream()
.flatMap(dailySchedule -> {
AtomicInteger num = new AtomicInteger(1); // 날짜별 일정 순서 카운터
return dailySchedule.getSchedules().stream()
.map(scheduleDetail -> toScheduleDetailEntity(scheduleDetail, dailySchedule, schedule, num.getAndIncrement()));
.map(scheduleDetailDTO -> toScheduleDetailEntity(scheduleDetailDTO, dailySchedule, schedule, num.getAndIncrement()));
})
.collect(Collectors.toList());

Expand All @@ -52,8 +54,8 @@ public Schedule toScheduleEntity(ScheduleSaveRequest scheduleSaveRequest, Member
}

// 일정 세부 DTO를 엔티티로 변환
private com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail toScheduleDetailEntity(ScheduleDetail scheduleDetailDTO, DailySchedule dailySchedule, Schedule schedule, int num) {
com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail scheduleDetail = com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail.builder()
private ScheduleDetail toScheduleDetailEntity(ScheduleDetailRequest scheduleDetailDTO, DailySchedule dailySchedule, Schedule schedule, int num) {
ScheduleDetail scheduleDetail = ScheduleDetail.builder()
.todo(scheduleDetailDTO.getTodo())
.place(scheduleDetailDTO.getPlace())
.budget(scheduleDetailDTO.getBudget())
Expand Down Expand Up @@ -96,13 +98,13 @@ public ScheduleListResponse toScheduleListResponseDTO(Schedule schedule) {
public ScheduleSaveRequest toScheduleResponseDTO(Schedule schedule) {
// 일정 세부를 날짜별로 그룹화하고, 날짜를 기준으로 정렬
List<DailySchedule> sortedDailySchedules = schedule.getScheduleDetails().stream()
.sorted(Comparator.comparing(com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail::getDate))
.collect(Collectors.groupingBy(com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail::getDate))
.sorted(Comparator.comparing(ScheduleDetail::getDate))
.collect(Collectors.groupingBy(ScheduleDetail::getDate))
.entrySet().stream()
.map(entry -> new DailySchedule(
entry.getKey(),
entry.getValue().stream()
.sorted(Comparator.comparingInt(com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail::getNum))
.sorted(Comparator.comparingInt(ScheduleDetail::getNum))
.map(this::toScheduleDetailDTO)
.collect(Collectors.toList())
))
Expand All @@ -111,31 +113,32 @@ public ScheduleSaveRequest toScheduleResponseDTO(Schedule schedule) {

return new ScheduleSaveRequest(
schedule.getScheduleName(),
schedule.getCountry().getCity(),
schedule.getCountry().getCity(), // 국가에서 도시 가져오기로 수정
schedule.getStartDate(),
schedule.getEndDate(),
sortedDailySchedules
);
}

// ScheduleDetail 엔티티를 ScheduleDetailDTO로 변환하는 메서드
private ScheduleDetail toScheduleDetailDTO(com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail scheduleDetail) {
return new ScheduleDetail(
private ScheduleDetailRequest toScheduleDetailDTO(ScheduleDetail scheduleDetail) {
ScheduleDetailRequest scheduleDetailDTO = new ScheduleDetailRequest(
scheduleDetail.getTodo(),
scheduleDetail.getPlace(),
scheduleDetail.getScheduleType(),
scheduleDetail.getBudget(),
scheduleDetail.getLatitude(),
scheduleDetail.getLongitude()
);
return scheduleDetailDTO;
}


/**
* 일정 수정
**/
// ScheduleDetailDTO 목록을 ScheduleDetail 엔티티 목록으로 변환하는 메서드
public List<com.isp.backend.domain.scheduleDetail.entity.ScheduleDetail> updateSchedulesEntity(ScheduleSaveRequest scheduleSaveRequest, Schedule schedule) {
public List<ScheduleDetail> updateSchedulesEntity(ScheduleSaveRequest scheduleSaveRequest, Schedule schedule) {
return scheduleSaveRequest.getDailySchedules().stream()
.flatMap(dailySchedule -> {
AtomicInteger num = new AtomicInteger(1); // 날짜별 일정 순서 카운터
Expand Down Expand Up @@ -173,6 +176,28 @@ private String calculateDday(String startDateString) {



/**
* 최근에 생성한 여행 일정 top 5
**/
public LatestCreateResponse toLatestCreateResponse(Schedule schedule, int limit) {
List<String> limitedPlan = schedule.getScheduleDetails().stream()
.sorted(Comparator.comparing(ScheduleDetail::getDate)
.thenComparing(ScheduleDetail::getNum))
.map(ScheduleDetail::getTodo)
.limit(limit) // 최대 개수로 제한
.collect(Collectors.toList());

return new LatestCreateResponse(
schedule.getId(),
schedule.getScheduleName(),
schedule.getCountry().getCity(), // 국가에서 도시 가져오기로 수정
schedule.getCountry().getImageUrl(), // 이미지 URL 추가 (엔티티에 이 필드가 있다고 가정)
limitedPlan
);
}



}


Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.isp.backend.domain.member.entity.Member;
import com.isp.backend.domain.schedule.entity.Schedule;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -11,8 +12,8 @@

public interface ScheduleRepository extends JpaRepository<Schedule, Long> {
Optional<Schedule> findByIdAndActivatedIsTrue(Long scheduleId);

@Query("SELECT s FROM Schedule s WHERE s.member = :member AND s.activated = true ORDER BY s.updatedAt DESC")
List<Schedule> findSchedulesByMember(@Param("member") Member member);
List<Schedule> findTop5ByMemberOrderByIdDesc(Member member);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
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.LatestCreateResponse;
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 java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface ScheduleService {

Expand All @@ -23,11 +26,15 @@ public interface ScheduleService {

ScheduleSaveRequest updateSchedule(String uid, Long scheduleId, ScheduleSaveRequest updateRequestDTO);

Optional<Schedule> findClosestSchedule(List<Schedule> schedules, LocalDate today);

void calculateTotalPrice(Schedule schedule);

Member validateUserCheck(String uid);

Schedule validateSchedule(Long scheduleId);

Country validateCountry(String countryName);

List<LatestCreateResponse> getLatestCreatedSchedules(String uid, int limit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.isp.backend.domain.country.entity.Country;
import com.isp.backend.domain.country.repository.CountryRepository;
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.LatestCreateResponse;
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 @@ -23,7 +23,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -133,6 +133,7 @@ public void deleteSchedule(String uid, Long scheduleId) {
}

/** 나의 여행 D-day 출력 **/
@Override
public FastestScheduleResponse getFastestSchedule(String uid) {
Member findMember = validateUserCheck(uid);
List<Schedule> schedules = scheduleRepository.findSchedulesByMember(findMember);
Expand All @@ -151,7 +152,8 @@ public FastestScheduleResponse getFastestSchedule(String uid) {


/** 가장 가까운 일정 찾기 **/
private Optional<Schedule> findClosestSchedule(List<Schedule> schedules, LocalDate today) {
@Override
public 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()))); // 시작일이 가장 빠른 순으로 정렬
Expand Down Expand Up @@ -196,6 +198,18 @@ public Country validateCountry(String countryName) {
}


/** 최근에 생성한 여행 일정 top 5**/
@Override
public List<LatestCreateResponse> getLatestCreatedSchedules(String uid, int limit) {
Member findMember = validateUserCheck(uid);
List<Schedule> topSchedules = scheduleRepository.findTop5ByMemberOrderByIdDesc(findMember);

List<LatestCreateResponse> responses = new ArrayList<>();
for (Schedule schedule : topSchedules) {
LatestCreateResponse response = scheduleMapper.toLatestCreateResponse(schedule, limit);
responses.add(response);
}
return responses;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import jakarta.persistence.*;
import lombok.*;

@Getter
@AllArgsConstructor
@Entity
@Setter
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "schedule_detail")
public class ScheduleDetail {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
@AllArgsConstructor
public enum ScheduleType {

AIRPLANE, // 비행기

HOTEL, // 호텧

PLACE, // 관광지 장소
AIRPLANE,
HOTEL,
PLACE,

};

0 comments on commit e609e4a

Please sign in to comment.