From aa6ed915ad937407f9069d8891751e76d8742e44 Mon Sep 17 00:00:00 2001 From: ji hwan Date: Fri, 26 Apr 2024 20:31:21 +0900 Subject: [PATCH] =?UTF-8?q?#39=20feat:=20db=EC=97=90=20=EC=97=86=EB=8A=94?= =?UTF-8?q?=20=EC=B1=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AddBookRequest.java} | 28 +++++++++---------- .../book/controller/BookController.java | 21 ++++++++++++++ .../project/capstone/book/domain/Book.java | 5 ++-- .../book/exception/BookExceptionType.java | 4 ++- .../capstone/book/service/BookService.java | 24 ++++++++++++++++ .../member/controller/MemberController.java | 4 +-- .../member/service/MemberService.java | 9 ++---- 7 files changed, 68 insertions(+), 27 deletions(-) rename backend/src/main/java/com/project/capstone/{member/controller/dto/AddMyBookRequest.java => book/controller/AddBookRequest.java} (60%) create mode 100644 backend/src/main/java/com/project/capstone/book/controller/BookController.java create mode 100644 backend/src/main/java/com/project/capstone/book/service/BookService.java diff --git a/backend/src/main/java/com/project/capstone/member/controller/dto/AddMyBookRequest.java b/backend/src/main/java/com/project/capstone/book/controller/AddBookRequest.java similarity index 60% rename from backend/src/main/java/com/project/capstone/member/controller/dto/AddMyBookRequest.java rename to backend/src/main/java/com/project/capstone/book/controller/AddBookRequest.java index 2856a55f4e..3c7483dc50 100644 --- a/backend/src/main/java/com/project/capstone/member/controller/dto/AddMyBookRequest.java +++ b/backend/src/main/java/com/project/capstone/book/controller/AddBookRequest.java @@ -1,15 +1,13 @@ -package com.project.capstone.member.controller.dto; - -import java.time.LocalDateTime; - -public record AddMyBookRequest ( - String isbn, - String title, - String category1d, - String category2d, - String category3d, - String author, - String publisher, - String publishDate -) { -} +package com.project.capstone.book.controller; + +public record AddBookRequest( + String isbn, + String title, + String category1d, + String category2d, + String category3d, + String author, + String publisher, + String publishDate +) { +} diff --git a/backend/src/main/java/com/project/capstone/book/controller/BookController.java b/backend/src/main/java/com/project/capstone/book/controller/BookController.java new file mode 100644 index 0000000000..f0ce60f90f --- /dev/null +++ b/backend/src/main/java/com/project/capstone/book/controller/BookController.java @@ -0,0 +1,21 @@ +package com.project.capstone.book.controller; + +import com.project.capstone.book.service.BookService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/book") +@RequiredArgsConstructor +public class BookController { + + private final BookService bookService; + + // 책 추가하기 + @PostMapping("/add") + public ResponseEntity addBook(@RequestBody AddBookRequest request) { + bookService.addBook(request); + return ResponseEntity.ok().body("도서 추가 완료"); + } +} diff --git a/backend/src/main/java/com/project/capstone/book/domain/Book.java b/backend/src/main/java/com/project/capstone/book/domain/Book.java index ed1db8f5c2..7667c75f75 100644 --- a/backend/src/main/java/com/project/capstone/book/domain/Book.java +++ b/backend/src/main/java/com/project/capstone/book/domain/Book.java @@ -1,9 +1,9 @@ package com.project.capstone.book.domain; import com.fasterxml.jackson.annotation.JsonManagedReference; +import com.project.capstone.book.controller.AddBookRequest; import com.project.capstone.club.domain.Club; import com.project.capstone.content.domain.Content; -import com.project.capstone.member.controller.dto.AddMyBookRequest; import com.project.capstone.mybook.domain.MyBook; import com.project.capstone.quiz.domain.Quiz; import jakarta.persistence.*; @@ -53,8 +53,9 @@ public class Book { @OneToMany(mappedBy = "book") private List membersAddThisBook = new ArrayList<>(); - public Book(AddMyBookRequest request) { + public Book(AddBookRequest request) { this(null, request.isbn(), request.title(), request.category1d(), request.category2d(), request.category3d(), request.author(), request.publisher(), request.publishDate(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); } + } diff --git a/backend/src/main/java/com/project/capstone/book/exception/BookExceptionType.java b/backend/src/main/java/com/project/capstone/book/exception/BookExceptionType.java index 545afef3a0..42ebd065ec 100644 --- a/backend/src/main/java/com/project/capstone/book/exception/BookExceptionType.java +++ b/backend/src/main/java/com/project/capstone/book/exception/BookExceptionType.java @@ -4,12 +4,14 @@ import lombok.AllArgsConstructor; import org.springframework.http.HttpStatus; +import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.NOT_FOUND; @AllArgsConstructor public enum BookExceptionType implements ExceptionType { - BOOK_NOT_FOUND(NOT_FOUND, 801, "해당 책을 찾을 수 없습니다.") + BOOK_NOT_FOUND(NOT_FOUND, 801, "해당 책을 찾을 수 없습니다."), + ALREADY_EXIST_BOOK(BAD_REQUEST, 802, "이미 해당 책이 존재합니다.") ; private final HttpStatus status; diff --git a/backend/src/main/java/com/project/capstone/book/service/BookService.java b/backend/src/main/java/com/project/capstone/book/service/BookService.java new file mode 100644 index 0000000000..53c42ed3a2 --- /dev/null +++ b/backend/src/main/java/com/project/capstone/book/service/BookService.java @@ -0,0 +1,24 @@ +package com.project.capstone.book.service; + +import com.project.capstone.book.controller.AddBookRequest; +import com.project.capstone.book.domain.Book; +import com.project.capstone.book.domain.BookRepository; +import com.project.capstone.book.exception.BookException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import static com.project.capstone.book.exception.BookExceptionType.ALREADY_EXIST_BOOK; + +@Service +@RequiredArgsConstructor +@Slf4j +public class BookService { + private final BookRepository bookRepository; + public void addBook(AddBookRequest request) { + if (bookRepository.findBookByIsbn(request.isbn()).isPresent()) { + throw new BookException(ALREADY_EXIST_BOOK); + } + bookRepository.save(new Book(request)); + } +} 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 4098019e27..f9e7213f5f 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 @@ -1,7 +1,7 @@ package com.project.capstone.member.controller; import com.project.capstone.auth.domain.PrincipalDetails; -import com.project.capstone.member.controller.dto.AddMyBookRequest; +import com.project.capstone.book.controller.AddBookRequest; import com.project.capstone.member.controller.dto.MemberResponse; import com.project.capstone.member.controller.dto.MyBookResponse; import com.project.capstone.member.service.MemberService; @@ -35,7 +35,7 @@ public ResponseEntity> getMyBook(@AuthenticationPrincipal P // 나만의 서재 추가 @PostMapping("/my-book/add") - public ResponseEntity addMyBook(@AuthenticationPrincipal PrincipalDetails details, @RequestBody AddMyBookRequest request) { + public ResponseEntity addMyBook(@AuthenticationPrincipal PrincipalDetails details, @RequestBody AddBookRequest request) { memberService.addMyBook(details.getUserId(), request); 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 8207638e45..a704f7d791 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 @@ -1,10 +1,8 @@ package com.project.capstone.member.service; +import com.project.capstone.book.controller.AddBookRequest; import com.project.capstone.book.domain.Book; import com.project.capstone.book.domain.BookRepository; -import com.project.capstone.book.exception.BookException; -import com.project.capstone.book.exception.BookExceptionType; -import com.project.capstone.member.controller.dto.AddMyBookRequest; import com.project.capstone.member.controller.dto.MemberResponse; import com.project.capstone.member.controller.dto.MyBookResponse; import com.project.capstone.member.domain.Member; @@ -13,17 +11,14 @@ import com.project.capstone.mybook.domain.MyBook; import com.project.capstone.mybook.domain.MyBookRepository; import com.project.capstone.mybook.exception.MyBookException; -import com.project.capstone.mybook.exception.MyBookExceptionType; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; -import java.util.Optional; import java.util.UUID; -import static com.project.capstone.book.exception.BookExceptionType.BOOK_NOT_FOUND; import static com.project.capstone.member.exception.MemberExceptionType.MEMBER_NOT_FOUND; import static com.project.capstone.mybook.exception.MyBookExceptionType.ALREADY_EXIST_MYBOOK; @@ -54,7 +49,7 @@ public List getMyBooks(String userId) { return books; } - public void addMyBook(String userId, AddMyBookRequest request) { + public void addMyBook(String userId, AddBookRequest request) { Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow( () -> new MemberException(MEMBER_NOT_FOUND) );