Skip to content

Commit

Permalink
Merge pull request #74 from kookmin-sw/BE_Feature/#32-member
Browse files Browse the repository at this point in the history
#32 feat: 책 여러권 추가하기
  • Loading branch information
wjdwlghks authored May 13, 2024
2 parents c9cd83f + c66e75d commit 5244426
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public ResponseEntity<?> addMyBook(@AuthenticationPrincipal PrincipalDetails det
memberService.addMyBook(details.getUserId(), request, groupName);
return ResponseEntity.ok().body("추가 완료");
}

@PostMapping("/my-book/adds")
public ResponseEntity<?> addMyBooks(@AuthenticationPrincipal PrincipalDetails details,
@RequestBody List<AddBookRequest> requests, @RequestParam String groupName) {
memberService.addMyBooks(details.getUserId(), requests, groupName);
return ResponseEntity.ok().body("추가 완료");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,21 @@ public void addMyBook(String userId, AddBookRequest request, String groupName) {
member.getMyBooks().add(saved);
book.getMembersAddThisBook().add(saved);
}

public void addMyBooks(String userId, List<AddBookRequest> requests, String groupName) {
Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow(
() -> new MemberException(MEMBER_NOT_FOUND)
);
for (AddBookRequest request : requests) {
Book book = bookRepository.findBookByIsbn(request.isbn()).orElseGet(
() -> bookRepository.save(new Book(request))
);
if (myBookRepository.findMyBookByMemberAndBook(member, book).isPresent()) {
throw new MyBookException(ALREADY_EXIST_MYBOOK);
}
MyBook saved = myBookRepository.save(new MyBook(null, groupName, member, book));
member.getMyBooks().add(saved);
book.getMembersAddThisBook().add(saved);
}
}
}

0 comments on commit 5244426

Please sign in to comment.