Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 일정 CRUD #5 #6

Merged
merged 11 commits into from
Aug 30, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Getter
@Setter
@JsonPropertyOrder({"success", "message", "result"})
public class BaseResponse<T> {

Expand Down Expand Up @@ -36,6 +38,7 @@ public BaseResponse(ResponseStatus status) {
}



public ResponseEntity<BaseResponse> convert() {
System.out.println("convert call");
HttpHeaders headers = new HttpHeaders();
Expand Down
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"),
MISSING_REQUIRED_FIELD(415, HttpStatus.Series.CLIENT_ERROR, "Required field(s) are missing"),

// for mypage status
NO_USER(false, 472, "존재하지 않는 유저입니다."),
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/haemil/backend/global/config/SpringConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.haemil.backend.global.config;

import com.haemil.backend.schedule.repository.ScheduleRepository;
import com.haemil.backend.schedule.service.ScheduleService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringConfig {
private final ScheduleRepository scheduleRepository;

public SpringConfig(ScheduleRepository scheduleRepository){
this.scheduleRepository = scheduleRepository;
}

@Bean
public ScheduleService scheduleService(){
return new ScheduleService(scheduleRepository);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.haemil.backend.global.exception;

import com.haemil.backend.global.config.ResponseStatus;

public class BaseException extends RuntimeException {

private final ResponseStatus status;


public BaseException(ResponseStatus status) {
super(status.getMessage());
this.status = status;
}

public ResponseStatus getStatus() {
return status;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.haemil.backend.global.exception;

import com.haemil.backend.global.config.ResponseStatus;

public class MissingRequiredFieldException extends BaseException {

public MissingRequiredFieldException(String s) {
super(ResponseStatus.MISSING_REQUIRED_FIELD);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,120 @@
package com.haemil.backend.schedule.controller;

import com.haemil.backend.global.config.BaseResponse;
import com.haemil.backend.global.exception.BaseException;
import com.haemil.backend.map.service.MapService;
import com.haemil.backend.schedule.dto.ScheduleRequestDto;
import com.haemil.backend.schedule.dto.ScheduleResponseDto;
import com.haemil.backend.schedule.entity.Schedule;
import com.haemil.backend.schedule.service.ScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


@RestController
@RequestMapping("/schedules")
@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) {

try {
ScheduleResponseDto createSchedule = scheduleService.createSchedule(scheduleRequestDto);

// 맵 API 호출하여 맵 URL 얻어오는 부분
String mapUrl = mapService.getMapUrl(scheduleRequestDto.getPlace());
// 응답에 맵 URL을 포함하여 리턴
createSchedule.setMapUrl(mapUrl);

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) {
try {
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 {
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 {
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) {
try {
Schedule updatedSchedule = 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);

ScheduleResponseDto responseDto = new ScheduleResponseDto(updatedSchedule);
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.haemil.backend.schedule.dto;

import com.haemil.backend.schedule.entity.RepeatType;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;

//schedule에 데이터를 넣을 때의 입력 요청 값을 받음
@NoArgsConstructor
@Getter
public class ScheduleRequestDto {

private Long id;

private LocalDate localDate;

private DayOfWeek dayOfWeek;

private LocalTime time;

private String content;

private Boolean done;

private String place;

private String medicine;

private RepeatType repeatType;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.haemil.backend.schedule.dto;

import com.haemil.backend.schedule.entity.RepeatType;
import com.haemil.backend.schedule.entity.Schedule;
import lombok.Getter;
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 RepeatType repeatType;


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.repeatType = schedule.getRepeatType();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.haemil.backend.schedule.entity;


public enum RepeatType {
DAILY,
WEEKLY,
MONTHLY,
NONE
}
59 changes: 53 additions & 6 deletions src/main/java/com/haemil/backend/schedule/entity/Schedule.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,66 @@
package com.haemil.backend.schedule.entity;

import javax.persistence.Column;
import javax.persistence.Id;
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;

@Entity(name = "schedules")
@Setter
@Getter
@NoArgsConstructor
public class Schedule {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

//date는 생성과 수정 필요
private LocalDate date;
//일정의 실제 날짜 정보(year, month, day)
@Column(nullable = false)
private LocalDate localDate;

//요일
@Column(nullable = false)
private DayOfWeek dayOfWeek;

private DayOfWeek week;
//일정 시간(hour, minute, second, nano)
@Column(nullable = false)
private LocalTime time;

@Column(nullable = true, length = 100)
//일정 내용
@Column(nullable = false, length = 100)
private String content;

//일정 완료 여부
@Column(nullable = false)
private Boolean done;

//장소
@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;

private String mapUrl;

public String getMapUrl() {
return mapUrl;
}

public void setMapUrl(String mapUrl) {
this.mapUrl = mapUrl;
}

}
Loading