Skip to content

Commit

Permalink
feat: 게시물 전체 개수 조회 API (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
yongbin97 committed Oct 2, 2024
1 parent 2f224db commit 27fb0e7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/java/project/backend/business/post/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import project.backend.business.post.request.PostListServiceRequest;
import project.backend.business.post.request.UpdatePostServiceRequest;
import project.backend.business.post.response.CreateUpdatePostResponse;
import project.backend.business.post.response.PostCountResponse;
import project.backend.business.post.response.PostDetailResponse;
import project.backend.business.post.response.PostListResponse;
import project.backend.business.post.response.dto.PostDetailDto;
Expand Down Expand Up @@ -58,6 +59,12 @@ public PostListResponse getPosts(Long userId, PostListServiceRequest postListSer
return PostListResponse.from(postListDtos);
}

@Transactional(readOnly = true)
public PostCountResponse getTotalPostCount(Long userId) {
int count = postReader.readActivatePostCountByUserId(userId);
return PostCountResponse.from(count);
}

@Transactional(readOnly = true)
public PostDetailResponse getPostDetail(Long userId,
PostDetailServiceRequest postDetailServiceRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ public PostDetailDto readPostDetailWithTags(Long userId,
"yy.MM.dd"))
.build();
}

public int readActivatePostCountByUserId(Long userId) {
return postRepository.countPostsByUserIdAndStatusAndActivatedTrue(userId, PostStatus.PUBLISHED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package project.backend.business.post.response;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class PostCountResponse {

private final int totalCount;

public static PostCountResponse from(int count) {
return PostCountResponse.builder()
.totalCount(count)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import project.backend.business.post.request.PostDetailServiceRequest;
import project.backend.business.post.request.PostListServiceRequest;
import project.backend.business.post.response.CreateUpdatePostResponse;
import project.backend.business.post.response.PostCountResponse;
import project.backend.business.post.response.PostDetailResponse;
import project.backend.business.post.response.PostListResponse;
import project.backend.presentation.post.docs.PostControllerDocs;
Expand All @@ -35,6 +36,13 @@ public class PostController implements PostControllerDocs {

private final PostService postService;

@AssignCurrentUserInfo
@GetMapping("/count")
public ResponseEntity<PostCountResponse> getPostCount(CurrentUserInfo userInfo) {
PostCountResponse response = postService.getTotalPostCount(userInfo.getUserId());
return new ResponseEntity<>(response, HttpStatus.OK);
}

@AssignCurrentUserInfo
@GetMapping
public ResponseEntity<PostListResponse> getPosts(CurrentUserInfo userInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import project.backend.business.post.response.CreateUpdatePostResponse;
import project.backend.business.post.response.PostCountResponse;
import project.backend.business.post.response.PostDetailResponse;
import project.backend.business.post.response.PostListResponse;
import project.backend.entity.post.PostStatus;
Expand All @@ -16,7 +17,7 @@
@Tag(name = "게시글 API")
public interface PostControllerDocs {

@Operation(summary = "게시글 목록 조회 API", description = "로그인한 유저가 작성한 게시글을 최근 작성한 순서대로 조회합니다.")
@Operation(summary = "게시글 목록 조회 API", description = "로그인한 유저의 게시글을 최근 작성한 순서대로 조회.")
@Parameter(name = "userInfo", hidden = true)
@Parameter(name = "page", description = "조회할 페이지 번호. 시작 = 0")
@Parameter(name = "archiveId", description = "조회할 아카이브 번호.")
Expand All @@ -27,6 +28,10 @@ ResponseEntity<PostListResponse> getPosts(
Long archiveId,
String search);

@Operation(summary = "게시글 개수 조회 API", description = "로그인한 유저가 작성한 게시글의 총 개수 조회.")
@Parameter(name = "userInfo", hidden = true)
ResponseEntity<PostCountResponse> getPostCount(CurrentUserInfo userInfo);

@Operation(summary = "요약 요청 API", description = "요약 옵션을 통해 웹사이트 요약.")
@Parameter(name = "userInfo", hidden = true)
ResponseEntity<CreateUpdatePostResponse> createPost(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@Repository
public interface PostRepository extends JpaRepository<Post, Long>, JpaSpecificationExecutor {

int countPostsByUserIdAndStatusAndActivatedTrue(Long userId, PostStatus status);

Optional<Post> findPostAndUserAndActivatedTrueById(Long postId);

Optional<Post> findByIdAndUserIdAndActivatedTrue(Long postId, Long userId);
Expand Down

0 comments on commit 27fb0e7

Please sign in to comment.