Skip to content

Commit

Permalink
entity 계층 dto변환 로직 분리
Browse files Browse the repository at this point in the history
calendar 관련 예외처리
  • Loading branch information
juhhoho committed Mar 25, 2024
1 parent 9cf6dd4 commit 93f9934
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ResponseEntity<List<EventBriefDTO>> calendar_main(
}


// day==0 이면 일정보가 필요없어서 디폴트 값이 할당된것
// day==0 이면 일정보가 필요없어서 디폴트 값이 할당된것 ->
if(day == 0){
return ResponseEntity.ok(eventServiceImpl.findMonthlyEvents(year, month));
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public ResponseEntity<?> calendar_put(@Valid @RequestBody UpdateEventDTO updateE
public ResponseEntity<?> calendar_delete(@PathVariable("event_id") Long id){
// 예외처리: event_id를 pk로 갖지 않으면 예외
if (!eventServiceImpl.checkById(id)){
throw new NotFoundException("존재하지 않는 이벤트");
throw new NotFoundException("존재하지 않는 이벤트 오류");
}
eventServiceImpl.eventDelete(id);
return ResponseEntity.ok().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@

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

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package JWTLogIn.JWT.user.repository;

import JWTLogIn.JWT.user.entity.EventEntity;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface EventRepositoryCustom {
List<EventEntity> findDayEvents(int year, int month, int day);

List<EventEntity> findMonthEvents(int year, int month);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public List<EventEntity> findDayEvents(int year, int month, int day) {
.setParameter("findDate", findDate)
.getResultList();
}

@Override
public List<EventEntity> findMonthEvents(int year, int month) {
return em.createQuery("SELECT e FROM EventEntity e WHERE YEAR(e.startDate) = :year AND MONTH(e.startDate) = :month")
.setParameter("year", year)
.setParameter("month", month)
.getResultList();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class EventServiceImpl implements EventService{
@Override
public List<EventBriefDTO> findMonthlyEvents(int year, int month) {

List<EventEntity> monthlyEvents = eventRepository.findMonthEvents(year, month);
List<EventEntity> monthlyEvents = eventRepositoryCustom.findMonthEvents(year, month);
if(monthlyEvents == null){
return null;
}
Expand Down Expand Up @@ -68,7 +68,6 @@ public EventDetailDTO findDayEvent(Long id) {
@Override
public void eventSave(PostEventDTO postEventDTO) {
EventEntity eventEntity = PostEventDTO.toEntity(postEventDTO);
validateDates(eventEntity.getStartDate(), eventEntity.getEndDate());
eventRepository.save(eventEntity);
}

Expand All @@ -78,7 +77,6 @@ public void eventUpdate(UpdateEventDTO updateEventDTO, Long id) {
if(findEvent.isPresent()){
EventEntity eventEntity = findEvent.get();
updateEntityByPutEventDTO(eventEntity, updateEventDTO);
validateDates(eventEntity.getStartDate(), eventEntity.getEndDate());
}
}

Expand All @@ -95,13 +93,6 @@ public void eventDelete(Long id) {

//-----------------------------------------------------------------------------------------------------

// start_date와 end_date의 선후 관계 검증을 위한 로직
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()
Expand Down

0 comments on commit 93f9934

Please sign in to comment.