From caaa9493388895cbde16613c6b955b66ccb24e21 Mon Sep 17 00:00:00 2001 From: JSoi Date: Tue, 2 Aug 2022 19:48:38 +0900 Subject: [PATCH] =?UTF-8?q?#18=20[Refactor]=20=EB=A6=AC=EB=B7=B0=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20Errorcode=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpnp/baechelin/exception/ErrorCode.java | 9 ++- .../review/controller/ReviewController.java | 78 ++++++++++--------- .../review/service/ReviewService.java | 44 +++++------ 3 files changed, 68 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java b/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java index ce75fb0..6816488 100644 --- a/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java +++ b/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java @@ -24,7 +24,14 @@ public enum ErrorCode { NULL_POINTER_EXCEPTION(500, "E-NPE500", "빈 값이 들어올 수 없습니다."), IMAGE_PROCESS_FAIL(500,"E-IPF500","이미지 오류 발생"), KEYWORD_ARE_NEEDED(500, "E-KAE500", "검색어를 입력해주세요."), - INVALID_BARRIER_TAG(500, "E-IBT500","배리어 프리 태그를 확인해주세요"); + INVALID_BARRIER_TAG(500, "E-IBT500","배리어 프리 태그를 확인해주세요."), + NO_STORE_FOUND(500, "E-NSF", "가게가 존재하지 않습니다."), + NO_FOLDER_FOUND(500, "E-NFF", "폴더가 존재하지 않습니다."), + NO_BOOKMARK_FOUND(500, "E-NBF", "북마크가 존재하지 않습니다."), + NO_USER_FOUND(500, "E-NUF", "유저가 존재하지 않습니다."), + NO_REVIEW_FOUND(500, "E-NRF", "리뷰가 존재하지 않습니다."), + + ; private final int status; private final String code; diff --git a/src/main/java/com/mpnp/baechelin/review/controller/ReviewController.java b/src/main/java/com/mpnp/baechelin/review/controller/ReviewController.java index 0a57318..7e0f417 100644 --- a/src/main/java/com/mpnp/baechelin/review/controller/ReviewController.java +++ b/src/main/java/com/mpnp/baechelin/review/controller/ReviewController.java @@ -1,5 +1,8 @@ package com.mpnp.baechelin.review.controller; +import com.mpnp.baechelin.common.SuccessResponse; +import com.mpnp.baechelin.exception.CustomException; +import com.mpnp.baechelin.exception.ErrorCode; import com.mpnp.baechelin.review.domain.Review; import com.mpnp.baechelin.review.dto.PageInfoResponseDto; import com.mpnp.baechelin.review.dto.ReviewMainResponseDto; @@ -34,22 +37,19 @@ public class ReviewController { private final ReviewService reviewService; - /** 리뷰 조회 */ + /** + * 리뷰 조회 + */ @GetMapping("/review/{storeId}") public ResponseEntity getStoreReview(@PathVariable int storeId, @AuthenticationPrincipal User user, @PageableDefault(page = 0, size = 5, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { - - if (user != null) { - PageInfoResponseDto pageInfoResponseDto = reviewService.getReview(storeId, user.getUsername(), pageable); - return new ResponseEntity<>(pageInfoResponseDto, HttpStatus.OK); - } - if (user == null) { PageInfoResponseDto pageInfoResponseDto = reviewService.getReview(storeId, pageable); return new ResponseEntity<>(pageInfoResponseDto, HttpStatus.OK); } - return null; + PageInfoResponseDto pageInfoResponseDto = reviewService.getReview(storeId, user.getUsername(), pageable); + return new ResponseEntity<>(pageInfoResponseDto, HttpStatus.OK); } // @GetMapping("/review/{storeId}") @@ -59,49 +59,56 @@ public ResponseEntity getStoreReview(@PathVariable int stor // return new ResponseEntity<>(reviewList, HttpStatus.OK); // } - /** 리뷰 작성 */ + /** + * 리뷰 작성 + */ @PostMapping("/review") - public ResponseEntity review(@ModelAttribute ReviewRequestDto reviewRequestDto, - @AuthenticationPrincipal User user) throws IOException { - - - if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다.");} + public SuccessResponse review(@ModelAttribute ReviewRequestDto reviewRequestDto, + @AuthenticationPrincipal User user) throws IOException { + if (user == null) { + throw new CustomException(ErrorCode.ACCESS_DENIED); + } reviewService.review(reviewRequestDto, user.getUsername()); - - return new ResponseEntity<>(HttpStatus.OK); + return new SuccessResponse("리뷰 등록 성공"); } - /** 리뷰 수정 */ + /** + * 리뷰 수정 + */ @PatchMapping("/review/{reviewId}") - public ResponseEntity reviewUpdate(@ModelAttribute ReviewRequestDto reviewRequestDto, - @AuthenticationPrincipal User user, - @PathVariable int reviewId) throws IOException { - - if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } + public SuccessResponse reviewUpdate(@ModelAttribute ReviewRequestDto reviewRequestDto, + @AuthenticationPrincipal User user, + @PathVariable int reviewId) { + if (user == null) { + throw new CustomException(ErrorCode.ACCESS_DENIED); + } reviewService.reviewUpdate(reviewRequestDto, user.getUsername(), reviewId); - - return new ResponseEntity<>(HttpStatus.OK); + return new SuccessResponse("리뷰 수정 성공"); } @PostMapping("/review/imageUpload") - public ResponseEntity imageUpload(@AuthenticationPrincipal User user, - @RequestParam MultipartFile imageFile){ - - if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } + public SuccessResponse imageUpload(@AuthenticationPrincipal User user, + @RequestParam MultipartFile imageFile) { + if (user == null) { + throw new CustomException(ErrorCode.ACCESS_DENIED); + } reviewService.imageUpload(imageFile, user.getUsername()); - return new ResponseEntity<>(HttpStatus.OK); + return new SuccessResponse("리뷰 이미지 등록 완료"); } - /** 리뷰 삭제 */ + /** + * 리뷰 삭제 + */ @DeleteMapping("/review/{reviewId}") - public ResponseEntity reviewDelete(@AuthenticationPrincipal User user, - @PathVariable int reviewId) { + public SuccessResponse reviewDelete(@AuthenticationPrincipal User user, + @PathVariable int reviewId) { - if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } + if (user == null) { + throw new CustomException(ErrorCode.ACCESS_DENIED); + } reviewService.reviewDelete(user.getUsername(), reviewId); - - return new ResponseEntity<>(HttpStatus.OK); + return new SuccessResponse("리뷰 이미지 삭제 완료"); } // 반경 넓히기 @@ -111,5 +118,4 @@ public List recentReview(@RequestParam(required = false) @RequestParam int limit) { return reviewService.getRecentReview(lat, lng, limit); } - } diff --git a/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java b/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java index 0fa0db1..ed4d3c0 100644 --- a/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java +++ b/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java @@ -1,5 +1,7 @@ package com.mpnp.baechelin.review.service; +import com.mpnp.baechelin.exception.CustomException; +import com.mpnp.baechelin.exception.ErrorCode; import com.mpnp.baechelin.review.domain.Review; import com.mpnp.baechelin.review.domain.ReviewImage; import com.mpnp.baechelin.review.dto.PageInfoResponseDto; @@ -95,32 +97,28 @@ public void review(ReviewRequestDto reviewRequestDto, String socialId) throws IO public PageInfoResponseDto getReview(long storeId, String socialId, Pageable pageable) { User myUser = userRepository.findBySocialId(socialId); - Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당 가게가 없습니다")); + Store store = storeRepository.findById(storeId).orElseThrow(() -> new CustomException(ErrorCode.NO_STORE_FOUND)); Page reviewList = reviewRepository.findAllByStoreId(store, pageable); List reviewResponseDtoList = new ArrayList<>(); for (Review review : reviewList) { ReviewResponseDto reviewResponseDto = new ReviewResponseDto(review); - Optional user = userRepository.findById(reviewResponseDto.getUserId()); - reviewResponseDto.userInfo(user.get(), myUser); + User user = userRepository.findById(reviewResponseDto.getUserId()).orElseThrow(() -> new CustomException(ErrorCode.NO_USER_FOUND)); + reviewResponseDto.userInfo(user, myUser); reviewResponseDtoList.add(reviewResponseDto); } - - PageInfoResponseDto pageInfoResponseDto = PageInfoResponseDto + return PageInfoResponseDto .builder() .totalElements((int) reviewList.getTotalElements()) .totalPages(reviewList.getTotalPages()) .number(reviewList.getNumber()) .size(reviewList.getSize()) .reviewResponseDtoList(reviewResponseDtoList) - .hasNextPage(reviewList.isFirst() ? false : true) - .hasPreviousPage(!reviewList.isLast() ? false : true) + .hasNextPage(!reviewList.isFirst()) + .hasPreviousPage(reviewList.isLast()) .build(); - - - return pageInfoResponseDto; } public PageInfoResponseDto getReview(long storeId, Pageable pageable) { @@ -132,25 +130,22 @@ public PageInfoResponseDto getReview(long storeId, Pageable pageable) { List reviewResponseDtoList = new ArrayList<>(); for (Review review : reviewList) { ReviewResponseDto reviewResponseDto = new ReviewResponseDto(review); - Optional user = userRepository.findById(reviewResponseDto.getUserId()); - reviewResponseDto.userInfo(user.get()); + User user = userRepository.findById(reviewResponseDto.getUserId()).orElseThrow(()-> new CustomException(ErrorCode.NO_USER_FOUND)); + reviewResponseDto.userInfo(user); reviewResponseDtoList.add(reviewResponseDto); } - PageInfoResponseDto pageInfoResponseDto = PageInfoResponseDto + return PageInfoResponseDto .builder() .totalElements((int) reviewList.getTotalElements()) .totalPages(reviewList.getTotalPages()) .number(reviewList.getNumber()) .size(reviewList.getSize()) .reviewResponseDtoList(reviewResponseDtoList) - .hasNextPage(reviewList.isFirst() ? false : true) - .hasPreviousPage(!reviewList.isLast() ? false : true) + .hasNextPage(reviewList.isFirst()) + .hasPreviousPage(reviewList.isLast()) .build(); - - - return pageInfoResponseDto; } @@ -241,11 +236,9 @@ public void reviewUpdate(ReviewRequestDto reviewRequestDto, String socialId, int */ public void reviewDelete(String socialId, int reviewId) { - User user = userRepository .findBySocialId(socialId); if (user == null) { new IllegalArgumentException("해당하는 소셜아이디를 찾을 수 없습니다."); } // 유저 유무 확인 예외처리 - Review review = reviewRepository.findById(reviewId).orElseThrow(() -> new IllegalArgumentException("해당하는 리뷰가 이미 삭제 되었습니다.")); // 리뷰 유무 확인 예외처리; - Optional store = storeRepository.findById(Long.valueOf(review.getStoreId().getId())); - if(store.isEmpty()){ throw new IllegalArgumentException("해당하는 업장이 없습니다.");} - + User user = userRepository .findBySocialId(socialId); if (user == null) { throw new CustomException(ErrorCode.NO_USER_FOUND); } // 유저 유무 확인 예외처리 + Review review = reviewRepository.findById(reviewId).orElseThrow(() -> new CustomException(ErrorCode.NO_REVIEW_FOUND)); // 리뷰 유무 확인 예외처리; + Store store = storeRepository.findById(review.getStoreId().getId()).orElseThrow(()-> new CustomException(ErrorCode.NO_STORE_FOUND)); List imageList = review.getReviewImageList(); @@ -258,9 +251,8 @@ public void reviewDelete(String socialId, int reviewId) { awsS3Manager.deleteFile(reviewImage.getReviewImageUrl().substring(reviewImage.getReviewImageUrl().indexOf("com/") + 4)); } } - - store.get().removeReview(review); - storeRepository.save(store.get().updatePointAvg()); // 별점 평점 구하는 코드 + store.removeReview(review); + storeRepository.save(store.updatePointAvg()); // 별점 평점 구하는 코드 }