From d13d86cd91e10bbd75061ab62f539667d7fdeee5 Mon Sep 17 00:00:00 2001 From: JSoi Date: Tue, 2 Aug 2022 19:48:20 +0900 Subject: [PATCH] =?UTF-8?q?#18=20[Refactor]=20=ED=8F=B4=EB=8D=94=20?= =?UTF-8?q?=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 --- .../bookmark/controller/FolderController.java | 35 ++++++-------- .../bookmark/dto/FolderRequestDto.java | 2 +- .../bookmark/service/FolderService.java | 47 ++++++++++--------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java b/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java index 773b2bf..a0d49b3 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java @@ -4,6 +4,9 @@ import com.mpnp.baechelin.bookmark.dto.FolderRequestDto; import com.mpnp.baechelin.bookmark.dto.FolderResponseDto; import com.mpnp.baechelin.bookmark.service.FolderService; +import com.mpnp.baechelin.common.SuccessResponse; +import com.mpnp.baechelin.exception.CustomException; +import com.mpnp.baechelin.exception.ErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -19,45 +22,37 @@ public class FolderController { private final FolderService folderService; - /** 폴더 신규 생성 */ @PostMapping("/folder") - public ResponseEntity folder (@RequestBody FolderRequestDto folderRequestDto, - @AuthenticationPrincipal User user){ - - if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } + public SuccessResponse folder (@RequestBody FolderRequestDto folderRequestDto, + @AuthenticationPrincipal User user){ + if(user==null){ + throw new CustomException(ErrorCode.NO_USER_FOUND); + } folderService.folder(folderRequestDto, user.getUsername()); - return new ResponseEntity<>(HttpStatus.OK); + return new SuccessResponse("폴더 생성 완료"); } - - /** 폴더 삭제 -> 삭제 시 안에 담긴 모든 북마크가 삭제됨 */ @DeleteMapping("/folder/{folderId}") - public ResponseEntity folderDelete (@PathVariable int folderId, + public SuccessResponse folderDelete (@PathVariable int folderId, @AuthenticationPrincipal User user) { - if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } + if(user==null){ throw new CustomException(ErrorCode.NO_USER_FOUND); } folderService.folderDelete(folderId); - return new ResponseEntity<>(HttpStatus.OK); + return new SuccessResponse("폴더 및 북마크 삭제 완료"); } - - /** 폴더 명 변경 */ @PutMapping("/folderUpdate/{folderId}") - public ResponseEntity folderUpdate (@PathVariable int folderId, + public SuccessResponse folderUpdate (@PathVariable int folderId, @RequestParam String newFolderName, @AuthenticationPrincipal User user){ - if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } - + if(user==null){ throw new CustomException(ErrorCode.NO_USER_FOUND); } folderService.folderUpdate(folderId, newFolderName); - - return new ResponseEntity<>(HttpStatus.OK); + return new SuccessResponse("폴더명 변경 완료"); } - - /** 폴더 리스트 */ @GetMapping("/folderList") public List folderList (@AuthenticationPrincipal User user){ diff --git a/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderRequestDto.java b/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderRequestDto.java index 74f6fea..9cb7ffc 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderRequestDto.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderRequestDto.java @@ -12,5 +12,5 @@ @Builder public class FolderRequestDto { - private String folderName = "미분류"; + private String folderName; } diff --git a/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java b/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java index 268f615..121e26d 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java @@ -5,6 +5,8 @@ import com.mpnp.baechelin.bookmark.dto.FolderResponseDto; import com.mpnp.baechelin.bookmark.repository.FolderRepository; +import com.mpnp.baechelin.exception.CustomException; +import com.mpnp.baechelin.exception.ErrorCode; import com.mpnp.baechelin.user.domain.User; import com.mpnp.baechelin.user.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -22,50 +24,53 @@ public class FolderService { private final FolderRepository folderRepository; private final UserRepository userRepository; - /** 폴더 생성 */ + /** + * 폴더 생성 + */ @Transactional public void folder(FolderRequestDto folderRequestDto, String socialId) { - + String folderName = folderRequestDto.getFolderName(); + if (folderName == null || folderName.equals("")) { + throw new CustomException(ErrorCode.NULL_POINTER_EXCEPTION); + } User user = userRepository.findBySocialId(socialId); Folder folder = Folder.builder() .folderName(folderRequestDto.getFolderName()) .userId(user) .build(); folderRepository.save(folder); - } - /** 폴더 삭제 */ + /** + * 폴더 삭제 + */ public void folderDelete(int folderId) { folderRepository.deleteById(folderId); } - /** 폴더 수정 */ + /** + * 폴더 수정 + */ public void folderUpdate(int folderId, String newFolderName) { - - - Optional folder = folderRepository.findById(folderId); - folder.get().setFolderName(newFolderName); - - - folderRepository.save(folder.get()); + Folder folder = folderRepository.findById(folderId).orElseThrow(() -> new CustomException(ErrorCode.NO_FOLDER_FOUND)); + folder.setFolderName(newFolderName); + folderRepository.save(folder); } - /** 폴더 조회 */ + /** + * 폴더 조회 + */ @Transactional(readOnly = true) public List folderList(String socialId) { - - - User user = userRepository.findBySocialId(socialId); if(user == null){ throw new IllegalArgumentException("해당하는 유저를 찾을 수 없습니다."); } + User user = userRepository.findBySocialId(socialId); + if (user == null) { + throw new CustomException(ErrorCode.NO_USER_FOUND); + } List folderResponseDtoList = new ArrayList<>(); - - - for(Folder obj : user.getFolderList()){ + for (Folder obj : user.getFolderList()) { folderResponseDtoList.add(FolderResponseDto.FolderDtoRes(obj)); } - - return folderResponseDtoList; } }