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 index a143214931..99cab82d62 100644 --- a/backend/src/main/java/com/project/capstone/book/controller/BookController.java +++ b/backend/src/main/java/com/project/capstone/book/controller/BookController.java @@ -1,11 +1,16 @@ package com.project.capstone.book.controller; import com.project.capstone.book.controller.dto.AddBookRequest; +import com.project.capstone.book.controller.dto.BookResponse; import com.project.capstone.book.service.BookService; +import com.project.capstone.content.controller.dto.ContentResponse; +import com.project.capstone.quiz.controller.dto.QuizResponse; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @RequestMapping("/book") @RequiredArgsConstructor @@ -19,4 +24,27 @@ public ResponseEntity addBook(@RequestBody AddBookRequest request) { bookService.addBook(request); return ResponseEntity.ok().body("도서 추가 완료"); } + + // 책 기본 정보 불러오기 + @GetMapping("/search/{isbn}") + public ResponseEntity getBook(@PathVariable String isbn) { + BookResponse bookResponse = bookService.getBookByIsbn(isbn); + return ResponseEntity.ok(bookResponse); + } + + // 해당 책의 퀴즈 불러오기 + @GetMapping("/{isbn}/quiz") + public ResponseEntity> getQuizByBook(@PathVariable String isbn) { + List quizResponseList = bookService.getQuizByBook(isbn); + return ResponseEntity.ok(quizResponseList); + } + + // 해당 책의 컨텐츠 불러오기 + @GetMapping("/{isbn}/content") + public ResponseEntity> getContentsByBook(@PathVariable String isbn, @RequestParam String type) { + List contentResponseList = bookService.getContentsByBook(isbn, type); + return ResponseEntity.ok(contentResponseList); + } + + } diff --git a/backend/src/main/java/com/project/capstone/book/controller/dto/AddBookRequest.java b/backend/src/main/java/com/project/capstone/book/controller/dto/AddBookRequest.java index d4fbe48912..7f30d8e4af 100644 --- a/backend/src/main/java/com/project/capstone/book/controller/dto/AddBookRequest.java +++ b/backend/src/main/java/com/project/capstone/book/controller/dto/AddBookRequest.java @@ -3,11 +3,9 @@ public record AddBookRequest( String isbn, String title, - String category1d, - String category2d, - String category3d, String author, String publisher, - String publishDate + String publishDate, + String imageUrl ) { } diff --git a/backend/src/main/java/com/project/capstone/book/controller/dto/BookResponse.java b/backend/src/main/java/com/project/capstone/book/controller/dto/BookResponse.java index 0a8c3a31e9..fe2d6eb2f5 100644 --- a/backend/src/main/java/com/project/capstone/book/controller/dto/BookResponse.java +++ b/backend/src/main/java/com/project/capstone/book/controller/dto/BookResponse.java @@ -7,9 +7,10 @@ public record BookResponse( String isbn, String title, String author, - String publisher + String publisher, + String imageUrl ) { public BookResponse(Book book) { - this(book.getId(), book.getIsbn(), book.getTitle(), book.getAuthor(), book.getPublisher()); + this(book.getId(), book.getIsbn(), book.getTitle(), book.getAuthor(), book.getPublisher(), book.getImageUrl()); } } 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 a48d0344f6..c4e7f3ef42 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 @@ -27,16 +27,12 @@ public class Book { private Long id; private String isbn; private String title; - @Column(name = "category_1d") - private String category1d; - @Column(name = "category_2d") - private String category2d; - @Column(name = "category_3d") - private String category3d; private String author; private String publisher; @Column(name = "publish_date") private String publishDate; + @Column(name = "image_url") + private String imageUrl; @JsonManagedReference @OneToMany(mappedBy = "book") @@ -55,8 +51,8 @@ public class Book { private List membersAddThisBook = new ArrayList<>(); 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<>()); + this(null, request.isbn(), request.title(), request.author(), request.publisher(), request.publishDate(), + request.imageUrl(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); } } 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 index 5e25445a65..3bcdf51da1 100644 --- a/backend/src/main/java/com/project/capstone/book/service/BookService.java +++ b/backend/src/main/java/com/project/capstone/book/service/BookService.java @@ -1,24 +1,74 @@ package com.project.capstone.book.service; import com.project.capstone.book.controller.dto.AddBookRequest; +import com.project.capstone.book.controller.dto.BookResponse; 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.content.controller.dto.ContentResponse; +import com.project.capstone.content.domain.Content; +import com.project.capstone.content.domain.ContentRepository; +import com.project.capstone.content.domain.ContentType; +import com.project.capstone.content.exception.ContentException; +import com.project.capstone.quiz.controller.dto.QuizResponse; +import com.project.capstone.quiz.domain.Quiz; +import com.project.capstone.quiz.domain.QuizRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + import static com.project.capstone.book.exception.BookExceptionType.ALREADY_EXIST_BOOK; +import static com.project.capstone.book.exception.BookExceptionType.BOOK_NOT_FOUND; +import static com.project.capstone.content.exception.ContentExceptionType.TYPE_NOT_FOUND; @Service @RequiredArgsConstructor @Slf4j public class BookService { private final BookRepository bookRepository; + private final QuizRepository quizRepository; + private final ContentRepository contentRepository; public void addBook(AddBookRequest request) { if (bookRepository.findBookByIsbn(request.isbn()).isPresent()) { throw new BookException(ALREADY_EXIST_BOOK); } bookRepository.save(new Book(request)); } + + public BookResponse getBookByIsbn(String isbn) { + Book book = getBook(isbn); + return new BookResponse(book); + } + + public List getQuizByBook(String isbn) { + Book book = getBook(isbn); + List quizList= quizRepository.findQuizzesByBook(book); + List quizResponseList = new ArrayList<>(); + for (Quiz quiz : quizList) { + quizResponseList.add(new QuizResponse(quiz)); + } + return quizResponseList; + } + + private Book getBook(String isbn) { + return bookRepository.findBookByIsbn(isbn).orElseThrow( + () -> new BookException(BOOK_NOT_FOUND) + ); + } + + public List getContentsByBook(String isbn, String type) { + Book book = getBook(isbn); + for (ContentType contentType : ContentType.values()) { + if (contentType.equals(ContentType.valueOf(type))) { + List contentsByTypeAndBook = contentRepository.findContentsByTypeAndBook(ContentType.valueOf(type), book); + return contentsByTypeAndBook.stream() + .map(ContentResponse::new) + .toList(); + } + } + throw new ContentException(TYPE_NOT_FOUND); + } } diff --git a/backend/src/main/java/com/project/capstone/content/domain/ContentRepository.java b/backend/src/main/java/com/project/capstone/content/domain/ContentRepository.java index 86e2e2037a..58c049abcd 100644 --- a/backend/src/main/java/com/project/capstone/content/domain/ContentRepository.java +++ b/backend/src/main/java/com/project/capstone/content/domain/ContentRepository.java @@ -1,5 +1,6 @@ package com.project.capstone.content.domain; +import com.project.capstone.book.domain.Book; import com.project.capstone.club.domain.Club; import org.springframework.data.jpa.repository.JpaRepository; @@ -9,4 +10,6 @@ public interface ContentRepository extends JpaRepository { Optional findContentById(Long id); List findContentsByTypeAndClub(ContentType type, Club club); + + List findContentsByTypeAndBook(ContentType type, Book book); } diff --git a/backend/src/main/java/com/project/capstone/quiz/domain/QuizRepository.java b/backend/src/main/java/com/project/capstone/quiz/domain/QuizRepository.java index 7d0568b8bc..af5747e6d3 100644 --- a/backend/src/main/java/com/project/capstone/quiz/domain/QuizRepository.java +++ b/backend/src/main/java/com/project/capstone/quiz/domain/QuizRepository.java @@ -1,9 +1,13 @@ package com.project.capstone.quiz.domain; +import com.project.capstone.book.domain.Book; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface QuizRepository extends JpaRepository { Optional findQuizById(Long id); + + List findQuizzesByBook(Book book); }