Skip to content

Commit

Permalink
Merge pull request #56 from studio-recoding/fix-schedule-crud
Browse files Browse the repository at this point in the history
[🔧fix]  스케쥴 변경의 결과를 스케쥴 리스트로 수정
  • Loading branch information
JeonHaeseung authored May 3, 2024
2 parents 4a017de + 2dbe885 commit 2dc4bc4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 33 deletions.
24 changes: 14 additions & 10 deletions src/main/java/Ness/Backend/domain/schedule/ScheduleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Ness.Backend.domain.member.entity.Member;
import Ness.Backend.domain.schedule.dto.request.PostScheduleDto;
import Ness.Backend.domain.schedule.dto.request.PutScheduleDto;
import Ness.Backend.domain.schedule.dto.response.GetOneMonthSchedulesDto;
import Ness.Backend.domain.schedule.dto.response.GetScheduleListDto;
import Ness.Backend.global.auth.AuthUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -22,9 +22,9 @@ public class ScheduleController {

@GetMapping("")
@Operation(summary = "특정 사용자의 한달치 스케쥴 내역", description = "&month=2024-01 와 같은 형식으로 데이터가 전달됩니다.")
public ResponseEntity<GetOneMonthSchedulesDto> getUserSchedule(@AuthUser Member member, @RequestParam String month){
GetOneMonthSchedulesDto oneUserMonthSchedules = scheduleService.getOneMonthUserSchedule(member.getId(), month);
return new ResponseEntity<>(oneUserMonthSchedules, HttpStatusCode.valueOf(200));
public ResponseEntity<GetScheduleListDto> getUserSchedule(@AuthUser Member member, @RequestParam String month){
GetScheduleListDto oneUserOneMonthSchedules = scheduleService.getOneMonthUserSchedule(member.getId(), month);
return new ResponseEntity<>(oneUserOneMonthSchedules, HttpStatusCode.valueOf(200));
}

@PostMapping("")
Expand All @@ -35,14 +35,18 @@ public ResponseEntity<Long> postOneSchedule(@AuthUser Member member, @RequestBod
}

@PutMapping("")
@Operation(summary = "하나의 스케쥴 변경", description = "하나의 스케쥴의 정보를 변경하는 API로 스케쥴, 위치, 사람, 시간 모두 PUT으로 처리합니다.")
public ResponseEntity<Long> putOneSchedule(@AuthUser Member member, @RequestBody PutScheduleDto putScheduleDto){
Long userId = scheduleService.changeSchedule(member.getId(), putScheduleDto);
return new ResponseEntity<>(userId, HttpStatusCode.valueOf(200));
@Operation(
summary = "하나의 스케쥴 변경",
description =
"하나의 스케쥴의 정보를 변경하는 API로 스케쥴, 위치, 사람, 시간 모두 PUT으로 처리합니다." +
"&day=2024-01-01과 같은 형식으로 데이터가 전달됩니다.")
public ResponseEntity<GetScheduleListDto> putOneSchedule(@AuthUser Member member, @RequestParam String day, @RequestBody PutScheduleDto putScheduleDto){
GetScheduleListDto oneUserOneDaySchedules = scheduleService.changeSchedule(member.getId(), putScheduleDto, day);
return new ResponseEntity<>(oneUserOneDaySchedules, HttpStatusCode.valueOf(200));
}

@DeleteMapping("")
@Operation(summary = "하나의 스케쥴 삭제", description = "하나의 스케쥴의 정보를 변경하는 API로 스케쥴, 위치, 사람, 시간 모두 PUT으로 처리합니다.")
@Operation(summary = "하나의 스케쥴 삭제", description = "하나의 스케쥴을 삭제하는 API 입니다.")
public ResponseEntity<Long> deleteOneSchedule(@AuthUser Member member, @RequestParam Long id){
scheduleService.deleteSchedule(id);
return new ResponseEntity<>(HttpStatusCode.valueOf(200));
Expand All @@ -51,7 +55,7 @@ public ResponseEntity<Long> deleteOneSchedule(@AuthUser Member member, @RequestP
@PostMapping("/ai")
@Operation(summary = "AI에 의해서 생성된 새로운 스케쥴", description = "프론트가 보내주는 AI에 의해서 생성된 새로운 스케쥴을 Accept/Deny합니다.")
public ResponseEntity<GetChatListDto> PostAiSchedule(@AuthUser Member member, @RequestParam Boolean isAccepted, @RequestParam Long chatId, @RequestBody PostScheduleDto postScheduleDto){
GetChatListDto oneUserChats = scheduleService.postNewAiSchedule(member.getId(), isAccepted, chatId, postScheduleDto);
GetChatListDto oneUserChats = scheduleService.postAiScheduleAccept(member.getId(), isAccepted, chatId, postScheduleDto);
return new ResponseEntity<>(oneUserChats, HttpStatusCode.valueOf(200));
}
}
14 changes: 14 additions & 0 deletions src/main/java/Ness/Backend/domain/schedule/ScheduleRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ List<Schedule> findOneMonthSchedulesByMember_Id(
@Param("year") int year,
@Param("month") int month);

// 특정 맴버의 한달 치 스케쥴 반환
@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(
@Param("memberId") Long memberId,
@Param("year") int year,
@Param("month") int month,
@Param("day") int day);

//TODO: 현재는 가장 첫번째 행 데이터를 가져옴->기준을 만들 필요 있음
@Query( value = "SELECT * FROM schedule " +
"WHERE member_id = :memberId " +
Expand Down
61 changes: 39 additions & 22 deletions src/main/java/Ness/Backend/domain/schedule/ScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import Ness.Backend.domain.schedule.dto.request.PostFastApiScheduleDto;
import Ness.Backend.domain.schedule.dto.request.PostScheduleDto;
import Ness.Backend.domain.schedule.dto.request.PutScheduleDto;
import Ness.Backend.domain.schedule.dto.response.GetOneMonthSchedulesDto;
import Ness.Backend.domain.schedule.dto.response.GetScheduleListDto;
import Ness.Backend.domain.schedule.dto.response.GetScheduleDetailDto;
import Ness.Backend.domain.schedule.dto.response.GetScheduleDto;
import Ness.Backend.domain.schedule.entity.Schedule;
Expand Down Expand Up @@ -41,15 +41,43 @@ public class ScheduleService {
private final FastApiScheduleApi fastApiScheduleApi;

@Transactional(readOnly = true)
public GetOneMonthSchedulesDto getOneMonthUserSchedule(Long id, String date){
log.info("getOneMonthUserSchedule called by "+ id);
public GetScheduleListDto getOneMonthUserSchedule(Long memberId, String date){
log.info("getOneMonthUserSchedule called by "+ memberId);
// 년도, 월, 일 추출
String[] parts = date.split("-");
int year = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);

List<Schedule> scheduleList = scheduleRepository.findOneMonthSchedulesByMember_Id(id, year, month);

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

/* 사용자가 직접 변경한 스케쥴 */
@Transactional
public GetScheduleListDto changeSchedule(Long memberId, PutScheduleDto putScheduleDto, String date){
// 년도, 월, 일 추출
String[] parts = date.split("-");
int year = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int day = Integer.parseInt(parts[2]);

Schedule schedule = scheduleRepository.findScheduleById(putScheduleDto.getId());
Category category = categoryRepository.findCategoryById(putScheduleDto.getCategoryNum());
schedule.changeSchedule(
putScheduleDto.getInfo(),
putScheduleDto.getLocation(),
putScheduleDto.getPerson(),
putScheduleDto.getStartTime(),
putScheduleDto.getEndTime(),
category);

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()
Expand All @@ -65,21 +93,7 @@ public GetOneMonthSchedulesDto getOneMonthUserSchedule(Long id, String date){
.build())
.build())
.toList();
return new GetOneMonthSchedulesDto(getScheduleDtos);
}

@Transactional
public Long changeSchedule(Long id, PutScheduleDto putScheduleDto){
Schedule schedule = scheduleRepository.findScheduleById(putScheduleDto.getId());
Category category = categoryRepository.findCategoryById(putScheduleDto.getCategoryNum());
schedule.changeSchedule(
putScheduleDto.getInfo(),
putScheduleDto.getLocation(),
putScheduleDto.getPerson(),
putScheduleDto.getStartTime(),
putScheduleDto.getEndTime(),
category);
return schedule.getId();
return new GetScheduleListDto(getScheduleDtos);
}

@Transactional
Expand All @@ -88,11 +102,12 @@ public void deleteSchedule(Long id){
scheduleRepository.delete(schedule);
}

/* 사용자가 AI가 생성한 스케쥴을 Accept/Deny한 여부에 따라서 채팅 및 스케쥴 저장 */
@Transactional
public GetChatListDto postNewAiSchedule(Long memberId, Boolean idAccepted, Long chatId, PostScheduleDto postScheduleDto){
public GetChatListDto postAiScheduleAccept(Long memberId, Boolean idAccepted, Long chatId, PostScheduleDto postScheduleDto){
Member member = memberRepository.findMemberById(memberId);

if(idAccepted = Boolean.TRUE){
if(idAccepted){
/* 사용자가 Accept 했으면 스케쥴 생성하기 */
Chat chat = chatRepository.findChatById(chatId);

Expand All @@ -106,6 +121,7 @@ public GetChatListDto postNewAiSchedule(Long memberId, Boolean idAccepted, Long
//.category() //이 연관관계들은 나중에 넣어야 함
.chat(chat)
.build();

scheduleRepository.save(newSchedule);

chatService.createNewChat(memberId, "일정을 추가해드렸습니다:)", ChatType.AI, 2, member);
Expand All @@ -119,6 +135,7 @@ public GetChatListDto postNewAiSchedule(Long memberId, Boolean idAccepted, Long
}


/* 사용자가 직접 생성한 스케쥴을 RDB & VectorDB에 저장 */
public Long postNewUserSchedule(Long id, PostScheduleDto postScheduleDto){
log.info("postNewUserSchedule called by "+ id);
Member memberEntity = memberRepository.findMemberById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class GetOneMonthSchedulesDto {
public class GetScheduleListDto {
private List<GetScheduleDto> scheduleList;
}

0 comments on commit 2dc4bc4

Please sign in to comment.