From 97e6f69f97b258ed65bf98764db4bff5cc0dec71 Mon Sep 17 00:00:00 2001 From: JSoi Date: Tue, 2 Aug 2022 19:48:02 +0900 Subject: [PATCH] =?UTF-8?q?#18=20[Refactor]=20=EB=B6=81=EB=A7=88=ED=81=AC?= =?UTF-8?q?=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BookmarkController.java | 34 +++++++++++-------- .../bookmark/service/BookmarkService.java | 15 ++++---- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java b/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java index 523344b..347bade 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java @@ -3,6 +3,9 @@ import com.mpnp.baechelin.bookmark.dto.BookmarkInfoDto; import com.mpnp.baechelin.bookmark.dto.BookmarkRequestDto; import com.mpnp.baechelin.bookmark.service.BookmarkService; +import com.mpnp.baechelin.common.SuccessResponse; +import com.mpnp.baechelin.exception.CustomException; +import com.mpnp.baechelin.exception.ErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -20,29 +23,32 @@ public class BookmarkController { private final BookmarkService bookmarkService; - /** 북마크 생성 폴더 담기 */ + /** + * 북마크 생성 폴더 담기 + */ @PostMapping("/bookmark") - public ResponseEntity bookmark(@RequestBody BookmarkRequestDto bookmarkRequestDto, - @AuthenticationPrincipal User user){ - - if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } + public SuccessResponse bookmark(@RequestBody BookmarkRequestDto bookmarkRequestDto, + @AuthenticationPrincipal User user) { + if (user == null) { + throw new CustomException(ErrorCode.NO_USER_FOUND); + } bookmarkService.bookmark(bookmarkRequestDto, user.getUsername()); - - return new ResponseEntity<>(HttpStatus.OK); + return new SuccessResponse("북마크를 폴더에 저장 완료"); } @DeleteMapping("/bookmark/{bookmarkId}") - public ResponseEntity bookmarkDelete(@PathVariable int bookmarkId, - @AuthenticationPrincipal User user){ - if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } + public SuccessResponse bookmarkDelete(@PathVariable int bookmarkId, + @AuthenticationPrincipal User user) { + if (user == null) { + throw new CustomException(ErrorCode.NO_USER_FOUND); + } bookmarkService.bookmarkDelete(bookmarkId, user.getUsername()); - - return new ResponseEntity<>(HttpStatus.OK); + return new SuccessResponse("북마크를 삭제 완료"); } @GetMapping("/bookmarkTop") - public ResponseEntity bookmarkTop(@AuthenticationPrincipal User user, - @PageableDefault(page = 0, size = 5, sort = "id", direction = Sort.Direction.DESC) Pageable pageable){ + public ResponseEntity> bookmarkTop(@AuthenticationPrincipal User user, + @PageableDefault(page = 0, size = 5, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { List bookmarkList = bookmarkService.bookmarkTop(user.getUsername(), pageable); return new ResponseEntity<>(bookmarkList, HttpStatus.OK); diff --git a/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java b/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java index 73dcc1f..041fef6 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java @@ -6,6 +6,8 @@ import com.mpnp.baechelin.bookmark.dto.BookmarkRequestDto; import com.mpnp.baechelin.bookmark.repository.BookmarkRepository; import com.mpnp.baechelin.bookmark.repository.FolderRepository; +import com.mpnp.baechelin.exception.CustomException; +import com.mpnp.baechelin.exception.ErrorCode; import com.mpnp.baechelin.store.domain.Store; import com.mpnp.baechelin.store.repository.StoreImgRepository; import com.mpnp.baechelin.store.repository.StoreRepository; @@ -33,10 +35,10 @@ public class BookmarkService { @Transactional public void bookmark(BookmarkRequestDto bookmarkRequestDto, String socialId) { - - Folder folder = folderRepository.findById(bookmarkRequestDto.getFolderId()).orElseThrow(()-> new IllegalArgumentException("폴더가 존재하지 않습니다")); - Store store = storeRepository.findById((long) bookmarkRequestDto.getStoreId()).orElseThrow(()-> new IllegalArgumentException("가게가 존재하지 않습니다")); - User user = userRepository.findBySocialId(socialId); if(user == null) { throw new IllegalArgumentException("해당하는 유저가 없습니다."); } + // 북마크 폴더 생성하는 Flow를 따르므로 bookmarkRequestDto의 folderId는 존재 + Folder folder = folderRepository.findById(bookmarkRequestDto.getFolderId()).orElseThrow(()-> new CustomException(ErrorCode.NO_FOLDER_FOUND)); + Store store = storeRepository.findById((long) bookmarkRequestDto.getStoreId()).orElseThrow(()-> new CustomException(ErrorCode.NO_BOOKMARK_FOUND)); + User user = userRepository.findBySocialId(socialId); if(user == null) {throw new CustomException(ErrorCode.NO_USER_FOUND); } Bookmark bookmark = Bookmark .builder() @@ -52,8 +54,8 @@ public void bookmark(BookmarkRequestDto bookmarkRequestDto, String socialId) { } @Transactional public void bookmarkDelete(int bookmarkId, String socialId) { - User user = userRepository.findBySocialId(socialId); if(user == null) { throw new IllegalArgumentException("해당하는 유저가 없습니다."); } - Bookmark bookmark = bookmarkRepository.findById(bookmarkId).orElseThrow(() -> new IllegalArgumentException("해당하는 북마크는 이미 삭제 되었습니다")); + User user = userRepository.findBySocialId(socialId); if(user == null) { throw new CustomException(ErrorCode.NO_USER_FOUND); } + Bookmark bookmark = bookmarkRepository.findById(bookmarkId).orElseThrow(() -> new CustomException(ErrorCode.NO_BOOKMARK_FOUND)); Store store = bookmark.getStoreId(); store.removeBookmark(bookmark); bookmarkRepository.deleteById(bookmarkId); @@ -66,7 +68,6 @@ public List bookmarkTop(String socialId, Pageable pageable) { User user = userRepository.findBySocialId(socialId); Page bookmarkPage = bookmarkRepository.findAllByUserId(user, pageable); - List bookmarkList = new ArrayList<>(); for(Bookmark bookmark: bookmarkPage){ BookmarkInfoDto bookmarkInfoDto = new BookmarkInfoDto(bookmark);