Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import com.fastcampus.book_bot.service.api.ApiToMySQLService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -23,16 +19,16 @@ public BookController(ApiToMySQLService apiToMySQLService) {
this.apiToMySQLService = apiToMySQLService;
}

@PostMapping("/search")
public Mono<ResponseEntity<List<Book>>> searchAndSaveBooks(
@GetMapping("/search")
public ResponseEntity<String> searchAndSaveBooks(
@RequestParam String query,
@RequestParam(defaultValue = "1") int start,
@RequestParam(defaultValue = "10") int display) {

log.info("도서 검색 요청: query={}, start={}, display={}", query, start, display);

return apiToMySQLService.searchAndSaveBooks(query, start, display)
.map(ResponseEntity::ok)
.onErrorReturn(ResponseEntity.internalServerError().build());
List<Book> bookList = apiToMySQLService.searchAndSaveBooks(query, start, display);

return ResponseEntity.ok("도서 저장 완료: " + bookList.size());
}
}
11 changes: 6 additions & 5 deletions src/main/java/com/fastcampus/book_bot/domain/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.time.LocalDate;

@Entity
@Table(name = "books")
Expand All @@ -25,10 +26,10 @@ public class Book {
@Column(name = "BOOK_ID", nullable = false, updatable = false)
private Long bookId;

@Column(name = "BOOK_TITLE", length = 100, nullable = false)
@Column(name = "BOOK_TITLE", length = 500, nullable = false)
private String bookTitle;

@Column(name = "BOOK_AUTHOR", length = 200)
@Column(name = "BOOK_AUTHOR", length = 500)
private String bookAuthor;

@Column(name = "BOOK_LINK", length = 200)
Expand All @@ -41,13 +42,13 @@ public class Book {
private String bookPublisher;

@Column(name = "BOOK_ISBN")
private Integer bookIsbn;
private Long bookIsbn;

@Column(name = "BOOK_DESCRIPTION", length = 500)
@Column(name = "BOOK_DESCRIPTION", length = 2000)
private String bookDescription;

@Column(name = "BOOK_PUBDATE")
private LocalDateTime bookPubdate;
private LocalDate bookPubdate;

@Column(name = "BOOK_DISCOUNT")
private Integer bookDiscount;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/fastcampus/book_bot/dto/api/BookDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.fastcampus.book_bot.dto.api;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.time.LocalDate;

@Data
public class BookDTO {

private String title;
private String link;
private String image;
private String author;
private Integer discount;
private String publisher;
private Long isbn;
private String description;

@JsonFormat(pattern = "yyyyMMdd")
private LocalDate pubdate;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,5 @@ public class NaverBookResponseDTO {
private int total;
private int start;
private int display;
private NaverBookItemDTO[] items;

@Data
public static class NaverBookItemDTO {

private String title;
private String link;
private String image;
private String author;
private int discount;
private String publisher;
private int isbn;
private String description;
private LocalDateTime pubdate;
}
private BookDTO[] items;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {

Optional<Book> findByBookIsbn(Integer bookIsbn);
Optional<Book> findByBookIsbn(Long bookIsbn);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.fastcampus.book_bot.service.api;

import com.fastcampus.book_bot.domain.Book;
import com.fastcampus.book_bot.dto.api.BookDTO;
import com.fastcampus.book_bot.dto.api.NaverBookResponseDTO;
import com.fastcampus.book_bot.repository.BookRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

Expand All @@ -32,24 +33,22 @@ public ApiToMySQLService(BookRepository bookRepository, NaverBookAPIService nave
this.naverBookAPIService = naverBookAPIService;
}

@Transactional
public Mono<List<Book>> searchAndSaveBooks(String query, int start, int display) {
return naverBookAPIService.searchBooks(query, start, display)
.map(this::saveBooks)
.doOnNext(books -> log.info("총 {} 건의 도서가 저장되었습니다.", books.size()))
.doOnError(error -> log.error("도서 검색 및 저장 중 오류 발생: {}", error.getMessage()));
public List<Book> searchAndSaveBooks(String query, int start, int display) {
NaverBookResponseDTO response = naverBookAPIService.searchBooks(query, start, display);

if (response.getItems() == null || response.getItems().length == 0) {
log.warn("조건에 맞는 도서가 없습니다. 검색어: {}", query);
return Collections.emptyList();
}

return saveBooks(response);
}

@Transactional
public List<Book> saveBooks(NaverBookResponseDTO response) {
List<Book> savedBooks = new ArrayList<>();

if (response.getItems() == null || response.getItems().length == 0) {
log.warn("API 응답에 책 정보가 없습니다.");
return savedBooks;
}

for (NaverBookResponseDTO.NaverBookItemDTO item : response.getItems()) {
for (BookDTO item : response.getItems()) {
try {
Book book = convertToBook(item);

Expand All @@ -60,14 +59,15 @@ public List<Book> saveBooks(NaverBookResponseDTO response) {
} catch (Exception e) {
{
log.error("도서 저장 중 오류 발생: {}, 도서: {}", e.getMessage(), item.getTitle());
return Collections.emptyList();
}
}
}

return savedBooks;
}

private Book convertToBook(NaverBookResponseDTO.NaverBookItemDTO item) {
private Book convertToBook(BookDTO item) {
return Book.builder()
.bookTitle(item.getTitle())
.bookAuthor(item.getAuthor())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public NaverBookAPIService(WebClient webClient) {
this.webClient = webClient;
}

public Mono<NaverBookResponseDTO> searchBooks(String query, int start, int display) {
public NaverBookResponseDTO searchBooks(String query, int start, int display) {
return webClient.get()
.uri(uriBuilder -> uriBuilder.path("/book.json")
.queryParam("query", query)
Expand All @@ -55,6 +55,7 @@ public Mono<NaverBookResponseDTO> searchBooks(String query, int start, int displ
.timeout(Duration.ofSeconds(30))
.doOnNext(response -> log.debug("API 응답 성공: 총 {} 건", response.getTotal()))
.doOnError(error -> log.error("네이버 도서 API 비동기 호출 실패: {}", error.getMessage()))
.onErrorReturn(new NaverBookResponseDTO());
.onErrorReturn(new NaverBookResponseDTO())
.block(Duration.ofSeconds(30));
}
}