Skip to content

Commit

Permalink
Merge pull request #6 from hanium-haemil/feature/schedule
Browse files Browse the repository at this point in the history
[FEAT] 일정 CRUD #5
  • Loading branch information
devyubin authored Aug 30, 2023
2 parents 3c2e974 + 2f3f8a0 commit 114d98c
Show file tree
Hide file tree
Showing 14 changed files with 598 additions and 6 deletions.
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 @@ -17,6 +17,7 @@ public enum ResponseStatus {
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
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,121 @@
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 updateSchedule = scheduleService.updateSchedule(scheduleId, requestDto);
log.debug("requestDto.getPlace() = {}",requestDto.getPlace());

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

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

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);
}
}
}
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,63 @@
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.mapUrl = schedule.getMapUrl();

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

0 comments on commit 114d98c

Please sign in to comment.