Skip to content

Commit

Permalink
Feat: 책과 관련된 게시글 수 반환 api 구현 (#64)
Browse files Browse the repository at this point in the history
* #63 - feat: 책관 관련되 게시글 수 가져오는 기능 구현

* #63 - test: 테스트 기능 구현
  • Loading branch information
GGHDMS authored Feb 4, 2024
1 parent 8a5b1ad commit 3ad90d5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cotato.bookitlist.config.security.jwt.AuthDetails;
import cotato.bookitlist.post.dto.requeset.PostRegisterRequest;
import cotato.bookitlist.post.dto.requeset.PostUpdateRequest;
import cotato.bookitlist.post.dto.response.PostCountResponse;
import cotato.bookitlist.post.dto.response.PostListResponse;
import cotato.bookitlist.post.dto.response.PostResponse;
import cotato.bookitlist.post.service.PostService;
Expand Down Expand Up @@ -74,4 +75,11 @@ public ResponseEntity<PostListResponse> searchPost(
) {
return ResponseEntity.ok(postService.searchPost(isbn13, pageable));
}

@GetMapping("/count")
public ResponseEntity<PostCountResponse> getPostCount(
@IsValidIsbn @RequestParam String isbn13
) {
return ResponseEntity.ok(postService.getPostCount(isbn13));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cotato.bookitlist.post.dto.response;

public record PostCountResponse(
int count
) {

public static PostCountResponse of(int count) {
return new PostCountResponse(count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface PostRepository extends JpaRepository<Post, Long> {
Page<Post> findByBook_Isbn13(String isbn13, Pageable pageable);

int countByBook_Isbn13(String isbn13);
}
6 changes: 6 additions & 0 deletions src/main/java/cotato/bookitlist/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cotato.bookitlist.member.repository.MemberRepository;
import cotato.bookitlist.post.domain.Post;
import cotato.bookitlist.post.dto.PostDto;
import cotato.bookitlist.post.dto.response.PostCountResponse;
import cotato.bookitlist.post.dto.response.PostListResponse;
import cotato.bookitlist.post.dto.requeset.PostRegisterRequest;
import cotato.bookitlist.post.dto.requeset.PostUpdateRequest;
Expand Down Expand Up @@ -60,4 +61,9 @@ public PostListResponse getAllPost(Pageable pageable) {
public PostListResponse searchPost(String isbn13, Pageable pageable) {
return PostListResponse.from(postRepository.findByBook_Isbn13(isbn13, pageable));
}

@Transactional(readOnly = true)
public PostCountResponse getPostCount(String isbn13) {
return PostCountResponse.of(postRepository.countByBook_Isbn13(isbn13));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,20 @@ void givenNothing_whenSearchingPost_thenReturnPostListResponse() throws Exceptio
.andExpect(jsonPath("$.totalResults").value(6))
;
}

@Test
@DisplayName("isbn13을 이용해 게시글 count를 조회한다.")
void givenIsbn13_whenCountingPost_thenReturnPostCountResponse() throws Exception {
//given
String isbn13 = "9788931514810";

//when & then
mockMvc.perform(get("/posts/count")
.param("isbn13", isbn13)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.count").value(4))
;
}
}

0 comments on commit 3ad90d5

Please sign in to comment.