Skip to content

Commit

Permalink
[BOOK-26]- 기본 도서 목록 랜덤 조회 API (#18)
Browse files Browse the repository at this point in the history
* [BOOK-26]-feat: 기본 도서 목록 랜덤 조회 API

* [BOOK-26]-refactor: 스키마 변경
  • Loading branch information
LeeHanEum authored Oct 12, 2024
1 parent d674613 commit f3bbb34
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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;
Expand Down Expand Up @@ -33,7 +36,6 @@
public class BookService {
private final BookRepository bookRepository;
private final UserService userService;
private final ReviewRepository reviewRepository;

@Value("${naver.api.clientId}")
private String clientId;
Expand Down Expand Up @@ -93,6 +95,16 @@ public BookPageResponse searchBooks(int page, int size, String keyword) {
return BookPageResponse.of(bookResponses, PageableResponse.of(PageRequest.of(page, size), (long)total));
}

public BookListResponse getDefaultBookList(int size) {
List<Book> books = bookRepository.findAll();
Collections.shuffle(books);
List<Book> randomBooks = books.stream()
.limit(size)
.collect(Collectors.toList());
return BookListResponse.of(randomBooks);
}


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 @@ -13,4 +13,5 @@ public interface BookRepository {

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

List<Book> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ public Optional<Book> findById(Long id) {
public Optional<Book> findByTitleAndAuthor(String title, String author) {
return jpaBookRepository.findByTitleAndAuthor(title, author);
}

@Override
public List<Book> findAll() {
return jpaBookRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package goorm.unit.booklog.domain.book.presentation;

import goorm.unit.booklog.domain.book.presentation.response.BookListResponse;
import goorm.unit.booklog.domain.book.presentation.response.UserBookListResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -44,6 +45,22 @@ public ResponseEntity<BookPageResponse> searchBooks(
return ResponseEntity.ok(response);
}

@Operation(summary = "기본 도서 목록 조회", description = "랜덤으로 요청한 개수 만큼의 도서 목록을 반환합니다.")
@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "기본 도서 목록 조회 성공",
content = @Content(schema = @Schema(implementation = BookListResponse.class))
)
})
@GetMapping
public ResponseEntity<BookListResponse> getDefaultBookList(
@Parameter(description = "응답 개수", example = "10", required = true) @RequestParam(value = "size", defaultValue = "10") int size
) {
BookListResponse response = bookService.getDefaultBookList(size);
return ResponseEntity.ok(response);
}

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

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

import java.util.List;

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

@Builder
public record BookListResponse(
@Schema(
description = "도서 목록",
example = "[{\"id\": 1, \"title\": \"스프링 부트와 AWS로 혼자 구현하는 웹 서비스\", \"author\": \"이동욱\", \"description\": \"이 책은 스프링부트 입문자용 책입니다.\", \"file\": {\"id\": 1, \"logicalName\": \"example.jpg\", \"physicalPath\": \"https://example-bucket.ncp.com/files/example.jpg\"}}]",
requiredMode = REQUIRED
)
List<BookResponse> contents
) {
public static BookListResponse of(List<Book> books) {
return BookListResponse.builder()
.contents(books.stream()
.map(BookResponse::from)
.toList())
.build();
}
}
6 changes: 5 additions & 1 deletion src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
spring:
jpa:
hibernate:
ddl-auto: update
ddl-auto: update
data:
redis:
host: localhost
port: 6379

0 comments on commit f3bbb34

Please sign in to comment.