From fc60959f588660ec8281b1aea724ccdc1624e274 Mon Sep 17 00:00:00 2001 From: SemiLee Date: Thu, 12 Oct 2023 19:34:27 +0900 Subject: [PATCH] [REFACTOR] code refactoring - schedule api --- .../controller/ScheduleController.java | 47 +++++------ .../schedule/dto/ScheduleRequestDto.java | 6 -- .../schedule/dto/ScheduleResponseDto.java | 24 ++---- .../backend/schedule/entity/Schedule.java | 21 +---- .../repository/ScheduleRepository.java | 10 +-- .../schedule/service/ScheduleService.java | 77 +++++++------------ .../backend/HaemilApplicationTests.java | 13 ---- .../repository/ScheduleRepositoryTest.java | 4 - .../ScheduleServiceIntegrationTest.java | 4 - .../schedule/service/ScheduleServiceTest.java | 60 --------------- 10 files changed, 56 insertions(+), 210 deletions(-) delete mode 100644 src/test/java/com/haemil/backend/HaemilApplicationTests.java delete mode 100644 src/test/java/com/haemil/backend/schedule/repository/ScheduleRepositoryTest.java delete mode 100644 src/test/java/com/haemil/backend/schedule/service/ScheduleServiceIntegrationTest.java delete mode 100644 src/test/java/com/haemil/backend/schedule/service/ScheduleServiceTest.java diff --git a/src/main/java/com/haemil/backend/schedule/controller/ScheduleController.java b/src/main/java/com/haemil/backend/schedule/controller/ScheduleController.java index 2e22144..f2c431d 100644 --- a/src/main/java/com/haemil/backend/schedule/controller/ScheduleController.java +++ b/src/main/java/com/haemil/backend/schedule/controller/ScheduleController.java @@ -7,6 +7,7 @@ import com.haemil.backend.schedule.dto.ScheduleResponseDto; import com.haemil.backend.schedule.entity.Schedule; import com.haemil.backend.schedule.service.ScheduleService; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.annotation.DateTimeFormat; @@ -19,100 +20,92 @@ @RestController @RequestMapping("/schedules") +@AllArgsConstructor @Slf4j public class ScheduleController { public final ScheduleService scheduleService; public final MapService mapService; - @Autowired - public ScheduleController(ScheduleService scheduleService, MapService mapService) { - this.scheduleService = scheduleService; - this.mapService = mapService; - } - //일정 추가 API @PostMapping("/schedule") - public ResponseEntity createSchedule(@RequestBody ScheduleRequestDto scheduleRequestDto) { - + public ResponseEntity createSchedule( + @RequestBody ScheduleRequestDto scheduleRequestDto) { try { + //schedule 생성 및 map URL 설정 ScheduleResponseDto createSchedule = scheduleService.createSchedule(scheduleRequestDto); - - // 맵 API 호출하여 맵 URL 얻어오는 부분 String mapUrlString = mapService.getMapUrl(scheduleRequestDto.getPlace()); - - // 응답에 맵 URL을 포함하여 리턴 createSchedule.setMapUrl(mapUrlString); - BaseResponse response = new BaseResponse<>(createSchedule); return response.convert(); } catch (BaseException e) { + //에러 처리 return new BaseResponse<>(e.getStatus()).convert(); } catch (com.haemil.backend.global.config.BaseException e) { + //에러 처리 throw new RuntimeException(e); } - } //주어진 날짜에 해당하는 일정 조회 API @GetMapping("/getSchedule") - public ResponseEntity getSchedulesByDate(@RequestParam("localDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate localDate) { + public ResponseEntity getSchedulesByDate( + @RequestParam("localDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate localDate) { try { + //원하는 schedule 조회 List schedules = scheduleService.getSchedule(localDate); return new BaseResponse<>(schedules).convert(); - } catch (BaseException e) { + //에러 처리 return new BaseResponse<>(e.getStatus()).convert(); } } - //오늘 일정 조회 API @GetMapping("/today") public ResponseEntity getTodaySchedules() { try { + //오늘 schedule 조회 List todaySchedules = scheduleService.getTodaySchedules(); return new BaseResponse<>(todaySchedules).convert(); - } catch (BaseException e) { + //에러 처리 return new BaseResponse<>(e.getStatus()).convert(); } } - //일정 삭제 API @DeleteMapping("/schedule/{scheduleId}") public ResponseEntity deleteSchedule(@PathVariable Long scheduleId) { try { + //schedule 삭제 Long deletedId = scheduleService.deleteSchedule(scheduleId); BaseResponse response = new BaseResponse<>(deletedId); return response.convert(); } catch (BaseException e) { + //에러 처리 return new BaseResponse<>(e.getStatus()).convert(); } } - //일정 수정 API @PatchMapping("/schedule/{scheduleId}") - public ResponseEntity updateSchedule(@PathVariable Long scheduleId, @RequestBody ScheduleRequestDto requestDto) { + public ResponseEntity updateSchedule(@PathVariable Long scheduleId, + @RequestBody ScheduleRequestDto requestDto) { try { + //schedule 수정 및 map URL 수정 Schedule updateSchedule = scheduleService.updateSchedule(scheduleId, requestDto); - - // 맵 API 호출하여 맵 URL 얻어오는 부분 String mapUrlString = mapService.getMapUrl(requestDto.getPlace()); - - // 응답에 맵 URL을 포함하여 리턴 updateSchedule.setMapUrl(mapUrlString); ScheduleResponseDto responseDto = new ScheduleResponseDto(updateSchedule); BaseResponse response = new BaseResponse<>(responseDto); return response.convert(); - } catch (BaseException e) { + //에러 처리 return new BaseResponse<>(e.getStatus()).convert(); - } catch (com.haemil.backend.global.config.BaseException e) { + //에러 처리 throw new RuntimeException(e); } } diff --git a/src/main/java/com/haemil/backend/schedule/dto/ScheduleRequestDto.java b/src/main/java/com/haemil/backend/schedule/dto/ScheduleRequestDto.java index 92d239b..b473b4d 100644 --- a/src/main/java/com/haemil/backend/schedule/dto/ScheduleRequestDto.java +++ b/src/main/java/com/haemil/backend/schedule/dto/ScheduleRequestDto.java @@ -4,11 +4,9 @@ import lombok.Getter; import lombok.NoArgsConstructor; -import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; -//schedule에 데이터를 넣을 때의 입력 요청 값을 받음 @NoArgsConstructor @Getter public class ScheduleRequestDto { @@ -17,8 +15,6 @@ public class ScheduleRequestDto { private LocalDate localDate; - private DayOfWeek dayOfWeek; - private LocalTime time; private String content; @@ -27,8 +23,6 @@ public class ScheduleRequestDto { private String place; - private String medicine; - private RepeatType repeatType; diff --git a/src/main/java/com/haemil/backend/schedule/dto/ScheduleResponseDto.java b/src/main/java/com/haemil/backend/schedule/dto/ScheduleResponseDto.java index 5aabe09..73103b7 100644 --- a/src/main/java/com/haemil/backend/schedule/dto/ScheduleResponseDto.java +++ b/src/main/java/com/haemil/backend/schedule/dto/ScheduleResponseDto.java @@ -6,58 +6,48 @@ import lombok.NoArgsConstructor; import lombok.Setter; -import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; -//schedule에서 값을 가져올 때 직접적인 entity 대신 앞에 써줌 -//클라이언트에게 응답할 때 필요한 속성들 추가 @NoArgsConstructor @Getter @Setter public class ScheduleResponseDto { - private String mapUrl; - private Long id; private LocalDate localDate; - private DayOfWeek dayOfWeek; - private LocalTime time; private String content; - private String place; - private Boolean done; - private String medicine; + private String place; private RepeatType repeatType; + private String mapUrl; - public ScheduleResponseDto(Schedule schedule){ - this.mapUrl = schedule.getMapUrl(); + + public ScheduleResponseDto(Schedule schedule) { this.id = schedule.getId(); this.localDate = schedule.getLocalDate(); - this.dayOfWeek = schedule.getDayOfWeek(); - this.time = schedule.getTime(); this.content = schedule.getContent(); - this.place = schedule.getPlace(); - this.done = schedule.getDone(); - this.medicine = schedule.getMedicine(); + this.place = schedule.getPlace(); this.repeatType = schedule.getRepeatType(); + this.mapUrl = schedule.getMapUrl(); + } } diff --git a/src/main/java/com/haemil/backend/schedule/entity/Schedule.java b/src/main/java/com/haemil/backend/schedule/entity/Schedule.java index d691f2c..744623a 100644 --- a/src/main/java/com/haemil/backend/schedule/entity/Schedule.java +++ b/src/main/java/com/haemil/backend/schedule/entity/Schedule.java @@ -3,10 +3,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -import net.bytebuddy.asm.Advice; - import javax.persistence.*; -import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; @@ -24,10 +21,6 @@ public class Schedule { @Column(nullable = false) private LocalDate localDate; - //요일 - @Column(nullable = false) - private DayOfWeek dayOfWeek; - //일정 시간(hour, minute, second, nano) @Column(nullable = false) private LocalTime time; @@ -44,23 +37,13 @@ public class Schedule { @Column(nullable = true, length = 50) private String place; - //약 - @Column(nullable = true, length = 50) - private String medicine; - //반복 routine @Enumerated(value = EnumType.STRING) @Column(nullable = false) private RepeatType repeatType; + //장소 url + @Column(nullable = true) private String mapUrl; - public String getMapUrl() { - return mapUrl; - } - - public void setMapUrl(String mapUrl) { - this.mapUrl = mapUrl; - } - } \ No newline at end of file diff --git a/src/main/java/com/haemil/backend/schedule/repository/ScheduleRepository.java b/src/main/java/com/haemil/backend/schedule/repository/ScheduleRepository.java index 04bcce2..0e09207 100644 --- a/src/main/java/com/haemil/backend/schedule/repository/ScheduleRepository.java +++ b/src/main/java/com/haemil/backend/schedule/repository/ScheduleRepository.java @@ -2,21 +2,13 @@ import com.haemil.backend.schedule.entity.Schedule; import org.springframework.context.annotation.Primary; -import org.springframework.data.domain.Example; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -import javax.persistence.Entity; -import javax.swing.text.html.Option; -import java.time.DayOfWeek; import java.time.LocalDate; -import java.time.LocalTime; import java.util.List; -import java.util.Optional; @Primary -//JpaRepository를 상속받아서 데이터베이스와 상호작용하는 메서드들 제공 -public interface ScheduleRepository extends JpaRepository { +public interface ScheduleRepository extends JpaRepository { //일정을 조회 List findByLocalDate(LocalDate localDate); diff --git a/src/main/java/com/haemil/backend/schedule/service/ScheduleService.java b/src/main/java/com/haemil/backend/schedule/service/ScheduleService.java index 8960401..d796db7 100644 --- a/src/main/java/com/haemil/backend/schedule/service/ScheduleService.java +++ b/src/main/java/com/haemil/backend/schedule/service/ScheduleService.java @@ -7,67 +7,57 @@ import com.haemil.backend.schedule.entity.RepeatType; import com.haemil.backend.schedule.entity.Schedule; import com.haemil.backend.schedule.repository.ScheduleRepository; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.transaction.annotation.Transactional; import com.haemil.backend.global.config.ResponseStatus; -import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; import java.util.List; import java.util.Optional; - -//Schedule 객체를 저장하고 조회하는 기능을 구현 -//ScheduleRepository를 사용하여 데이터베이스와 상호작용 @Slf4j @Transactional +@AllArgsConstructor public class ScheduleService { - private final ScheduleRepository scheduleRepository; - public ScheduleService(ScheduleRepository scheduleRepository) { - this.scheduleRepository = scheduleRepository; - } + private final ScheduleRepository scheduleRepository; //일정 생성 - public ScheduleResponseDto createSchedule(ScheduleRequestDto scheduleRequestDto) throws BaseException { + public ScheduleResponseDto createSchedule(ScheduleRequestDto scheduleRequestDto) + throws BaseException { try { LocalDate localDate = scheduleRequestDto.getLocalDate(); - DayOfWeek dayOfWeek = scheduleRequestDto.getDayOfWeek(); String content = scheduleRequestDto.getContent(); Boolean done = scheduleRequestDto.getDone(); LocalTime time = scheduleRequestDto.getTime(); String place = scheduleRequestDto.getPlace(); - String medicine = scheduleRequestDto.getMedicine(); RepeatType repeatType = scheduleRequestDto.getRepeatType(); Schedule schedule = new Schedule(); schedule.setLocalDate(localDate); - schedule.setDayOfWeek(dayOfWeek); schedule.setContent(content); schedule.setDone(done); schedule.setTime(time); schedule.setPlace(place); schedule.setRepeatType(repeatType); - schedule.setMedicine(medicine); - // missing field 존재 여부 검사 - if (localDate == null || dayOfWeek == null || time == null || content == null || done == null || repeatType == null) { + if (localDate == null || time == null || content == null || done == null + || repeatType == null) { throw new MissingRequiredFieldException("Required field(s) are missing"); } - // 여기서 중복 일정 검사를 수행하고 이미 존재하는 경우 예외를 던짐 List existingSchedules = scheduleRepository.findByLocalDate(localDate); for (Schedule existingSchedule : existingSchedules) { - if (existingSchedule.getDayOfWeek() == dayOfWeek && - existingSchedule.getTime().equals(time) && - existingSchedule.getContent().equals(content) && - existingSchedule.getDone().equals(done) && - existingSchedule.getRepeatType().equals(repeatType) && - existingSchedule.getPlace().equals(place) && - existingSchedule.getMedicine().equals(medicine)) { + if (existingSchedule.getTime().equals(time) && + existingSchedule.getContent().equals(content) && + existingSchedule.getDone().equals(done) && + existingSchedule.getRepeatType().equals(repeatType) && + existingSchedule.getPlace().equals(place) + ) { throw new BaseException(ResponseStatus.CONFLICT); } } @@ -79,8 +69,6 @@ public ScheduleResponseDto createSchedule(ScheduleRequestDto scheduleRequestDto) } catch (BaseException e) { throw new BaseException(ResponseStatus.CONFLICT); } - - } //주어진 날짜에 해당하는 일정 조회 @@ -92,27 +80,21 @@ public List getSchedule(LocalDate localDate) throws BaseException { } return schedules; } catch (BaseException e) { - throw new BaseException(ResponseStatus.NOT_FOUND); // 현재의 예외를 다시 던져줍니다. + throw new BaseException(ResponseStatus.NOT_FOUND); } - } - //오늘에 해당하는 일정 조회 public List getTodaySchedules() throws BaseException { try { LocalDate today = LocalDate.now(); List todaySchedules = scheduleRepository.findByLocalDate(today); - if (todaySchedules.isEmpty()) { throw new BaseException(ResponseStatus.NOT_FOUND); } - return todaySchedules; - } catch (BaseException e) { - - throw new BaseException(ResponseStatus.NOT_FOUND); // 현재의 예외를 다시 던져줍니다. + throw new BaseException(ResponseStatus.NOT_FOUND); } } @@ -121,14 +103,12 @@ public List getTodaySchedules() throws BaseException { public Long deleteSchedule(Long scheduleId) throws BaseException { try { Optional optionalSchedule = scheduleRepository.findById(scheduleId); - if (optionalSchedule.isEmpty()) { throw new BaseException(ResponseStatus.NOT_FOUND); } scheduleRepository.deleteById(scheduleId); return scheduleId; } catch (BaseException e) { - throw new BaseException(ResponseStatus.NOT_FOUND); } } @@ -136,44 +116,39 @@ public Long deleteSchedule(Long scheduleId) throws BaseException { //일정 수정 @Transactional public Schedule updateSchedule(Long id, ScheduleRequestDto requestDto) { - try { Schedule schedule = scheduleRepository.findById(id).orElse(null); - LocalDate newLocalDate = requestDto.getLocalDate(); - DayOfWeek newDayOfWeek = requestDto.getDayOfWeek(); String newContent = requestDto.getContent(); Boolean newDone = requestDto.getDone(); LocalTime newTime = requestDto.getTime(); String newPlace = requestDto.getPlace(); - String newMedicine = requestDto.getMedicine(); RepeatType newRepeatType = requestDto.getRepeatType(); - if (newLocalDate == null || newDayOfWeek == null || newTime == null || newContent == null || newDone == null || newRepeatType == null) { - throw new MissingRequiredFieldException("Required field(s) are missing in updated schedule"); + if (newLocalDate == null || newTime == null || newContent == null || newDone == null + || newRepeatType == null) { + throw new MissingRequiredFieldException( + "Required field(s) are missing in updated schedule"); } + // 여기서 중복 일정 검사를 수행하고 이미 존재하는 경우 예외를 던짐 List existingSchedules = scheduleRepository.findByLocalDate(newLocalDate); for (Schedule existingSchedule : existingSchedules) { - 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)) { + if (existingSchedule.getTime().equals(newTime) && + existingSchedule.getContent().equals(newContent) && + existingSchedule.getDone().equals(newDone) && + existingSchedule.getRepeatType().equals(newRepeatType) && + existingSchedule.getPlace().equals(newPlace) + ) { throw new BaseException(ResponseStatus.CONFLICT); } } schedule.setLocalDate(newLocalDate); - schedule.setDayOfWeek(newDayOfWeek); schedule.setContent(newContent); schedule.setDone(newDone); schedule.setTime(newTime); schedule.setPlace(newPlace); schedule.setRepeatType(newRepeatType); - schedule.setMedicine(newMedicine); return scheduleRepository.save(schedule); diff --git a/src/test/java/com/haemil/backend/HaemilApplicationTests.java b/src/test/java/com/haemil/backend/HaemilApplicationTests.java deleted file mode 100644 index 6ef47bb..0000000 --- a/src/test/java/com/haemil/backend/HaemilApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.haemil.backend; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class HaemilApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/src/test/java/com/haemil/backend/schedule/repository/ScheduleRepositoryTest.java b/src/test/java/com/haemil/backend/schedule/repository/ScheduleRepositoryTest.java deleted file mode 100644 index 77a5d28..0000000 --- a/src/test/java/com/haemil/backend/schedule/repository/ScheduleRepositoryTest.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.haemil.backend.schedule.repository; - -public class ScheduleRepositoryTest { -} diff --git a/src/test/java/com/haemil/backend/schedule/service/ScheduleServiceIntegrationTest.java b/src/test/java/com/haemil/backend/schedule/service/ScheduleServiceIntegrationTest.java deleted file mode 100644 index c8a80bf..0000000 --- a/src/test/java/com/haemil/backend/schedule/service/ScheduleServiceIntegrationTest.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.haemil.backend.schedule.service; - -public class ScheduleServiceIntegrationTest { -} diff --git a/src/test/java/com/haemil/backend/schedule/service/ScheduleServiceTest.java b/src/test/java/com/haemil/backend/schedule/service/ScheduleServiceTest.java deleted file mode 100644 index d3f37e1..0000000 --- a/src/test/java/com/haemil/backend/schedule/service/ScheduleServiceTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.haemil.backend.schedule.service; - -import com.haemil.backend.schedule.entity.Schedule; -import com.haemil.backend.schedule.repository.ScheduleRepository; -import com.haemil.backend.schedule.repository.ScheduleRepositoryTest; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.domain.Example; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; -import org.springframework.data.repository.query.FluentQuery; - -import java.time.DayOfWeek; -import java.time.LocalDate; -import java.time.LocalTime; -import java.util.List; -import java.util.Optional; -import java.util.function.Function; - -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - -@SpringBootTest -public class ScheduleServiceTest { - - @Autowired - ScheduleService scheduleService; - - @Autowired - ScheduleRepository scheduleRepository; - - @Test - void createSchedule(){ - //given - LocalDate creationDate = LocalDate.now(); - DayOfWeek dayOfWeek = creationDate.getDayOfWeek(); - String content = "Test Schedule"; - boolean important_schedule = true; - boolean fixed_schedule = false; - LocalTime time = LocalTime.of(5, 3); - String place = "Test Place"; - - //when - Schedule schedule = scheduleService.createSchedule(creationDate, dayOfWeek, content, important_schedule, fixed_schedule, time, place); - - //then - assertThat(schedule.getCreationDate()).isEqualTo(creationDate); - assertThat(schedule.getDayOfWeek()).isEqualTo(dayOfWeek); - assertThat(schedule.getContent()).isEqualTo(content); - assertThat(schedule.isImportant()).isEqualTo(important_schedule); - assertThat(schedule.isFixed()).isEqualTo(fixed_schedule); - assertThat(schedule.getTime()).isEqualTo(time); - assertThat(schedule.getPlace()).isEqualTo(place); - - } - - -}