Skip to content

Commit

Permalink
refactor: 공연 도메인 예외처리 리팩토링 (#174)
Browse files Browse the repository at this point in the history
* refactor: #169 EventErrorCode -> BaseErrorCode Implements & 예외 처리 코드 리팩토링

* refactor: #169 계층간 DTO 분리를 위해 검색관련 DTO 객체 추가 처리

* refactor: #169 전반적인 코드 포맷팅 리팩토링

* feat: #169 Enum 클래스 예외처리 로직 추가
  • Loading branch information
park0jae authored Jan 9, 2024
1 parent 1207a63 commit ddb42b1
Show file tree
Hide file tree
Showing 52 changed files with 585 additions and 477 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public record EventCreateRequest(
GenreType genreType,

@NotNull(message = "이벤트 홀 ID는 필수 입력값 입니다.")
Long eventHallId
) {
Long eventHallId) {
public Event toEntity(EventHall eventHall) {
return Event.builder()
.title(title)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.pgms.apievent.event.dto.request;

import java.time.LocalDateTime;

import com.pgms.coredomain.domain.event.GenreType;

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;

import java.time.LocalDateTime;

public record
EventUpdateRequest(

Expand Down Expand Up @@ -39,6 +40,5 @@
LocalDateTime bookingEndedAt,

@NotNull(message = "이벤트 홀 ID는 필수 입력값 입니다.")
Long eventHallId
) {
Long eventHallId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.pgms.apievent.event.dto.request.EventUpdateRequest;
import com.pgms.apievent.event.dto.response.EventResponse;
import com.pgms.apievent.event.repository.EventCustomRepository;
import com.pgms.apievent.exception.CustomException;
import com.pgms.apievent.exception.EventException;
import com.pgms.coredomain.domain.event.Event;
import com.pgms.coredomain.domain.event.EventEdit;
import com.pgms.coredomain.domain.event.EventHall;
Expand Down Expand Up @@ -86,7 +86,7 @@ public void deleteEventById(Long id) {

private void validateDuplicateEvent(String title) {
if (Boolean.TRUE.equals((eventRepository.existsEventByTitle(title)))) {
throw new CustomException(ALREADY_EXIST_EVENT);
throw new EventException(ALREADY_EXIST_EVENT);
}
}

Expand All @@ -109,11 +109,11 @@ private EventEdit getEventEdit(EventUpdateRequest request) {

private Event getEvent(Long eventId) {
return eventRepository.findById(eventId)
.orElseThrow(() -> new CustomException(EVENT_NOT_FOUND));
.orElseThrow(() -> new EventException(EVENT_NOT_FOUND));
}

private EventHall getEventHall(Long eventHallId) {
return eventHallRepository.findById(eventHallId)
.orElseThrow(() -> new CustomException(EVENT_HALL_NOT_FOUND));
.orElseThrow(() -> new EventException(EVENT_HALL_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.pgms.apievent.eventHall.dto.request;

import java.util.List;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import java.util.List;

public record EventHallCreateRequest(@NotBlank(message = "공연장 이름은 빈칸을 지정할 수 없습니다.")
@Size(max = 25, message = "공연장 이름은 10자 이내로 입력해주세요.") String name,
String address,
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests) {
public record EventHallCreateRequest(
@NotBlank(message = "공연장 이름은 빈칸을 지정할 수 없습니다.")
@Size(max = 25, message = "공연장 이름은 10자 이내로 입력해주세요.")
String name,
String address,
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record EventHallSeatCreateRequest(@NotBlank(message = "공연장 좌석은 빈칸을 지정할 수 없습니다.")
@Size(max = 10, message = "공연장 좌석은 10자 이내로 입력해주세요.") String name) {
public record EventHallSeatCreateRequest(
@NotBlank(message = "공연장 좌석은 빈칸을 지정할 수 없습니다.")
@Size(max = 10, message = "공연장 좌석은 10자 이내로 입력해주세요.")
String name) {
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.pgms.apievent.eventHall.dto.request;

import java.util.List;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import java.util.List;

public record EventHallUpdateRequest(@NotBlank(message = "공연장 이름은 빈칸을 지정할 수 없습니다.")
@Size(max = 25, message = "공연장 이름은 10자 이내로 입력해주세요.") String name,
String address,
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests) {
public record EventHallUpdateRequest(
@NotBlank(message = "공연장 이름은 빈칸을 지정할 수 없습니다.")
@Size(max = 25, message = "공연장 이름은 10자 이내로 입력해주세요.")
String name,
String address,
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests) {
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.pgms.apievent.eventHall.dto.response;

import java.util.List;

import com.pgms.coredomain.domain.event.EventHall;
import lombok.Builder;

import java.util.List;
import lombok.Builder;

@Builder
public record EventHallResponse(Long id, String name, String address, List<EventHallSeatResponse> eventHallSeatResponses) {
static public EventHallResponse of(EventHall eventHall, List<EventHallSeatResponse> eventHallSeatResponses){
return EventHallResponse.builder()
.id(eventHall.getId())
.name(eventHall.getName())
.address(eventHall.getAddress())
.eventHallSeatResponses(eventHallSeatResponses)
.build();
}
public record EventHallResponse(
Long id,
String name,
String address,
List<EventHallSeatResponse> eventHallSeatResponses) {

public static EventHallResponse of(EventHall eventHall, List<EventHallSeatResponse> eventHallSeatResponses) {
return EventHallResponse.builder()
.id(eventHall.getId())
.name(eventHall.getName())
.address(eventHall.getAddress())
.eventHallSeatResponses(eventHallSeatResponses)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.pgms.apievent.eventHall.dto.response;

import com.pgms.coredomain.domain.event.EventHallSeat;

import lombok.Builder;

@Builder
public record EventHallSeatResponse(Long id, String name) {
static public EventHallSeatResponse of(EventHallSeat eventHallSeat){
return EventHallSeatResponse.builder()
.id(eventHallSeat.getId())
.name(eventHallSeat.getName())
.build();
}

public static EventHallSeatResponse of(EventHallSeat eventHallSeat) {
return EventHallSeatResponse.builder()
.id(eventHallSeat.getId())
.name(eventHallSeat.getName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,105 +1,111 @@
package com.pgms.apievent.eventHall.service;

import static com.pgms.apievent.exception.EventErrorCode.*;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pgms.apievent.eventHall.dto.request.EventHallCreateRequest;
import com.pgms.apievent.eventHall.dto.request.EventHallUpdateRequest;
import com.pgms.apievent.eventHall.dto.request.EventHallSeatCreateRequest;
import com.pgms.apievent.eventHall.dto.request.EventHallUpdateRequest;
import com.pgms.apievent.eventHall.dto.response.EventHallResponse;
import com.pgms.apievent.eventHall.dto.response.EventHallSeatResponse;
import com.pgms.apievent.exception.EventHallNotFoundException;
import com.pgms.apievent.exception.EventException;
import com.pgms.coredomain.domain.event.EventHall;
import com.pgms.coredomain.domain.event.EventHallEdit;
import com.pgms.coredomain.domain.event.EventHallSeat;
import com.pgms.coredomain.domain.event.repository.EventHallRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import lombok.RequiredArgsConstructor;

@Service
@Transactional
@RequiredArgsConstructor
public class EventHallService {

private final EventHallRepository eventHallRepository;
private final EventHallRepository eventHallRepository;

public EventHallResponse createEventHall(EventHallCreateRequest eventHallCreateRequest) {
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests = eventHallCreateRequest.eventHallSeatCreateRequests();
public EventHallResponse createEventHall(EventHallCreateRequest eventHallCreateRequest) {
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests = eventHallCreateRequest.eventHallSeatCreateRequests();

List<EventHallSeat> eventHallSeats = eventHallSeatCreateRequests.stream()
.map(this::createEventHallSeat)
.toList();
List<EventHallSeat> eventHallSeats = eventHallSeatCreateRequests.stream()
.map(this::createEventHallSeat)
.toList();

EventHall eventHall = EventHall.builder()
.name(eventHallCreateRequest.name())
.address(eventHallCreateRequest.address())
.eventHallSeats(eventHallSeats)
.build();
EventHall eventHall = EventHall.builder()
.name(eventHallCreateRequest.name())
.address(eventHallCreateRequest.address())
.eventHallSeats(eventHallSeats)
.build();

EventHall savedEventHall = eventHallRepository.save(eventHall);
EventHall savedEventHall = eventHallRepository.save(eventHall);

List<EventHallSeatResponse> eventHallSeatResponses = savedEventHall.getEventHallSeats()
.stream()
.map(EventHallSeatResponse::of)
.toList();
List<EventHallSeatResponse> eventHallSeatResponses = savedEventHall.getEventHallSeats()
.stream()
.map(EventHallSeatResponse::of)
.toList();

return EventHallResponse.of(savedEventHall, eventHallSeatResponses);
}
return EventHallResponse.of(savedEventHall, eventHallSeatResponses);
}

public void deleteEventHall(Long id) {
EventHall eventHall = eventHallRepository.findById(id)
.orElseThrow(RuntimeException::new);
public void deleteEventHall(Long id) {
EventHall eventHall = eventHallRepository.findById(id)
.orElseThrow(RuntimeException::new);

eventHallRepository.delete(eventHall);
}
eventHallRepository.delete(eventHall);
}

public EventHallResponse updateEventHall(Long id, EventHallUpdateRequest eventHallUpdateRequest) {
EventHall eventHall = eventHallRepository.findById(id).orElseThrow(EventHallNotFoundException::new);
public EventHallResponse updateEventHall(Long id, EventHallUpdateRequest eventHallUpdateRequest) {
EventHall eventHall = eventHallRepository.findById(id)
.orElseThrow(() -> new EventException(EVENT_HALL_NOT_FOUND));

List<EventHallSeatCreateRequest> eventHallSeatCreateRequests = eventHallUpdateRequest.eventHallSeatCreateRequests();
List<EventHallSeatCreateRequest> eventHallSeatCreateRequests = eventHallUpdateRequest.eventHallSeatCreateRequests();

List<EventHallSeat> eventHallSeats = eventHallSeatCreateRequests.stream()
.map(this::createEventHallSeat)
.toList();
List<EventHallSeat> eventHallSeats = eventHallSeatCreateRequests.stream()
.map(this::createEventHallSeat)
.toList();

EventHallEdit eventHallEdit = EventHallEdit.builder()
.name(eventHallUpdateRequest.name())
.address(eventHallUpdateRequest.address())
.eventHallSeats(eventHallSeats)
.build();
EventHallEdit eventHallEdit = EventHallEdit.builder()
.name(eventHallUpdateRequest.name())
.address(eventHallUpdateRequest.address())
.eventHallSeats(eventHallSeats)
.build();

eventHall.updateEventHall(eventHallEdit);
eventHall.updateEventHall(eventHallEdit);

List<EventHallSeatResponse> eventHallSeatResponses = eventHall.getEventHallSeats()
.stream()
.map(EventHallSeatResponse::of)
.toList();
List<EventHallSeatResponse> eventHallSeatResponses = eventHall.getEventHallSeats()
.stream()
.map(EventHallSeatResponse::of)
.toList();

return EventHallResponse.of(eventHall, eventHallSeatResponses);
}
return EventHallResponse.of(eventHall, eventHallSeatResponses);
}

@Transactional(readOnly = true)
public EventHallResponse getEventHall(Long id) {
EventHall eventHall = eventHallRepository.findById(id).orElseThrow(EventHallNotFoundException::new);
@Transactional(readOnly = true)
public EventHallResponse getEventHall(Long id) {
EventHall eventHall = eventHallRepository.findById(id)
.orElseThrow(() -> new EventException(EVENT_HALL_NOT_FOUND));

// TODO 못 읽어옴
List<EventHallSeatResponse> eventHallSeatResponses = eventHall.getEventHallSeats()
.stream()
.map(EventHallSeatResponse::of)
.toList();
// TODO 못 읽어옴
List<EventHallSeatResponse> eventHallSeatResponses = eventHall.getEventHallSeats()
.stream()
.map(EventHallSeatResponse::of)
.toList();

return EventHallResponse.of(eventHall, eventHallSeatResponses);
}
return EventHallResponse.of(eventHall, eventHallSeatResponses);
}

@Transactional(readOnly = true)
public List<EventHallResponse> getEventHalls() {
List<EventHall> eventHalls = eventHallRepository.findAll();
return eventHalls.stream()
.map(eventHall -> EventHallResponse.of(eventHall, null))
.toList();
}
@Transactional(readOnly = true)
public List<EventHallResponse> getEventHalls() {
List<EventHall> eventHalls = eventHallRepository.findAll();
return eventHalls.stream()
.map(eventHall -> EventHallResponse.of(eventHall, null))
.toList();
}

private EventHallSeat createEventHallSeat(EventHallSeatCreateRequest eventHallSeatCreateRequest) {
return new EventHallSeat(eventHallSeatCreateRequest.name());
}
private EventHallSeat createEventHallSeat(EventHallSeatCreateRequest eventHallSeatCreateRequest) {
return new EventHallSeat(eventHallSeatCreateRequest.name());
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.pgms.apievent.eventSearch.controller;

import com.pgms.apievent.common.dto.response.PageResponseDto;
import com.pgms.apievent.eventSearch.dto.request.EventKeywordSearchRequest;
import com.pgms.apievent.eventSearch.service.EventSearchService;
import com.pgms.coredomain.response.ApiResponse;
import com.pgms.coreinfraes.dto.TopTenSearchResponse;
import lombok.RequiredArgsConstructor;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import com.pgms.apievent.common.dto.response.PageResponseDto;
import com.pgms.apievent.eventSearch.dto.request.EventKeywordSearchRequest;
import com.pgms.apievent.eventSearch.dto.response.RecentTop10KeywordsResponse;
import com.pgms.apievent.eventSearch.service.EventSearchService;
import com.pgms.coredomain.response.ApiResponse;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/v1/events/search")
Expand All @@ -30,7 +32,7 @@ public ResponseEntity<ApiResponse> searchEventsByKeyword(

@GetMapping("/top-ten")
ResponseEntity<ApiResponse> getRecentTop10Keywords() {
List<TopTenSearchResponse> response = eventSearchService.getRecentTop10Keywords();
List<RecentTop10KeywordsResponse> response = eventSearchService.getRecentTop10Keywords();
return ResponseEntity.ok(ApiResponse.ok(response));
}
}
Loading

0 comments on commit ddb42b1

Please sign in to comment.