Skip to content

Commit

Permalink
/*
Browse files Browse the repository at this point in the history
1. 한 날짜에도 여러 이벤트가 있을 수 있음. 그래서 한 날짜에 대해서 상세보기를 하는 페이지를 추가하면 좋을거 같음
->
2. 그 달에 해당하는 이벤트만 가져옴 근데 만약 다른 달에 대한 케이스는 구현을 어떻게 해야할까 고민중
->
3. 해당 달에 해당하는 일정가져올 때 특별히 sorting 해야하는지
->
4. 엔티티 층에서 특정 DTO로 변환하는 로직을 갖고있는데 이 부분에 대한 처리
->
5. 데이터 입력받을 때 종료날짜가 시작날짜보다 빠른 경우에 대해서 처리!
->서비스계층에 처리 로직 만듦
*/
  • Loading branch information
juhhoho committed Mar 19, 2024
1 parent 9a104ec commit 89f5cb5
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package JWTLogIn.JWT.user.controller;

import JWTLogIn.JWT.user.dto.event.EventDTO;
import JWTLogIn.JWT.user.dto.event.PostEventDTO;
import JWTLogIn.JWT.user.dto.event.PutEventDTO;
import JWTLogIn.JWT.user.service.event.EventServiceImpl;
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.util.List;
import java.util.stream.Collectors;



@RestController
@RequiredArgsConstructor
@Log4j2
@RequestMapping("/tgwing.kr")
public class EventController {
private final EventServiceImpl eventServiceImpl;

@GetMapping("/calendar")
public ResponseEntity<List<EventDTO>> calendar_home(){
return ResponseEntity.ok(eventServiceImpl.findMonthlyEvents());
}

@GetMapping("/calendar/{event_id}")
public ResponseEntity<EventDTO> calendar_detail(@PathVariable("event_id")Long id){
return ResponseEntity.ok(eventServiceImpl.findDayEvent(id));
}
@PostMapping("/calendar/post")
public ResponseEntity<?> calendar_post(@Validated @RequestBody PostEventDTO postEventDTO, BindingResult bindingResult) throws Exception {
if (bindingResult.hasErrors()) {
// 유효성 검사 실패 시 클라이언트에게 오류 응답 반환
List<ObjectError> errors = bindingResult.getAllErrors();
List<String> errorMessages = errors.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.toList());
return ResponseEntity.badRequest().body(errorMessages);
}

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{
if (bindingResult.hasErrors()) {
// 유효성 검사 실패 시 클라이언트에게 오류 응답 반환
List<ObjectError> errors = bindingResult.getAllErrors();
List<String> errorMessages = errors.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.toList());
return ResponseEntity.badRequest().body(errorMessages);
}
eventServiceImpl.eventUpdate(putEventDTO, id);
return ResponseEntity.ok().build();
}
@DeleteMapping("/calendar/delete/{event_id}")
public ResponseEntity<?> calendar_delete(@PathVariable("event_id") Long id){
eventServiceImpl.eventDelete(id);
return ResponseEntity.ok().build();
}
}
28 changes: 28 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/dto/event/EventDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 EventDTO {
private String title;
private String description;
private String url;
private LocalDateTime startDate;
private LocalDateTime endDate;
private EventType eventType;
@Builder
public EventDTO(String title, String description, String url, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) {
this.title = title;
this.description = description;
this.url = url;
this.startDate = startDate;
this.endDate = endDate;
this.eventType = eventType;
}
}
42 changes: 42 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/dto/event/PostEventDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package JWTLogIn.JWT.user.dto.event;

import JWTLogIn.JWT.user.entity.Enum.EventType;
import JWTLogIn.JWT.user.entity.EventEntity;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostEventDTO {
private String title;
private String description;
private String url;
private LocalDateTime startDate;
private LocalDateTime endDate;
private EventType eventType;
@Builder
public PostEventDTO(String title, String description, String url, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) {
this.title = title;
this.description = description;
this.url = url;
this.startDate = startDate;
this.endDate = endDate;
this.eventType = eventType;
}

public static EventEntity toEntity(PostEventDTO postEventDTO){
EventEntity eventEntity = EventEntity.builder()
.title(postEventDTO.title)
.description(postEventDTO.description)
.url(postEventDTO.url)
.startDate(postEventDTO.startDate)
.endDate(postEventDTO.endDate)
.eventType(postEventDTO.eventType)
.build();
return eventEntity;
}
}
29 changes: 29 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/dto/event/PutEventDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 PutEventDTO {
private String title;
private String description;
private String url;
private LocalDateTime startDate;
private LocalDateTime endDate;
private EventType eventType;
@Builder
public PutEventDTO(String title, String description, String url, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) {
this.title = title;
this.description = description;
this.url = url;
this.startDate = startDate;
this.endDate = endDate;
this.eventType = eventType;
}

}
15 changes: 15 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/entity/Enum/EventType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package JWTLogIn.JWT.user.entity.Enum;

import lombok.Getter;

@Getter
public enum EventType {
SCHOOL("SCHOOL"),
CLUB("CLUB"),
HOLIDAY("HOLIDAY");

private String value;
EventType(String value) {
this.value = value;
}
}
73 changes: 73 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/entity/EventEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "event")
public class EventEntity extends BaseEntity{
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "title",nullable = false, length = 25)
private String title;

@Column(name = "description", length=200)
private String description;

@Column(name="url")
private String url;

@Column(name = "start_date", nullable = false)
private LocalDateTime startDate;

@Column(name = "end_date", nullable = false)
private LocalDateTime endDate;

@Column(name = "event_type", nullable = false)
@Enumerated(EnumType.STRING)
private EventType eventType;

@Builder
public EventEntity(String title, String description, String url, LocalDateTime startDate, LocalDateTime endDate, EventType eventType) {
this.title = title;
this.description = description;
this.url = url;
this.startDate = startDate;
this.endDate = endDate;
this.eventType = eventType;
}



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 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package JWTLogIn.JWT.user.repository;

import JWTLogIn.JWT.user.entity.EventEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface EventRepository extends JpaRepository<EventEntity, Long> {
@Query("SELECT e FROM EventEntity e WHERE YEAR(e.startDate) = YEAR(CURRENT_DATE()) AND MONTH(e.startDate) = MONTH(CURRENT_DATE())")
List<EventEntity> findByCurrentMonthEvents();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package JWTLogIn.JWT.user.service.event;

import JWTLogIn.JWT.user.dto.event.EventDTO;
import JWTLogIn.JWT.user.dto.event.PostEventDTO;
import JWTLogIn.JWT.user.dto.event.PutEventDTO;

import java.time.LocalDateTime;
import java.util.List;

public interface EventService {
List<EventDTO> findMonthlyEvents();


EventDTO findDayEvent(Long id);

void eventSave(PostEventDTO postEventDTO);

void eventUpdate(PutEventDTO putEventDTO, Long id);

void eventDelete(Long id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package JWTLogIn.JWT.user.service.event;

import JWTLogIn.JWT.user.dto.event.EventDTO;
import JWTLogIn.JWT.user.dto.event.PostEventDTO;
import JWTLogIn.JWT.user.dto.event.PutEventDTO;
import JWTLogIn.JWT.user.entity.EventEntity;
import JWTLogIn.JWT.user.repository.EventRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
public class EventServiceImpl implements EventService{

private final EventRepository eventRepository;
@Override
public List<EventDTO> findMonthlyEvents() {

List<EventEntity> monthlyEvents = eventRepository.findByCurrentMonthEvents();
if(monthlyEvents == null){
return null;
}
else{
return monthlyEvents.stream()
.map(EventEntity::toEventDTO)
.toList();
}
}

@Override
public EventDTO findDayEvent(Long id) {
Optional<EventEntity> findEvent = eventRepository.findById(id);
if (findEvent.isEmpty()){
return null;
}
else{
EventEntity eventEntity = findEvent.get();
return EventEntity.toEventDTO(eventEntity);
}
}

@Override
public void eventSave(PostEventDTO postEventDTO) {
EventEntity eventEntity = PostEventDTO.toEntity(postEventDTO);
validateDates(eventEntity.getStartDate(), eventEntity.getEndDate());
eventRepository.save(eventEntity);
}

@Override
public void eventUpdate(PutEventDTO putEventDTO, Long id) {
Optional<EventEntity> findEvent = eventRepository.findById(id);
if(findEvent.isPresent()){
EventEntity eventEntity = findEvent.get();
eventEntity.updateByPutEventDTO(putEventDTO);
validateDates(eventEntity.getStartDate(), eventEntity.getEndDate());
}
}


@Override
public void eventDelete(Long id) {
Optional<EventEntity> findEvent = eventRepository.findById(id);
if(findEvent.isPresent()){
EventEntity eventEntity = findEvent.get();
eventRepository.delete(eventEntity);
}

}

// start_date와 end_date의 선후 관계 검증을 위한 로직
private void validateDates(LocalDateTime startDate, LocalDateTime endDate) {
if (startDate != null && endDate != null && endDate.isBefore(startDate)) {
throw new IllegalArgumentException("End date cannot be before start date");
}
}
}
Loading

0 comments on commit 89f5cb5

Please sign in to comment.