diff --git a/backend/src/main/java/com/project/capstone/member/controller/MemberController.java b/backend/src/main/java/com/project/capstone/member/controller/MemberController.java index d12e3f8b57..9864c977ee 100644 --- a/backend/src/main/java/com/project/capstone/member/controller/MemberController.java +++ b/backend/src/main/java/com/project/capstone/member/controller/MemberController.java @@ -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 requests, @RequestParam String groupName) { + memberService.addMyBooks(details.getUserId(), requests, groupName); + return ResponseEntity.ok().body("추가 완료"); + } } diff --git a/backend/src/main/java/com/project/capstone/member/service/MemberService.java b/backend/src/main/java/com/project/capstone/member/service/MemberService.java index 100b7e6747..50d5bcc96f 100644 --- a/backend/src/main/java/com/project/capstone/member/service/MemberService.java +++ b/backend/src/main/java/com/project/capstone/member/service/MemberService.java @@ -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 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); + } + } }