diff --git a/JWT/build.gradle b/JWT/build.gradle index c548493..101a628 100644 --- a/JWT/build.gradle +++ b/JWT/build.gradle @@ -44,6 +44,9 @@ dependencies { // // Spring Security testImplementation 'org.springframework.security:spring-security-test' testImplementation 'org.springframework.boot:spring-boot-starter-test' + + // + implementation 'org.springframework.boot:spring-boot-starter-validation' } tasks.named('test') { diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/advice/EventControllerAdvice.java b/JWT/src/main/java/JWTLogIn/JWT/user/advice/EventControllerAdvice.java new file mode 100644 index 0000000..14ed98b --- /dev/null +++ b/JWT/src/main/java/JWTLogIn/JWT/user/advice/EventControllerAdvice.java @@ -0,0 +1,36 @@ +package JWTLogIn.JWT.user.advice; + +import JWTLogIn.JWT.user.dto.error.ErrorResultDTO; +import JWTLogIn.JWT.user.exception.BadRequestException; +import JWTLogIn.JWT.user.exception.NotFoundException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Slf4j +@RestControllerAdvice(basePackages = "JWTLogIn.JWT.user.controller") +public class EventControllerAdvice { + + @ExceptionHandler(NotFoundException.class) + public ResponseEntity notFoundExHandler(NotFoundException e){ + ErrorResultDTO errorResult = new ErrorResultDTO("NOT_FOUND", e.getMessage()); + return new ResponseEntity<>(errorResult, HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(BadRequestException.class) + public ResponseEntity badRequestException(BadRequestException e) { + ErrorResultDTO errorResult = new ErrorResultDTO("BAD_REQUEST", e.getMessage()); + return new ResponseEntity<>(errorResult, HttpStatus.BAD_REQUEST); + } + + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler + public ResponseEntity baseException(Exception e){ + ErrorResultDTO errorResult = new ErrorResultDTO("INTERNAL_SERVER_ERROR", e.getMessage()); + return new ResponseEntity<>(errorResult, HttpStatus.INTERNAL_SERVER_ERROR); + } +} + diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/controller/EventController.java b/JWT/src/main/java/JWTLogIn/JWT/user/controller/EventController.java index 2566ece..135b704 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/user/controller/EventController.java +++ b/JWT/src/main/java/JWTLogIn/JWT/user/controller/EventController.java @@ -1,21 +1,22 @@ package JWTLogIn.JWT.user.controller; -import JWTLogIn.JWT.user.dto.event.EventDTO; +import JWTLogIn.JWT.user.dto.event.EventBriefDTO; +import JWTLogIn.JWT.user.exception.BadRequestException; +import JWTLogIn.JWT.user.dto.event.EventDetailDTO; import JWTLogIn.JWT.user.dto.event.PostEventDTO; -import JWTLogIn.JWT.user.dto.event.PutEventDTO; +import JWTLogIn.JWT.user.dto.event.UpdateEventDTO; +import JWTLogIn.JWT.user.exception.NotFoundException; import JWTLogIn.JWT.user.service.event.EventServiceImpl; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; -import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.http.ResponseEntity; import org.springframework.validation.BindingResult; -import org.springframework.validation.ObjectError; -import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.time.LocalDateTime; +import java.time.YearMonth; import java.util.List; -import java.util.stream.Collectors; - @RestController @RequiredArgsConstructor @@ -25,45 +26,74 @@ public class EventController { private final EventServiceImpl eventServiceImpl; @GetMapping("/calendar") - public ResponseEntity> calendar_home(@RequestParam("year") int year, @RequestParam("month") int month){ - return ResponseEntity.ok(eventServiceImpl.findMonthlyEvents(year, month)); + public ResponseEntity> calendar_main( + @RequestParam("year") int year, @RequestParam("month") int month, @RequestParam(name = "day", defaultValue = "0") int day){ + // 예외처리: 2020~현재 연도를 벗어나는 연도 대해 예외 + if(year < 2020 || year >(int)LocalDateTime.now().getYear()){ + throw new BadRequestException("년도 입력 오류"); + } + // 예외처리: 1~12월을 벗어나는 월에 대해 예외 + if(month < 1 || month >12){ + throw new BadRequestException("월 입력 오류"); + } + if(day > getLastDay(month) || day < 0){ + throw new BadRequestException("날짜 입력 오류"); + } + + + // day==0 이면 일정보가 필요없어서 디폴트 값이 할당된것 + if(day == 0){ + return ResponseEntity.ok(eventServiceImpl.findMonthlyEvents(year, month)); + } + return ResponseEntity.ok(eventServiceImpl.findDayEvents(year, month, day)); } @GetMapping("/calendar/{event_id}") - public ResponseEntity calendar_detail(@PathVariable("event_id")Long id){ + public ResponseEntity calendar_detail(@PathVariable("event_id")Long id){ + // 예외처리: event_id를 pk로 갖지 않으면 예외 + if (!eventServiceImpl.checkById(id)){ + throw new NotFoundException("존재하지 않는 이벤트"); + } return ResponseEntity.ok(eventServiceImpl.findDayEvent(id)); } - @PostMapping("/calendar/post") - public ResponseEntity calendar_post(@Validated @RequestBody PostEventDTO postEventDTO, BindingResult bindingResult) throws Exception { + @PostMapping("/calendar") + public ResponseEntity calendar_post(@Valid @RequestBody PostEventDTO postEventDTO, BindingResult bindingResult) { + // 예외처리: PostEventDTO 대한 입력 정보 오류 if (bindingResult.hasErrors()) { - // 유효성 검사 실패 시 클라이언트에게 오류 응답 반환 - List errors = bindingResult.getAllErrors(); - List errorMessages = errors.stream() - .map(DefaultMessageSourceResolvable::getDefaultMessage) - .collect(Collectors.toList()); - return ResponseEntity.badRequest().body(errorMessages); + throw new BadRequestException("글 게시 입력 정보 오류"); + } + if(postEventDTO.getStartDate().compareTo(postEventDTO.getEndDate()) > 0){ + throw new BadRequestException("글 게시 시간 선후 정보 오류"); } - eventServiceImpl.eventSave(postEventDTO); return ResponseEntity.ok().build(); } - @PutMapping("/calendar/put/{event_id}") - public ResponseEntity calendar_put(@Validated @RequestBody PutEventDTO putEventDTO, BindingResult bindingResult, @PathVariable("event_id") Long id) throws Exception{ + + @PostMapping("/calendar/{event_id}") + public ResponseEntity calendar_put(@Valid @RequestBody UpdateEventDTO updateEventDTO, BindingResult bindingResult, @PathVariable("event_id") Long id){ + // 예외처리: UpdateEventDTO 대한 입력 정보 오류 if (bindingResult.hasErrors()) { - // 유효성 검사 실패 시 클라이언트에게 오류 응답 반환 - List errors = bindingResult.getAllErrors(); - List errorMessages = errors.stream() - .map(DefaultMessageSourceResolvable::getDefaultMessage) - .collect(Collectors.toList()); - return ResponseEntity.badRequest().body(errorMessages); + throw new BadRequestException("글 수정 입력 정보 오류"); } - eventServiceImpl.eventUpdate(putEventDTO, id); + eventServiceImpl.eventUpdate(updateEventDTO, id); return ResponseEntity.ok().build(); } - @DeleteMapping("/calendar/delete/{event_id}") + @DeleteMapping("/calendar/{event_id}") public ResponseEntity calendar_delete(@PathVariable("event_id") Long id){ + // 예외처리: event_id를 pk로 갖지 않으면 예외 + if (!eventServiceImpl.checkById(id)){ + throw new NotFoundException("존재하지 않는 이벤트"); + } eventServiceImpl.eventDelete(id); return ResponseEntity.ok().build(); } + + //------------------------------------------------------------------------------------------ + + // 이달의 마지막 날을 구하는 로직 + public int getLastDay(int month) { + YearMonth yearMonth = YearMonth.of(LocalDateTime.now().getYear(), month); + return yearMonth.lengthOfMonth(); + } } \ No newline at end of file diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/dto/error/ErrorResultDTO.java b/JWT/src/main/java/JWTLogIn/JWT/user/dto/error/ErrorResultDTO.java new file mode 100644 index 0000000..384269a --- /dev/null +++ b/JWT/src/main/java/JWTLogIn/JWT/user/dto/error/ErrorResultDTO.java @@ -0,0 +1,11 @@ +package JWTLogIn.JWT.user.dto.error; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class ErrorResultDTO { + private String code; + private String message; +} diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventBriefDTO.java b/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventBriefDTO.java new file mode 100644 index 0000000..bca58cb --- /dev/null +++ b/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventBriefDTO.java @@ -0,0 +1,25 @@ +package JWTLogIn.JWT.user.dto.event; + +import JWTLogIn.JWT.user.entity.Enum.EventType; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Data +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class EventBriefDTO { + private String title; + private LocalDateTime startDate; + private LocalDateTime endDate; + private EventType eventType; + @Builder + public EventBriefDTO(String title, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) { + this.title = title; + this.startDate = startDate; + this.endDate = endDate; + this.eventType = eventType; + } +} diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventDTO.java b/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventDetailDTO.java similarity index 79% rename from JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventDTO.java rename to JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventDetailDTO.java index fb01e52..752c484 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventDTO.java +++ b/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventDetailDTO.java @@ -9,7 +9,7 @@ import java.time.LocalDateTime; @Data @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class EventDTO { +public class EventDetailDTO { private String title; private String description; private String url; @@ -17,7 +17,7 @@ public class EventDTO { private LocalDateTime endDate; private EventType eventType; @Builder - public EventDTO(String title, String description, String url, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) { + public EventDetailDTO(String title, String description, String url, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) { this.title = title; this.description = description; this.url = url; diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/PostEventDTO.java b/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/PostEventDTO.java index c2eb549..a5e74a7 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/PostEventDTO.java +++ b/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/PostEventDTO.java @@ -2,22 +2,37 @@ import JWTLogIn.JWT.user.entity.Enum.EventType; import JWTLogIn.JWT.user.entity.EventEntity; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; import lombok.AccessLevel; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.springframework.lang.Nullable; import java.time.LocalDateTime; @Data @NoArgsConstructor(access = AccessLevel.PROTECTED) public class PostEventDTO { + + @Size(min=1) private String title; + private String description; + private String url; + + @NotNull private LocalDateTime startDate; + + @NotNull private LocalDateTime endDate; + + @NotNull private EventType eventType; + @Builder public PostEventDTO(String title, String description, String url, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) { this.title = title; diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/PutEventDTO.java b/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/UpdateEventDTO.java similarity index 79% rename from JWT/src/main/java/JWTLogIn/JWT/user/dto/event/PutEventDTO.java rename to JWT/src/main/java/JWTLogIn/JWT/user/dto/event/UpdateEventDTO.java index 30067f2..dd0d73f 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/PutEventDTO.java +++ b/JWT/src/main/java/JWTLogIn/JWT/user/dto/event/UpdateEventDTO.java @@ -9,7 +9,7 @@ import java.time.LocalDateTime; @Data @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class PutEventDTO { +public class UpdateEventDTO { private String title; private String description; private String url; @@ -17,7 +17,7 @@ public class PutEventDTO { private LocalDateTime endDate; private EventType eventType; @Builder - public PutEventDTO(String title, String description, String url, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) { + public UpdateEventDTO(String title, String description, String url, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) { this.title = title; this.description = description; this.url = url; @@ -25,5 +25,4 @@ public PutEventDTO(String title, String description, String url, LocalDateTime s this.endDate = endDate; this.eventType = eventType; } - } diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/entity/EventEntity.java b/JWT/src/main/java/JWTLogIn/JWT/user/entity/EventEntity.java index a92f581..68210e1 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/user/entity/EventEntity.java +++ b/JWT/src/main/java/JWTLogIn/JWT/user/entity/EventEntity.java @@ -1,7 +1,5 @@ package JWTLogIn.JWT.user.entity; -import JWTLogIn.JWT.user.dto.event.EventDTO; -import JWTLogIn.JWT.user.dto.event.PutEventDTO; import JWTLogIn.JWT.user.entity.Enum.EventType; import jakarta.persistence.*; import lombok.AccessLevel; @@ -48,26 +46,27 @@ public EventEntity(String title, String description, String url, LocalDateTime s this.eventType = eventType; } + public void setTitle(String title) { + this.title = title; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setUrl(String url) { + this.url = url; + } + public void setStartDate(LocalDateTime startDate) { + this.startDate = startDate; + } - public static EventDTO toEventDTO(EventEntity eventEntity){ - EventDTO eventDTO = EventDTO.builder() - .title(eventEntity.getTitle()) - .description(eventEntity.getDescription()) - .url(eventEntity.getUrl()) - .startDate(eventEntity.getStartDate()) - .endDate(eventEntity.getEndDate()) - .eventType(eventEntity.getEventType()) - .build(); - return eventDTO; + public void setEndDate(LocalDateTime endDate) { + this.endDate = endDate; } - public void updateByPutEventDTO(PutEventDTO putEventDTO){ - this.title = putEventDTO.getTitle(); - this.description = putEventDTO.getDescription(); - this.url = putEventDTO.getUrl(); - this.startDate = putEventDTO.getStartDate(); - this.endDate = putEventDTO.getEndDate(); - this.eventType = putEventDTO.getEventType(); + public void setEventType(EventType eventType) { + this.eventType = eventType; } } diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/exception/BadRequestException.java b/JWT/src/main/java/JWTLogIn/JWT/user/exception/BadRequestException.java new file mode 100644 index 0000000..292582f --- /dev/null +++ b/JWT/src/main/java/JWTLogIn/JWT/user/exception/BadRequestException.java @@ -0,0 +1,29 @@ +package JWTLogIn.JWT.user.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code= HttpStatus.BAD_REQUEST) +public class BadRequestException extends RuntimeException{ + + public BadRequestException() { + super(); + } + + public BadRequestException(String message) { + super(message); + } + + public BadRequestException(String message, Throwable cause) { + super(message, cause); + } + + public BadRequestException(Throwable cause) { + super(cause); + } + + protected BadRequestException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/exception/NotFoundException.java b/JWT/src/main/java/JWTLogIn/JWT/user/exception/NotFoundException.java new file mode 100644 index 0000000..2aa7fca --- /dev/null +++ b/JWT/src/main/java/JWTLogIn/JWT/user/exception/NotFoundException.java @@ -0,0 +1,27 @@ +package JWTLogIn.JWT.user.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.NOT_FOUND) +public class NotFoundException extends RuntimeException{ + public NotFoundException() { + super(); + } + + public NotFoundException(String message) { + super(message); + } + + public NotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public NotFoundException(Throwable cause) { + super(cause); + } + + protected NotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepository.java b/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepository.java index fd3c73d..b7590f1 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepository.java +++ b/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepository.java @@ -12,5 +12,6 @@ @Repository public interface EventRepository extends JpaRepository { @Query("SELECT e FROM EventEntity e WHERE YEAR(e.startDate) = :year AND MONTH(e.startDate) = :month") - List findByCurrentMonthEvents(int year, int month); + List findMonthEvents(int year, int month); + } \ No newline at end of file diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepositoryCustom.java b/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepositoryCustom.java new file mode 100644 index 0000000..5357c57 --- /dev/null +++ b/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepositoryCustom.java @@ -0,0 +1,9 @@ +package JWTLogIn.JWT.user.repository; + +import JWTLogIn.JWT.user.entity.EventEntity; + +import java.util.List; + +public interface EventRepositoryCustom { + List findDayEvents(int year, int month, int day); +} diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepositoryCustomImpl.java b/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepositoryCustomImpl.java new file mode 100644 index 0000000..36a3003 --- /dev/null +++ b/JWT/src/main/java/JWTLogIn/JWT/user/repository/EventRepositoryCustomImpl.java @@ -0,0 +1,22 @@ +package JWTLogIn.JWT.user.repository; + +import JWTLogIn.JWT.user.entity.EventEntity; +import jakarta.persistence.EntityManager; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +import java.time.LocalDateTime; +import java.util.List; +@Repository +@RequiredArgsConstructor +public class EventRepositoryCustomImpl implements EventRepositoryCustom{ + + private final EntityManager em; + @Override + public List findDayEvents(int year, int month, int day) { + LocalDateTime findDate = LocalDateTime.of(year, month, day, 0, 0); + return em.createQuery("SELECT e FROM EventEntity e WHERE FUNCTION('DATE', :findDate) BETWEEN FUNCTION('DATE', e.startDate) AND FUNCTION('DATE', e.endDate)") + .setParameter("findDate", findDate) + .getResultList(); + } +} diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/service/event/EventService.java b/JWT/src/main/java/JWTLogIn/JWT/user/service/event/EventService.java index 9bea463..fbeb89f 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/user/service/event/EventService.java +++ b/JWT/src/main/java/JWTLogIn/JWT/user/service/event/EventService.java @@ -1,22 +1,24 @@ package JWTLogIn.JWT.user.service.event; -import JWTLogIn.JWT.user.dto.event.EventDTO; +import JWTLogIn.JWT.user.dto.event.EventBriefDTO; +import JWTLogIn.JWT.user.dto.event.EventDetailDTO; import JWTLogIn.JWT.user.dto.event.PostEventDTO; -import JWTLogIn.JWT.user.dto.event.PutEventDTO; +import JWTLogIn.JWT.user.dto.event.UpdateEventDTO; -import java.time.LocalDateTime; import java.util.List; public interface EventService { - List findMonthlyEvents(int year, int month); + List findMonthlyEvents(int year, int month); + List findDayEvents(int year, int month, int day); - - EventDTO findDayEvent(Long id); + EventDetailDTO findDayEvent(Long id); void eventSave(PostEventDTO postEventDTO); - void eventUpdate(PutEventDTO putEventDTO, Long id); + void eventUpdate(UpdateEventDTO updateEventDTO, Long id); void eventDelete(Long id); + boolean checkById(Long id); + } \ No newline at end of file diff --git a/JWT/src/main/java/JWTLogIn/JWT/user/service/event/EventServiceImpl.java b/JWT/src/main/java/JWTLogIn/JWT/user/service/event/EventServiceImpl.java index 9f0fd7d..30dd624 100644 --- a/JWT/src/main/java/JWTLogIn/JWT/user/service/event/EventServiceImpl.java +++ b/JWT/src/main/java/JWTLogIn/JWT/user/service/event/EventServiceImpl.java @@ -1,16 +1,19 @@ package JWTLogIn.JWT.user.service.event; -import JWTLogIn.JWT.user.dto.event.EventDTO; +import JWTLogIn.JWT.user.dto.event.EventBriefDTO; +import JWTLogIn.JWT.user.dto.event.EventDetailDTO; import JWTLogIn.JWT.user.dto.event.PostEventDTO; -import JWTLogIn.JWT.user.dto.event.PutEventDTO; +import JWTLogIn.JWT.user.dto.event.UpdateEventDTO; import JWTLogIn.JWT.user.entity.EventEntity; import JWTLogIn.JWT.user.repository.EventRepository; +import JWTLogIn.JWT.user.repository.EventRepositoryCustomImpl; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.time.LocalDateTime; +import java.util.Comparator; import java.util.List; import java.util.Optional; @@ -20,29 +23,45 @@ public class EventServiceImpl implements EventService{ private final EventRepository eventRepository; + private final EventRepositoryCustomImpl eventRepositoryCustom; @Override - public List findMonthlyEvents(int year, int month) { + public List findMonthlyEvents(int year, int month) { - List monthlyEvents = eventRepository.findByCurrentMonthEvents(year, month); + List monthlyEvents = eventRepository.findMonthEvents(year, month); if(monthlyEvents == null){ return null; } else{ return monthlyEvents.stream() - .map(EventEntity::toEventDTO) + .map(EventServiceImpl::entity2EventBriefDTO) + .sorted(Comparator.comparing(EventBriefDTO::getStartDate)) // startDate를 기준으로 정렬 .toList(); } } @Override - public EventDTO findDayEvent(Long id) { + public List findDayEvents(int year, int month, int day) { + List dayEvents = eventRepositoryCustom.findDayEvents(year, month, day); + if(dayEvents == null){ + return null; + } + else{ + return dayEvents.stream() + .map(EventServiceImpl::entity2EventBriefDTO) + .sorted(Comparator.comparing(EventBriefDTO::getStartDate)) // startDate를 기준으로 정렬 + .toList(); + } + } + + @Override + public EventDetailDTO findDayEvent(Long id) { Optional findEvent = eventRepository.findById(id); if (findEvent.isEmpty()){ return null; } else{ EventEntity eventEntity = findEvent.get(); - return EventEntity.toEventDTO(eventEntity); + return entity2EventDetailDTO(eventEntity); } } @@ -54,11 +73,11 @@ public void eventSave(PostEventDTO postEventDTO) { } @Override - public void eventUpdate(PutEventDTO putEventDTO, Long id) { + public void eventUpdate(UpdateEventDTO updateEventDTO, Long id) { Optional findEvent = eventRepository.findById(id); if(findEvent.isPresent()){ EventEntity eventEntity = findEvent.get(); - eventEntity.updateByPutEventDTO(putEventDTO); + updateEntityByPutEventDTO(eventEntity, updateEventDTO); validateDates(eventEntity.getStartDate(), eventEntity.getEndDate()); } } @@ -74,10 +93,57 @@ public void eventDelete(Long id) { } + //----------------------------------------------------------------------------------------------------- + // start_date와 end_date의 선후 관계 검증을 위한 로직 - private void validateDates(LocalDateTime startDate, LocalDateTime endDate) { + public void validateDates(LocalDateTime startDate, LocalDateTime endDate) { if (startDate != null && endDate != null && endDate.isBefore(startDate)) { throw new IllegalArgumentException("End date cannot be before start date"); } } + + // EvenEntity를 EventDetailDTO로 변환하는 로직 + public static EventDetailDTO entity2EventDetailDTO(EventEntity eventEntity){ + EventDetailDTO eventDetailDTO = EventDetailDTO.builder() + .title(eventEntity.getTitle()) + .description(eventEntity.getDescription()) + .url(eventEntity.getUrl()) + .startDate(eventEntity.getStartDate()) + .endDate(eventEntity.getEndDate()) + .eventType(eventEntity.getEventType()) + .build(); + return eventDetailDTO; + } + + // EvenEntity를 EventBriefDTO 변환하는 로직 + public static EventBriefDTO entity2EventBriefDTO(EventEntity eventEntity){ + EventBriefDTO eventBriefDTO = EventBriefDTO.builder() + .title(eventEntity.getTitle()) + .startDate(eventEntity.getStartDate()) + .endDate(eventEntity.getEndDate()) + .eventType(eventEntity.getEventType()) + .build(); + return eventBriefDTO; + } + + + public EventEntity updateEntityByPutEventDTO(EventEntity eventEntity, UpdateEventDTO updateEventDTO) { + eventEntity.setTitle(updateEventDTO.getTitle()); + eventEntity.setDescription(updateEventDTO.getDescription()); + eventEntity.setUrl(updateEventDTO.getUrl()); + eventEntity.setStartDate(updateEventDTO.getStartDate()); + eventEntity.setEndDate(updateEventDTO.getEndDate()); + eventEntity.setEventType(updateEventDTO.getEventType()); + + return eventEntity; + } + // + public boolean checkById(Long id){ + if(eventRepository.findById(id).isPresent()){ + return true; + } else{ + return false; + } + } + }