Skip to content

Commit

Permalink
Feat: 내가 찜한 책 조회 기능 구현 (#145)
Browse files Browse the repository at this point in the history
* #142 - feat: 내가 찜한책 조회 api 구현

* #142 - test: 내가 찜한 책 조회 기능 테스트 작성
  • Loading branch information
GGHDMS authored Feb 18, 2024
1 parent 61d067e commit 77d9fd8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import cotato.bookitlist.book.dto.response.BookListResponse;
import cotato.bookitlist.book.dto.response.BookResponse;
import cotato.bookitlist.book.service.BookService;
import cotato.bookitlist.config.security.jwt.AuthDetails;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

Expand Down Expand Up @@ -79,6 +81,14 @@ public ResponseEntity<Void> registerBook(

return ResponseEntity.created(location).build();
}

@GetMapping("/likes")
public ResponseEntity<BookListResponse> getLikeBooks(
@AuthenticationPrincipal AuthDetails details,
Pageable pageable
) {
return ResponseEntity.ok(bookService.getLikeBooks(details.getId(), pageable));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public interface BookRepository extends JpaRepository<Book, Long> {
Optional<Book> findByIsbn13(String isbn13);

// TODO: 단순 like query로 한영에 따라 결과가 다르게 나온다. 이를 해결해야함!!
@Query("SELECT b FROM Book b WHERE LOWER(b.title) LIKE LOWER(CONCAT('%', :keyword, '%')) OR LOWER(b.author) LIKE LOWER(CONCAT('%', :keyword, '%')) OR LOWER(b.description) LIKE LOWER(CONCAT('%', :keyword, '%'))")
@Query("select b from Book b where lower(b.title) like lower(concat('%', :keyword, '%')) or lower(b.author) like lower(concat('%', :keyword, '%')) or lower(b.description) like lower(concat('%', :keyword, '%'))")
Page<Book> findAllByKeyword(@Param("keyword") String keyword, Pageable pageable);

@Query("select b from BookLike l join l.book b join l.member m where m.id = :memberId")
Page<Book> findLikeBookByMemberId(Long memberId, Pageable pageable);
}
6 changes: 6 additions & 0 deletions src/main/java/cotato/bookitlist/book/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cotato.bookitlist.book.dto.BookApiDto;
import cotato.bookitlist.book.dto.BookDto;
import cotato.bookitlist.book.dto.response.BookApiListResponse;
import cotato.bookitlist.book.dto.response.BookListResponse;
import cotato.bookitlist.book.repository.BookRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -60,4 +61,9 @@ public Long registerBook(String isbn13) {
.orElseGet(() -> bookApiComponent.findByIsbn13(isbn13)).toEntity()
).getId();
}


public BookListResponse getLikeBooks(Long memberId, Pageable pageable) {
return BookListResponse.from(bookRepository.findLikeBookByMemberId(memberId, pageable));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers(WHITE_LIST).permitAll()
.requestMatchers("/posts/likes", "/posts/me").authenticated()
.requestMatchers(HttpMethod.GET, "/posts/likes", "/posts/me", "/books/likes").authenticated()
.requestMatchers(HttpMethod.GET).permitAll()
.requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
.anyRequest().authenticated()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cotato.bookitlist.book.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import cotato.bookitlist.annotation.WithCustomMockUser;
import cotato.bookitlist.book.dto.request.BookIsbn13Request;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -268,4 +269,28 @@ void givenNonExistedId_whenGettingBook_thenReturnErrorResponse() throws Exceptio
.andExpect(jsonPath("$.message").value("책을 찾을 수 없습니다."))
;
}

@Test
@WithCustomMockUser
@DisplayName("찜한 책 목록을 조회한다.")
void givenLoginMember_whenGettingLikePosts_thenReturnBookListResponse() throws Exception{
//given

//when & then
mockMvc.perform(get("/books/likes"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.totalResults").value(2))
;
}

@Test
@DisplayName("로그인 없이 찜한 책 목록을 조회하면 에러를 반환한다.")
void givenNonLoginMember_whenGettingLikePosts_thenReturnErrorResponse() throws Exception {
//given

//when & then
mockMvc.perform(get("/books/likes"))
.andExpect(status().isUnauthorized())
;
}
}

0 comments on commit 77d9fd8

Please sign in to comment.