Skip to content
This repository has been archived by the owner on Oct 22, 2020. It is now read-only.

Commit

Permalink
[#164] feat: Enroll 과 Cancel 을 합친다.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevLuffy committed May 23, 2020
1 parent cd0e543 commit bd6868e
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ public Page<WishBookInfoResponse> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,15 @@ public Page<WishBookInfoResponse> 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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -30,15 +33,27 @@ public Page<WishBook> 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());
Expand All @@ -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());
}
}
}

0 comments on commit bd6868e

Please sign in to comment.