Skip to content

Commit

Permalink
[CHORE] schedule api
Browse files Browse the repository at this point in the history
  • Loading branch information
hisemsem committed Aug 30, 2023
1 parent 91c8f78 commit 2f3f8a0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum ResponseStatus {
CONFLICT(409,HttpStatus.Series.CLIENT_ERROR, "Conflict"),
PAYLOAD_TOO_LARGE(413,HttpStatus.Series.CLIENT_ERROR, "Payload Too Large"),
URI_TOO_LONG(414,HttpStatus.Series.CLIENT_ERROR, "URI Too Long"),
INVALID_DATA_FORMAT(415, HttpStatus.Series.CLIENT_ERROR, "Invalid Data Format"),
MISSING_REQUIRED_FIELD(416, HttpStatus.Series.CLIENT_ERROR, "Required field(s) are missing"),

// for mypage status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ public ResponseEntity<BaseResponse> deleteSchedule(@PathVariable Long scheduleId
@PatchMapping("/schedule/{scheduleId}")
public ResponseEntity<BaseResponse> updateSchedule(@PathVariable Long scheduleId, @RequestBody ScheduleRequestDto requestDto) {
try {
Schedule updatedSchedule = scheduleService.updateSchedule(scheduleId, requestDto);

Schedule updateSchedule = scheduleService.updateSchedule(scheduleId, requestDto);
log.debug("requestDto.getPlace() = {}",requestDto.getPlace());

// 맵 API 호출하여 맵 URL 얻어오는 부분
String mapUrl = mapService.getMapUrl(requestDto.getPlace());
System.out.println(mapUrl);

// 응답에 맵 URL을 포함하여 리턴
updatedSchedule.setMapUrl(mapUrl);
updateSchedule.setMapUrl(mapUrl);
System.out.println(mapUrl);

ScheduleResponseDto responseDto = new ScheduleResponseDto(updatedSchedule);
ScheduleResponseDto responseDto = new ScheduleResponseDto(updateSchedule);
BaseResponse<ScheduleResponseDto> response = new BaseResponse<>(responseDto);
return response.convert();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class ScheduleResponseDto {


public ScheduleResponseDto(Schedule schedule){
this.mapUrl = schedule.getMapUrl();

this.id = schedule.getId();

this.localDate = schedule.getLocalDate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public ScheduleResponseDto createSchedule(ScheduleRequestDto scheduleRequestDto)
schedule.setRepeatType(repeatType);
schedule.setMedicine(medicine);

//log.debug("Schedule fields set....");

// missing field 존재 여부 검사
if (localDate == null || dayOfWeek == null || time == null || content == null || done == null || repeatType == null) {
throw new MissingRequiredFieldException("Required field(s) are missing");
Expand All @@ -84,10 +82,8 @@ public ScheduleResponseDto createSchedule(ScheduleRequestDto scheduleRequestDto)
Schedule createdSchedule = scheduleRepository.save(schedule);
return new ScheduleResponseDto(createdSchedule);
} catch (MissingRequiredFieldException e) {
//log.error("Missing required fields: ", e);
throw new BaseException(ResponseStatus.MISSING_REQUIRED_FIELD);
} catch (BaseException e) {
//log.error("Same field is already exists.: ", e);
throw new BaseException(ResponseStatus.CONFLICT);
}

Expand All @@ -97,15 +93,12 @@ public ScheduleResponseDto createSchedule(ScheduleRequestDto scheduleRequestDto)
//주어진 날짜에 해당하는 일정 조회
public List<Schedule> getSchedule(LocalDate localDate) throws BaseException {
try {
//log.debug("1");
List<Schedule> schedules = scheduleRepository.findByLocalDate(localDate);
//log.debug("2");
if (schedules.isEmpty()) {
throw new BaseException(ResponseStatus.NOT_FOUND);
}
return schedules;
} catch (BaseException e) {
//log.error("Error occurred while fetching schedules: " + e.getMessage());
throw new BaseException(ResponseStatus.NOT_FOUND); // 현재의 예외를 다시 던져줍니다.
}

Expand All @@ -125,14 +118,14 @@ public List<Schedule> getTodaySchedules() throws BaseException {
return todaySchedules;

} catch (BaseException e) {
//log.error("Error occurred while fetching schedules: " + e.getMessage());

throw new BaseException(ResponseStatus.NOT_FOUND); // 현재의 예외를 다시 던져줍니다.
}
}

//일정 삭제
@Transactional
public Long deleteSchedule(Long scheduleId) {
public Long deleteSchedule(Long scheduleId) throws BaseException {
try {
Optional<Schedule> optionalSchedule = scheduleRepository.findById(scheduleId);

Expand All @@ -142,7 +135,7 @@ public Long deleteSchedule(Long scheduleId) {
scheduleRepository.deleteById(scheduleId);
return scheduleId;
} catch (BaseException e) {
//log.error("ID does not exist.");

throw new BaseException(ResponseStatus.NOT_FOUND);
}
}
Expand All @@ -154,10 +147,6 @@ public Schedule updateSchedule(Long id, ScheduleRequestDto requestDto) {
try {
Schedule schedule = scheduleRepository.findById(id).orElse(null);

if (schedule == null) {
throw new BaseException(ResponseStatus.NOT_FOUND); // 해당 아이디가 존재하지 않는 경우
}
// Validate the updated schedule's fields
LocalDate newLocalDate = requestDto.getLocalDate();
DayOfWeek newDayOfWeek = requestDto.getDayOfWeek();
String newContent = requestDto.getContent();
Expand All @@ -170,7 +159,21 @@ public Schedule updateSchedule(Long id, ScheduleRequestDto requestDto) {
if (newLocalDate == null || newDayOfWeek == null || newTime == null || newContent == null || newDone == null || newRepeatType == null) {
throw new MissingRequiredFieldException("Required field(s) are missing in updated schedule");
}

// 여기서 중복 일정 검사를 수행하고 이미 존재하는 경우 예외를 던짐
List<Schedule> existingSchedules = scheduleRepository.findByLocalDate(newLocalDate);
for (Schedule existingSchedule : existingSchedules) {
System.out.println("existingSchedule: " + existingSchedule);
if (
existingSchedule.getDayOfWeek() == newDayOfWeek &&
existingSchedule.getTime().equals(newTime) &&
existingSchedule.getContent().equals(newContent) &&
existingSchedule.getDone().equals(newDone) &&
existingSchedule.getRepeatType().equals(newRepeatType) &&
existingSchedule.getPlace().equals(newPlace) &&
existingSchedule.getMedicine().equals(newMedicine)) {
throw new BaseException(ResponseStatus.CONFLICT);
}
}
schedule.setLocalDate(newLocalDate);
schedule.setDayOfWeek(newDayOfWeek);
schedule.setContent(newContent);
Expand All @@ -180,14 +183,14 @@ public Schedule updateSchedule(Long id, ScheduleRequestDto requestDto) {
schedule.setRepeatType(newRepeatType);
schedule.setMedicine(newMedicine);

log.debug("newContent = {}", newContent);

return scheduleRepository.save(schedule);

} catch (MissingRequiredFieldException e) {
//log.error("Missing required fields: ", e);
throw new BaseException(ResponseStatus.MISSING_REQUIRED_FIELD);
} catch (BaseException e) {

throw new BaseException(ResponseStatus.NOT_FOUND);
throw new BaseException(ResponseStatus.CONFLICT);
}
}
}

0 comments on commit 2f3f8a0

Please sign in to comment.