Skip to content

Commit

Permalink
Merge pull request #62 from studio-recoding/fix-schedule-cd
Browse files Browse the repository at this point in the history
[🔧fix] 스케쥴 create 및 delete에서 하루 일정 반환하기
  • Loading branch information
JeonHaeseung authored May 8, 2024
2 parents 563f924 + 7d4d1b0 commit 5a8fb95
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public ResponseEntity<GetScheduleListDto> getUserSchedule(@AuthUser Member membe

@PostMapping("")
@Operation(summary = "새로운 스케쥴 생성", description = "새로운 스케쥴 내역 저장하는 API 입니다.")
public ResponseEntity<Long> postOneSchedule(@AuthUser Member member, @RequestBody PostScheduleDto postScheduleDto){
Long userId = scheduleService.postNewUserSchedule(member.getId(), postScheduleDto);
return new ResponseEntity<>(userId, HttpStatusCode.valueOf(201));
public ResponseEntity<GetScheduleListDto> postOneSchedule(@AuthUser Member member, @RequestBody PostScheduleDto postScheduleDto){
GetScheduleListDto oneUserOneDaySchedules = scheduleService.postNewUserSchedule(member.getId(), postScheduleDto);
return new ResponseEntity<>(oneUserOneDaySchedules, HttpStatusCode.valueOf(201));
}

@PutMapping("")
Expand All @@ -47,9 +47,9 @@ public ResponseEntity<GetScheduleListDto> putOneSchedule(@AuthUser Member member

@DeleteMapping("")
@Operation(summary = "하나의 스케쥴 삭제", description = "하나의 스케쥴을 삭제하는 API 입니다.")
public ResponseEntity<Long> deleteOneSchedule(@AuthUser Member member, @RequestParam Long id){
scheduleService.deleteSchedule(id);
return new ResponseEntity<>(HttpStatusCode.valueOf(200));
public ResponseEntity<GetScheduleListDto> deleteOneSchedule(@AuthUser Member member, @RequestParam Long id){
GetScheduleListDto oneUserOneDaySchedules = scheduleService.deleteSchedule(id);
return new ResponseEntity<>(oneUserOneDaySchedules, HttpStatusCode.valueOf(200));
}

@PostMapping("/ai")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.ZonedDateTime;
import java.util.List;

@Repository
Expand All @@ -23,28 +24,29 @@ List<Schedule> findOneMonthSchedulesByMember_Id(
@Param("year") int year,
@Param("month") int month);

// 특정 맴버의 한달 치 스케쥴 반환
// 특정 맴버의 하루 치 스케쥴 반환(String 사용)
@Query( value = "SELECT * FROM schedule " +
"WHERE member_id = :memberId " +
"AND YEAR(start_time) = :year " +
"AND MONTH(start_time) = :month " +
"AND DAY(start_time) = :day " +
"ORDER BY start_time ASC",
nativeQuery = true)
List<Schedule> findOneDaySchedulesByMember_Id(
List<Schedule> findOneDaySchedulesByMember_IdWithString(
@Param("memberId") Long memberId,
@Param("year") int year,
@Param("month") int month,
@Param("day") int day);

//TODO: 현재는 가장 첫번째 행 데이터를 가져옴->기준을 만들 필요 있음
// 특정 맴버의 하루 치 스케쥴 반환(ZoneDateTime 사용)
@Query( value = "SELECT * FROM schedule " +
"WHERE member_id = :memberId " +
"AND DATE(start_time) = CURDATE() " +
"ORDER BY start_time ASC LIMIT 1",
"AND DATE(start_time) = DATE(:date) " +
"ORDER BY start_time ASC",
nativeQuery = true)
Schedule findTodayOneScheduleByMember_Id(
@Param("memberId") Long memberId);
List<Schedule> findOneDaySchedulesByMember_Id(
@Param("memberId") Long memberId,
@Param("date") ZonedDateTime date);

//스케쥴 ID로 특정 스케쥴 찾아주기
Schedule findScheduleById(Long scheduleId);
Expand Down
67 changes: 38 additions & 29 deletions src/main/java/Ness/Backend/domain/schedule/ScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ScheduleService {
private final ChatService chatService;
private final FastApiScheduleApi fastApiScheduleApi;

// 한 달치 스케쥴 가져오는 로직
@Transactional(readOnly = true)
public GetScheduleListDto getOneMonthUserSchedule(Long memberId, String date){
log.info("getOneMonthUserSchedule called by "+ memberId);
Expand All @@ -53,8 +54,15 @@ public GetScheduleListDto getOneMonthUserSchedule(Long memberId, String date){
.findOneMonthSchedulesByMember_Id(memberId, year, month));
}

// 하루치 스케쥴 가져오는 로직(Create, Delete에서 사용)
@Transactional(readOnly = true)
public GetScheduleListDto getOneDayUserSchedule(Long memberId, ZonedDateTime date){
return makeScheduleListDto(
scheduleRepository
.findOneDaySchedulesByMember_Id(memberId, date));
}

/* 사용자가 직접 변경한 스케쥴 RDB에 저장하는 로직 */
@Transactional
public GetScheduleListDto changeSchedule(Long memberId, PutScheduleDto putScheduleDto, String date){
// 년도, 월, 일 추출
String[] parts = date.split("-");
Expand All @@ -74,36 +82,18 @@ public GetScheduleListDto changeSchedule(Long memberId, PutScheduleDto putSchedu

return makeScheduleListDto(
scheduleRepository
.findOneDaySchedulesByMember_Id(memberId, year, month, day));
}

public GetScheduleListDto makeScheduleListDto(List<Schedule> scheduleList){
// ScheduleListResponseDto에 매핑
List<GetScheduleDto> getScheduleDtos = scheduleList.stream()
.map(schedule -> GetScheduleDto.builder()
.id(schedule.getId())
.category(schedule.getCategory().getName())
.categoryNum(schedule.getCategory().getId())
.info(schedule.getInfo())
.startTime(schedule.getStartTime())
.endTime(schedule.getEndTime())
.details(GetScheduleDetailDto.builder()
.person(schedule.getPerson())
.location(schedule.getLocation())
.build())
.build())
.toList();
return new GetScheduleListDto(getScheduleDtos);
.findOneDaySchedulesByMember_IdWithString(memberId, year, month, day));
}

@Transactional
public void deleteSchedule(Long id){
Schedule schedule = scheduleRepository.findScheduleById(id);
/* 사용자가 직접 삭제한 스케쥴 */
public GetScheduleListDto deleteSchedule(Long memberId){
Schedule schedule = scheduleRepository.findScheduleById(memberId);
scheduleRepository.delete(schedule);

return getOneDayUserSchedule(memberId, schedule.getStartTime());
}

/* 사용자가 AI가 생성한 스케쥴을 Accept/Deny한 여부에 따라서 채팅 및 스케쥴 저장 */
@Transactional
public GetChatListDto postAiScheduleAccept(Long memberId, Boolean idAccepted, Long chatId, PostScheduleDto postScheduleDto){
Member member = memberRepository.findMemberById(memberId);
Category category = categoryRepository.findCategoryById(postScheduleDto.getCategoryNum());
Expand Down Expand Up @@ -137,9 +127,8 @@ public GetChatListDto postAiScheduleAccept(Long memberId, Boolean idAccepted, Lo


/* 사용자가 직접 생성한 스케쥴을 RDB & VectorDB에 저장 */
public Long postNewUserSchedule(Long id, PostScheduleDto postScheduleDto){
log.info("postNewUserSchedule called by "+ id);
Member memberEntity = memberRepository.findMemberById(id);
public GetScheduleListDto postNewUserSchedule(Long memberId, PostScheduleDto postScheduleDto){
Member memberEntity = memberRepository.findMemberById(memberId);
Category category = categoryRepository.findCategoryById(postScheduleDto.getCategoryNum());

//새로운 채팅 생성
Expand All @@ -166,7 +155,7 @@ public Long postNewUserSchedule(Long id, PostScheduleDto postScheduleDto){
newSchedule.getMember().getId(),
newSchedule.getId());

return newSchedule.getId(); // 저장한 Chat 확인용
return getOneDayUserSchedule(memberId, newSchedule.getStartTime());
}

/* 새로운 스케쥴을 VectorDB에 저장하는 API 호출 */
Expand Down Expand Up @@ -196,4 +185,24 @@ public void postNewAiSchedule(String info, String location, String person,
log.error("Failed to save data in Vector DB");
}
}

/* 리스트 DTO 만들어서 반환하는 로직 */
public GetScheduleListDto makeScheduleListDto(List<Schedule> scheduleList){
// ScheduleListResponseDto에 매핑
List<GetScheduleDto> getScheduleDtos = scheduleList.stream()
.map(schedule -> GetScheduleDto.builder()
.id(schedule.getId())
.category(schedule.getCategory().getName())
.categoryNum(schedule.getCategory().getId())
.info(schedule.getInfo())
.startTime(schedule.getStartTime())
.endTime(schedule.getEndTime())
.details(GetScheduleDetailDto.builder()
.person(schedule.getPerson())
.location(schedule.getLocation())
.build())
.build())
.toList();
return new GetScheduleListDto(getScheduleDtos);
}
}

0 comments on commit 5a8fb95

Please sign in to comment.