Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat/#127] 책 한 권 조회 API의 반환 값에 description 컬럼 추가 #128

Merged
merged 3 commits into from
Oct 15, 2024
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 @@ -20,4 +20,5 @@ public class BookRes {
private int pages;
private int likes;
private boolean likeStatus;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.techeer.checkIt.domain.book.dto.Response;

import lombok.*;

import java.util.List;


@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class BookResVO {
private List<Item> items;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = lombok.AccessLevel.PRIVATE)
public static class Item {
private String description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@Component
public class BookMapper {
public BookRes toDto(Book book, int likes, boolean likeStatus) {
public BookRes toDto(Book book, int likes, boolean likeStatus, String description) {
return BookRes.builder()
.id(book.getId())
.title(book.getTitle())
Expand All @@ -27,6 +27,7 @@ public BookRes toDto(Book book, int likes, boolean likeStatus) {
.pages(book.getPages())
.likes(likes)
.likeStatus(likeStatus)
.description(description)
.build();
}
public BookSearchRes toBookSearchDto(BookDocument book) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.techeer.checkIt.domain.book.service;

import com.techeer.checkIt.domain.book.dto.Response.BookResVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.nio.charset.StandardCharsets;

@Service
@Slf4j
public class BookSearchService {
private final RestTemplate restTemplate;

@Value("${naver.client.id}")
private String CLIENT_ID;

@Value("${naver.client.secret}")
private String CLIENT_SECRET;

private final String SEARCH_URL = "https://openapi.naver.com/v1/search/book.json";

public BookSearchService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}

public String getBookDescriptionByTitle(String title){
final HttpHeaders headers = new HttpHeaders();
headers.set("X-Naver-Client-Id", CLIENT_ID);
headers.set("X-Naver-Client-Secret", CLIENT_SECRET);

URI targetUrl = UriComponentsBuilder
.fromUriString(SEARCH_URL)
.queryParam("query", title)
.build()
.encode(StandardCharsets.UTF_8)
.toUri();

HttpEntity<?> entity = new HttpEntity<>(headers);

// 네이버 API 요청 보내기
ResponseEntity<BookResVO> response = restTemplate.exchange(
targetUrl,
HttpMethod.GET,
entity,
BookResVO.class
);

BookResVO bookResponse = response.getBody();
if (bookResponse != null && !bookResponse.getItems().isEmpty()) {
return bookResponse.getItems().get(0).getDescription();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class BookService {
private final BookSearchRepository bookSearchRepository;
private final BookMapper bookMapper;
private final RedisDao redisDao;
private final BookSearchService bookSearchService;

public List<BookSearchRes> findBookByTitle(String title) {
List<BookDocument> books = bookSearchRepository.findByTitleContaining(title);
Expand All @@ -55,7 +56,11 @@ public BookRes findBookById(Long userId, Long bookId) {
String values = (redisDao.getValues(redisKey) == null) ? redisDao.setValues(redisKey,"0") : redisDao.getValues(redisKey);
int likes = Integer.parseInt(values);
boolean likeStatus = !redisDao.getValuesList(redisUserKey).contains(redisKey.substring(1)) ? false : true;
return bookMapper.toDto(book, likes, likeStatus);

// 네이버 책 검색 API 통해 description 가져오기
String description = bookSearchService.getBookDescriptionByTitle(book.getTitle());

return bookMapper.toDto(book, likes, likeStatus, description);
}
// 책 판별용
public Book findById(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti

http.csrf().disable().cors().and()
.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint) // 403 (권한이 없는 사용자)
.accessDeniedHandler(jwtAccessDeniedHandler) // 401 (인증되지 않은 사용자)
.authenticationEntryPoint(jwtAuthenticationEntryPoint) // 401 (인증되지 않은 사용자)
.accessDeniedHandler(jwtAccessDeniedHandler) // 403 (권한이 없는 사용자)
.and()
.httpBasic().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) // Security는 기본적으로 세션 사용하지만, jwt 사용할 것이기 때문에 STATELESS 로 설정
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/security
Loading