Skip to content

Commit

Permalink
[BOOK-33]-feat: isbn 해당하는 도서 반환
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeHanEum committed Oct 12, 2024
1 parent f3bbb34 commit b6cf7e5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
import java.util.Optional;
import java.util.stream.Collectors;

import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.book.presentation.response.BookListResponse;
import goorm.unit.booklog.domain.book.presentation.response.UserBookListResponse;
import goorm.unit.booklog.domain.file.domain.File;
import goorm.unit.booklog.domain.review.domain.ReviewRepository;
import goorm.unit.booklog.domain.review.domain.Review;
import goorm.unit.booklog.domain.user.application.UserService;
import goorm.unit.booklog.domain.user.domain.User;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -26,9 +18,18 @@
import org.springframework.web.util.UriComponentsBuilder;

import goorm.unit.booklog.common.response.PageableResponse;
import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.book.domain.BookRepository;
import goorm.unit.booklog.domain.book.presentation.exception.BookNotFoundException;
import goorm.unit.booklog.domain.book.presentation.response.BookListResponse;
import goorm.unit.booklog.domain.book.presentation.response.BookPageResponse;
import goorm.unit.booklog.domain.book.presentation.response.BookResponse;
import goorm.unit.booklog.domain.book.presentation.response.BookSummaryResponse;
import goorm.unit.booklog.domain.book.presentation.response.UserBookListResponse;
import goorm.unit.booklog.domain.file.domain.File;
import goorm.unit.booklog.domain.review.domain.Review;
import goorm.unit.booklog.domain.user.application.UserService;
import goorm.unit.booklog.domain.user.domain.User;
import lombok.RequiredArgsConstructor;

@Service
Expand Down Expand Up @@ -104,6 +105,10 @@ public BookListResponse getDefaultBookList(int size) {
return BookListResponse.of(randomBooks);
}

public BookSummaryResponse getBookIdByIsbn(String isbn) {
Book book = bookRepository.findByIsbn(isbn).orElseThrow(BookNotFoundException::new);
return BookSummaryResponse.from(book);
}

public Book getBookById(Long id) {
return bookRepository.findById(id).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface BookRepository {

Optional<Book> findById(Long id);

Optional<Book> findByIsbn(String isbn);

Optional<Book> findByTitleAndAuthor(String title, String author);

List<Book> findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public Optional<Book> findById(Long id) {
return jpaBookRepository.findById(id);
}

@Override
public Optional<Book> findByIsbn(String isbn) {
return jpaBookRepository.findByIsbn(isbn);
}

@Override
public Optional<Book> findByTitleAndAuthor(String title, String author) {
return jpaBookRepository.findByTitleAndAuthor(title, author);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface JpaBookRepository extends JpaRepository<Book, Long> {
Optional<Book> findByTitleAndAuthor(String title, String author);

Optional<Book> findByIsbn(String isbn);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package goorm.unit.booklog.domain.book.presentation;

import goorm.unit.booklog.domain.book.presentation.response.BookListResponse;
import goorm.unit.booklog.domain.book.presentation.response.BookSummaryResponse;
import goorm.unit.booklog.domain.book.presentation.response.UserBookListResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -61,6 +63,22 @@ public ResponseEntity<BookListResponse> getDefaultBookList(
return ResponseEntity.ok(response);
}

@Operation(summary = "isbn에 해당하는 도서 ID 반환", description = "isbn에 해당하는 도서 ID를 반환합니다.")
@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "isbn에 해당하는 도서 ID 반환 성공",
content = @Content(schema = @Schema(implementation = BookSummaryResponse.class))
)
})
@GetMapping("/{isbn}")
public ResponseEntity<BookSummaryResponse> getBookIdByIsbn(
@Parameter(description = "isbn", example = "9788953583979", required = true) @PathVariable(value = "isbn") String isbn
) {
BookSummaryResponse response = bookService.getBookIdByIsbn(isbn);
return ResponseEntity.ok(response);
}

@Operation(summary = "유저 활동 내역 조회", description = " 작성한 독후감의 수와 읽은 책의 수, 표지, 제목을 반환합니다.")
@ApiResponses({
@ApiResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package goorm.unit.booklog.domain.book.presentation.response;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import goorm.unit.booklog.domain.book.domain.Book;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;

@Builder
public record BookSummaryResponse(
@Schema(description = "도서 ID", example = "1", requiredMode = REQUIRED)
Long id,

@Schema(description = "도서 제목", example = "스프링 부트와 AWS로 혼자 구현하는 웹 서비스", requiredMode = REQUIRED)
String title
) {
public static BookSummaryResponse from (Book book) {
return BookSummaryResponse.builder()
.id(book.getId())
.title(book.getTitle())
.build();
}
}

0 comments on commit b6cf7e5

Please sign in to comment.