Skip to content

Commit

Permalink
Merge pull request #32 from hanium-haemil/feature/n_schedule
Browse files Browse the repository at this point in the history
[REFACTOR] code refactoring - schedule api
  • Loading branch information
hisemsem authored Oct 12, 2023
2 parents 4106917 + fc60959 commit 09e0be0
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<BaseResponse> createSchedule(@RequestBody ScheduleRequestDto scheduleRequestDto) {

public ResponseEntity<BaseResponse> 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<ScheduleResponseDto> 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<BaseResponse> getSchedulesByDate(@RequestParam("localDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate localDate) {
public ResponseEntity<BaseResponse> getSchedulesByDate(
@RequestParam("localDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate localDate) {
try {
//원하는 schedule 조회
List<Schedule> schedules = scheduleService.getSchedule(localDate);
return new BaseResponse<>(schedules).convert();

} catch (BaseException e) {
//에러 처리
return new BaseResponse<>(e.getStatus()).convert();
}
}


//오늘 일정 조회 API
@GetMapping("/today")
public ResponseEntity<BaseResponse> getTodaySchedules() {
try {
//오늘 schedule 조회
List<Schedule> todaySchedules = scheduleService.getTodaySchedules();
return new BaseResponse<>(todaySchedules).convert();

} catch (BaseException e) {
//에러 처리
return new BaseResponse<>(e.getStatus()).convert();
}
}


//일정 삭제 API
@DeleteMapping("/schedule/{scheduleId}")
public ResponseEntity<BaseResponse> deleteSchedule(@PathVariable Long scheduleId) {
try {
//schedule 삭제
Long deletedId = scheduleService.deleteSchedule(scheduleId);
BaseResponse<Long> response = new BaseResponse<>(deletedId);
return response.convert();
} catch (BaseException e) {
//에러 처리
return new BaseResponse<>(e.getStatus()).convert();
}
}


//일정 수정 API
@PatchMapping("/schedule/{scheduleId}")
public ResponseEntity<BaseResponse> updateSchedule(@PathVariable Long scheduleId, @RequestBody ScheduleRequestDto requestDto) {
public ResponseEntity<BaseResponse> 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<ScheduleResponseDto> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,8 +15,6 @@ public class ScheduleRequestDto {

private LocalDate localDate;

private DayOfWeek dayOfWeek;

private LocalTime time;

private String content;
Expand All @@ -27,8 +23,6 @@ public class ScheduleRequestDto {

private String place;

private String medicine;

private RepeatType repeatType;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
}
21 changes: 2 additions & 19 deletions src/main/java/com/haemil/backend/schedule/entity/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Schedule,Long> {
public interface ScheduleRepository extends JpaRepository<Schedule, Long> {

//일정을 조회
List<Schedule> findByLocalDate(LocalDate localDate);
Expand Down
Loading

0 comments on commit 09e0be0

Please sign in to comment.