From bd6868e6c43a6822723fc91f41b2b66c0511b475 Mon Sep 17 00:00:00 2001 From: thedevluffy Date: Sat, 23 May 2020 16:40:10 +0900 Subject: [PATCH] =?UTF-8?q?[#164]=20feat:=20Enroll=20=EA=B3=BC=20Cancel=20?= =?UTF-8?q?=EC=9D=84=20=ED=95=A9=EC=B9=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tecobrary/TecobraryAdminApplication.java | 2 ++ .../web/wishbook/WishBookAdminController.java | 11 ++---- .../WishBookAdminControllerAdvice.java | 34 +++++++++++++++++++ .../wishbook/dto/WishBookEnrollRequest.java | 16 --------- ...equest.java => WishBookHandleRequest.java} | 7 ++-- .../WishBookAlreadyHandledException.java | 14 ++++++++ .../exception/WishBookNotFoundException.java | 14 ++++++++ .../WishBookStatusChangeFailedException.java | 14 ++++++++ .../wishbook/service/WishBookAdminFacade.java | 16 ++------- .../service/WishBookAdminService.java | 29 ++++++++++++---- 10 files changed, 111 insertions(+), 46 deletions(-) create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/WishBookAdminControllerAdvice.java delete mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookEnrollRequest.java rename tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/{WishBookCancelRequest.java => WishBookHandleRequest.java} (51%) create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookAlreadyHandledException.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookNotFoundException.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookStatusChangeFailedException.java diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/TecobraryAdminApplication.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/TecobraryAdminApplication.java index 023bfae..815f14b 100644 --- a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/TecobraryAdminApplication.java +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/TecobraryAdminApplication.java @@ -3,9 +3,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication +@EnableJpaAuditing @EntityScan("com.woowacourse.tecobrary.domain.*") @EnableJpaRepositories("com.woowacourse.tecobrary.domain.*") public class TecobraryAdminApplication { diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/WishBookAdminController.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/WishBookAdminController.java index 502f7b0..347b97a 100644 --- a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/WishBookAdminController.java +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/WishBookAdminController.java @@ -24,13 +24,8 @@ public Page getWishBooks(@PageableDefault Pageable pageabl } @PostMapping - public WishBookHandleResponse enrollWishBook(@RequestHeader("Send-Tecorvis-Message") boolean sendMessage, - @RequestBody WishBookEnrollRequest request) { - return wishBookAdminFacade.enrollWishBook(sendMessage, request); - } - - @PutMapping - public WishBookHandleResponse cancelWishBook(@RequestBody WishBookCancelRequest request) { - return wishBookAdminFacade.cancelWishBook(request); + public WishBookHandleResponse handleWishBook(@RequestHeader("Send-Tecorvis-Message") boolean sendMessage, + @RequestBody WishBookHandleRequest request) { + return wishBookAdminFacade.handleWishBook(sendMessage, request); } } diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/WishBookAdminControllerAdvice.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/WishBookAdminControllerAdvice.java new file mode 100644 index 0000000..af0b555 --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/WishBookAdminControllerAdvice.java @@ -0,0 +1,34 @@ +package com.woowacourse.tecobrary.admin.web.wishbook; + +import com.woowacourse.tecobrary.admin.web.wishbook.exception.WishBookAlreadyHandledException; +import com.woowacourse.tecobrary.admin.web.wishbook.exception.WishBookNotFoundException; +import com.woowacourse.tecobrary.admin.web.wishbook.exception.WishBookStatusChangeFailedException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import javax.servlet.http.HttpServletRequest; + +@RestControllerAdvice +@Slf4j +public class WishBookAdminControllerAdvice { + + @ExceptionHandler(WishBookNotFoundException.class) + public ResponseEntity handleError(HttpServletRequest request, WishBookNotFoundException e) { + log.info("[WishBookAdminControllerAdvice] exception request={}, occurred={}", request.toString(), e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } + + @ExceptionHandler(WishBookAlreadyHandledException.class) + public ResponseEntity handleError(HttpServletRequest request, WishBookAlreadyHandledException e) { + log.info("[WishBookAdminControllerAdvice] exception request={}, occurred={}", request.toString(), e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } + + @ExceptionHandler(WishBookStatusChangeFailedException.class) + public ResponseEntity handleError(HttpServletRequest request, WishBookStatusChangeFailedException e) { + log.info("[WishBookAdminControllerAdvice] exception request={}, occurred={}", request.toString(), e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookEnrollRequest.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookEnrollRequest.java deleted file mode 100644 index 16b52fd..0000000 --- a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookEnrollRequest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.woowacourse.tecobrary.admin.web.wishbook.dto; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -@Getter -public class WishBookEnrollRequest { - - private Long id; - - public WishBookEnrollRequest(Long id) { - this.id = id; - } -} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookCancelRequest.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookHandleRequest.java similarity index 51% rename from tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookCancelRequest.java rename to tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookHandleRequest.java index 6eb60f8..623e16a 100644 --- a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookCancelRequest.java +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/dto/WishBookHandleRequest.java @@ -1,18 +1,21 @@ package com.woowacourse.tecobrary.admin.web.wishbook.dto; +import com.woowacourse.tecobrary.domain.wishbook.entity.WishBookStatus; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @NoArgsConstructor(access = AccessLevel.PRIVATE) @Getter -public class WishBookCancelRequest { +public class WishBookHandleRequest { private Long id; + private WishBookStatus wishBookStatus; private String reason; - public WishBookCancelRequest(Long id, String reason) { + public WishBookHandleRequest(Long id, WishBookStatus wishBookStatus, String reason) { this.id = id; + this.wishBookStatus = wishBookStatus; this.reason = reason; } } diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookAlreadyHandledException.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookAlreadyHandledException.java new file mode 100644 index 0000000..27a1a60 --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookAlreadyHandledException.java @@ -0,0 +1,14 @@ +package com.woowacourse.tecobrary.admin.web.wishbook.exception; + +import lombok.Getter; + +@Getter +public class WishBookAlreadyHandledException extends RuntimeException { + + private final String title; + + public WishBookAlreadyHandledException(String title) { + super("이미 처리된 희망도서 입니다."); + this.title = title; + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookNotFoundException.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookNotFoundException.java new file mode 100644 index 0000000..c732db9 --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookNotFoundException.java @@ -0,0 +1,14 @@ +package com.woowacourse.tecobrary.admin.web.wishbook.exception; + +import lombok.Getter; + +@Getter +public class WishBookNotFoundException extends RuntimeException { + + private final Long id; + + public WishBookNotFoundException(Long id) { + super("희망도서를 찾을 수 없습니다."); + this.id = id; + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookStatusChangeFailedException.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookStatusChangeFailedException.java new file mode 100644 index 0000000..fb2a53d --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/exception/WishBookStatusChangeFailedException.java @@ -0,0 +1,14 @@ +package com.woowacourse.tecobrary.admin.web.wishbook.exception; + +import lombok.Getter; + +@Getter +public class WishBookStatusChangeFailedException extends RuntimeException { + + private final Long id; + + public WishBookStatusChangeFailedException(Long id) { + super("지원하지 않는 상태입니다."); + this.id = id; + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/service/WishBookAdminFacade.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/service/WishBookAdminFacade.java index 9ffe796..c18529d 100644 --- a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/service/WishBookAdminFacade.java +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/service/WishBookAdminFacade.java @@ -34,25 +34,15 @@ public Page getWishBooks(Pageable pageable, WishBookSearch return new PageImpl<>(dtos, pageable, wishbooks.getTotalElements()); } - public WishBookHandleResponse enrollWishBook(boolean sendMessage, WishBookEnrollRequest request) { - WishBook wishBook = wishBookAdminService.enrollWishBook(request); + public WishBookHandleResponse handleWishBook(boolean sendMessage, WishBookHandleRequest request) { + WishBook wishBook = wishBookAdminService.handleWishBook(request); sendTecorvisMessage(sendMessage, wishBook); return WishBookHandleResponse.builder() .id(wishBook.getId()) .status(wishBook.getStatus()) - .message("등록 성공") - .build(); - } - - public WishBookHandleResponse cancelWishBook(WishBookCancelRequest request) { - WishBook wishBook = wishBookAdminService.cancelWishBook(request); - - return WishBookHandleResponse.builder() - .id(wishBook.getId()) - .status(wishBook.getStatus()) - .message("취소 성공") + .message("처리 성공") .build(); } diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/service/WishBookAdminService.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/service/WishBookAdminService.java index 2bf97f1..cff3a92 100644 --- a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/service/WishBookAdminService.java +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/wishbook/service/WishBookAdminService.java @@ -1,11 +1,14 @@ package com.woowacourse.tecobrary.admin.web.wishbook.service; -import com.woowacourse.tecobrary.admin.web.wishbook.dto.WishBookCancelRequest; -import com.woowacourse.tecobrary.admin.web.wishbook.dto.WishBookEnrollRequest; +import com.woowacourse.tecobrary.admin.web.wishbook.dto.WishBookHandleRequest; import com.woowacourse.tecobrary.admin.web.wishbook.dto.WishBookSearchRequest; +import com.woowacourse.tecobrary.admin.web.wishbook.exception.WishBookAlreadyHandledException; +import com.woowacourse.tecobrary.admin.web.wishbook.exception.WishBookNotFoundException; +import com.woowacourse.tecobrary.admin.web.wishbook.exception.WishBookStatusChangeFailedException; import com.woowacourse.tecobrary.admin.web.wishbook.repository.WishBookAdminRepository; import com.woowacourse.tecobrary.admin.web.wishbook.repository.WishBookSearchClause; import com.woowacourse.tecobrary.domain.wishbook.entity.WishBook; +import com.woowacourse.tecobrary.domain.wishbook.entity.WishBookStatus; import com.woowacourse.tecobrary.domain.wishbook.repository.WishBookRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,15 +33,27 @@ public Page getWishBooks(Pageable pageable, WishBookSearchRequest requ return wishBookAdminRepository.findAllByCondition(pageable, searchClause); } - public WishBook enrollWishBook(WishBookEnrollRequest request) { + public WishBook handleWishBook(WishBookHandleRequest request) { + if (WishBookStatus.ENROLLED == request.getWishBookStatus()) { + return enrollWishBook(request); + } + + if (WishBookStatus.CANCELED == request.getWishBookStatus()) { + return cancelWishBook(request); + } + + throw new WishBookStatusChangeFailedException(request.getId()); + } + + private WishBook enrollWishBook(WishBookHandleRequest request) { WishBook wishBook = getWishBook(request.getId()); checkHandled(wishBook); - wishBook.enrollWishBook(); + wishBook.enrollWishBook(request.getReason()); return wishBook; } - public WishBook cancelWishBook(WishBookCancelRequest request) { + private WishBook cancelWishBook(WishBookHandleRequest request) { WishBook wishBook = getWishBook(request.getId()); checkHandled(wishBook); wishBook.cancelWishBook(request.getReason()); @@ -48,12 +63,12 @@ public WishBook cancelWishBook(WishBookCancelRequest request) { private WishBook getWishBook(Long id) { return wishBookRepository.findById(id) - .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 희망도서 입니다.")); + .orElseThrow(() -> new WishBookNotFoundException(id)); } private void checkHandled(WishBook wishBook) { if (wishBook.isHandled()) { - throw new IllegalArgumentException("이미 처리된 희망도서 입니다."); + throw new WishBookAlreadyHandledException(wishBook.getTitle()); } } }