Skip to content

Commit

Permalink
#18 [Refactor] 북마크 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Aug 2, 2022
1 parent a81dfef commit 97e6f69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<List<BookmarkInfoDto>> bookmarkTop(@AuthenticationPrincipal User user,
@PageableDefault(page = 0, size = 5, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {

List<BookmarkInfoDto> bookmarkList = bookmarkService.bookmarkTop(user.getUsername(), pageable);
return new ResponseEntity<>(bookmarkList, HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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);
Expand All @@ -66,7 +68,6 @@ public List<BookmarkInfoDto> bookmarkTop(String socialId, Pageable pageable) {
User user = userRepository.findBySocialId(socialId);
Page<Bookmark> bookmarkPage = bookmarkRepository.findAllByUserId(user, pageable);


List<BookmarkInfoDto> bookmarkList = new ArrayList<>();
for(Bookmark bookmark: bookmarkPage){
BookmarkInfoDto bookmarkInfoDto = new BookmarkInfoDto(bookmark);
Expand Down

0 comments on commit 97e6f69

Please sign in to comment.