Skip to content

Commit

Permalink
Merge pull request #63 from kookmin-sw/BE_Feature/#39-book
Browse files Browse the repository at this point in the history
Be feature/#39 book
  • Loading branch information
wjdwlghks authored May 10, 2024
2 parents ac9ae7c + e7a1bfe commit abbe483
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,4 +24,27 @@ public ResponseEntity<?> addBook(@RequestBody AddBookRequest request) {
bookService.addBook(request);
return ResponseEntity.ok().body("도서 추가 완료");
}

// 책 기본 정보 불러오기
@GetMapping("/search/{isbn}")
public ResponseEntity<BookResponse> getBook(@PathVariable String isbn) {
BookResponse bookResponse = bookService.getBookByIsbn(isbn);
return ResponseEntity.ok(bookResponse);
}

// 해당 책의 퀴즈 불러오기
@GetMapping("/{isbn}/quiz")
public ResponseEntity<List<QuizResponse>> getQuizByBook(@PathVariable String isbn) {
List<QuizResponse> quizResponseList = bookService.getQuizByBook(isbn);
return ResponseEntity.ok(quizResponseList);
}

// 해당 책의 컨텐츠 불러오기
@GetMapping("/{isbn}/content")
public ResponseEntity<List<ContentResponse>> getContentsByBook(@PathVariable String isbn, @RequestParam String type) {
List<ContentResponse> contentResponseList = bookService.getContentsByBook(isbn, type);
return ResponseEntity.ok(contentResponseList);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
12 changes: 4 additions & 8 deletions backend/src/main/java/com/project/capstone/book/domain/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -55,8 +51,8 @@ public class Book {
private List<MyBook> 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<>());
}

}
Original file line number Diff line number Diff line change
@@ -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<QuizResponse> getQuizByBook(String isbn) {
Book book = getBook(isbn);
List<Quiz> quizList= quizRepository.findQuizzesByBook(book);
List<QuizResponse> 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<ContentResponse> getContentsByBook(String isbn, String type) {
Book book = getBook(isbn);
for (ContentType contentType : ContentType.values()) {
if (contentType.equals(ContentType.valueOf(type))) {
List<Content> contentsByTypeAndBook = contentRepository.findContentsByTypeAndBook(ContentType.valueOf(type), book);
return contentsByTypeAndBook.stream()
.map(ContentResponse::new)
.toList();
}
}
throw new ContentException(TYPE_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,4 +10,6 @@
public interface ContentRepository extends JpaRepository<Content, Long> {
Optional<Content> findContentById(Long id);
List<Content> findContentsByTypeAndClub(ContentType type, Club club);

List<Content> findContentsByTypeAndBook(ContentType type, Book book);
}
Original file line number Diff line number Diff line change
@@ -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<Quiz, Long> {
Optional<Quiz> findQuizById(Long id);

List<Quiz> findQuizzesByBook(Book book);
}

0 comments on commit abbe483

Please sign in to comment.