-
Notifications
You must be signed in to change notification settings - Fork 1
feat : BoardType ENUM 속성 POST에 추가, refactor : postController,Service,Repository,Converter 코드를 커서 페이지네이션 기반으로 수정 #45
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
Conversation
refactor : PostController, PostService, PostRepository, PostConverter 커서 페이지네이션에 맞게 코드 리팩토링
|
""" WalkthroughThe codebase was refactored to implement cursor-based pagination for post retrieval, introduce board type context in post APIs, and update method signatures and entities accordingly. These changes span controllers, services, repositories, converters, and DTOs, ensuring board-aware, paginated post listing and consistent use of authenticated user details. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Controller
participant Service
participant Repository
Client->>Controller: GET /boards/{boardType}/posts?cursor={cursor}&size={size}
Controller->>Service: getPostList(boardType, cursor, size)
Service->>Repository: findByBoardTypeAndCursor(boardType, cursor, pageable)
Repository-->>Service: Slice<Post>
Service->>Controller: PostPreviewListDTO (with nextCursor, hasNext)
Controller-->>Client: Response (paginated posts)
Assessment against linked issues
Assessment against linked issues: Out-of-scope changesNo out-of-scope changes found. Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (3)
src/main/java/naughty/tuzamate/domain/post/repository/PostRepository.java (1)
11-17: LGTM: Well-implemented cursor-based pagination query.The JPQL query correctly implements cursor-based pagination with the following strengths:
- Proper filtering by
boardTypeand optional cursor- Correct ordering with
ORDER BY p.id DESCfor cursor pagination- Appropriate use of
Slice<Post>return type for paginationThe cursor condition
(:cursor IS NULL OR p.id < :cursor)properly handles both initial page load and subsequent pagination.Consider adding an index on
(boardType, id)for optimal query performance:CREATE INDEX idx_post_board_type_id ON post (board_type, id DESC);src/main/java/naughty/tuzamate/domain/post/converter/PostConverter.java (1)
43-44: Consider performance implications of comment count calculation.The implementation correctly adds author and comment count fields to the DTO. However,
post.getComments().size()may trigger an additional database query if comments are lazily loaded.Consider optimizing the comment count calculation by:
- Using a dedicated query with COUNT() in the repository
- Adding a
commentCountfield to the Post entity that's updated when comments are added/removed- Using a JOIN FETCH or projection in the repository query to avoid N+1 queries
// Alternative approach using COUNT query in repository @Query("SELECT p, COUNT(c) FROM Post p LEFT JOIN p.comments c WHERE p.id = :postId GROUP BY p") Optional<Object[]> findPostWithCommentCount(@Param("postId") Long postId);src/main/java/naughty/tuzamate/domain/post/service/query/PostQueryServiceImpl.java (1)
34-50: LGTM: Well-implemented cursor-based pagination logic.The implementation correctly handles cursor-based pagination with proper:
- Repository method invocation with board type and cursor filtering
- Stream-based DTO conversion
- Next cursor calculation from the last item's ID
- Pagination metadata (
hasNext) from the sliceThe logic properly handles both initial page load and subsequent pagination requests.
Consider adding null safety for the cursor calculation:
-Long nextCursor = slice.hasNext() ? previews.get(previews.size() - 1).id() : null; +Long nextCursor = slice.hasNext() && !previews.isEmpty() ? + previews.get(previews.size() - 1).id() : null;This prevents potential
IndexOutOfBoundsExceptionif the previews list is unexpectedly empty.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (12)
src/main/java/naughty/tuzamate/auth/principal/PrincipalDetails.java(1 hunks)src/main/java/naughty/tuzamate/domain/post/controller/PostController.java(2 hunks)src/main/java/naughty/tuzamate/domain/post/converter/PostConverter.java(3 hunks)src/main/java/naughty/tuzamate/domain/post/dto/PostReqDTO.java(1 hunks)src/main/java/naughty/tuzamate/domain/post/dto/PostResDTO.java(1 hunks)src/main/java/naughty/tuzamate/domain/post/entity/Post.java(2 hunks)src/main/java/naughty/tuzamate/domain/post/enums/BoardType.java(1 hunks)src/main/java/naughty/tuzamate/domain/post/repository/PostRepository.java(1 hunks)src/main/java/naughty/tuzamate/domain/post/service/command/PostCommandService.java(1 hunks)src/main/java/naughty/tuzamate/domain/post/service/command/PostCommandServiceImpl.java(6 hunks)src/main/java/naughty/tuzamate/domain/post/service/query/PostQueryService.java(1 hunks)src/main/java/naughty/tuzamate/domain/post/service/query/PostQueryServiceImpl.java(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/main/java/naughty/tuzamate/domain/post/dto/PostResDTO.java (1)
src/main/java/naughty/tuzamate/global/apiPayload/CustomResponse.java (1)
Builder(15-74)
src/main/java/naughty/tuzamate/domain/post/service/command/PostCommandServiceImpl.java (2)
src/main/java/naughty/tuzamate/domain/post/dto/PostReqDTO.java (1)
PostReqDTO(5-18)src/main/java/naughty/tuzamate/domain/post/dto/PostResDTO.java (1)
PostResDTO(8-52)
src/main/java/naughty/tuzamate/domain/post/service/command/PostCommandService.java (2)
src/main/java/naughty/tuzamate/domain/post/dto/PostReqDTO.java (1)
PostReqDTO(5-18)src/main/java/naughty/tuzamate/domain/post/dto/PostResDTO.java (1)
PostResDTO(8-52)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build-docker-image
🔇 Additional comments (37)
src/main/java/naughty/tuzamate/auth/principal/PrincipalDetails.java (1)
27-29: LGTM! Clean abstraction for accessing user ID.The addition of
getId()method properly exposes the user's ID through the security principal, supporting the refactoring to usePrincipalDetailsinstead of explicitly passing user IDs. This centralized approach aligns well with Spring Security practices.src/main/java/naughty/tuzamate/domain/post/enums/BoardType.java (1)
1-5: LGTM! Well-defined enum for board categorization.The
BoardTypeenum is properly defined with clear values (FREE,INFO) that support the board categorization feature. The implementation follows Java naming conventions and is appropriately minimal.src/main/java/naughty/tuzamate/domain/post/entity/Post.java (2)
6-6: LGTM! Proper import for BoardType enum.The import statement correctly brings in the
BoardTypeenum for use in the entity.
33-34: LGTM! Proper enum field implementation.The
boardTypefield is correctly implemented with@Enumerated(EnumType.STRING)annotation, which will persist the enum values as strings in the database. This approach provides better readability and maintainability compared to ordinal values.src/main/java/naughty/tuzamate/domain/post/dto/PostResDTO.java (2)
23-25: LGTM! Enhanced PostPreviewDTO with useful fields.The addition of
authorandcommentNumfields enhances the post preview data with essential information. The Korean comment clearly explains the purpose of these fields, making the code more maintainable.
33-35: LGTM! Proper cursor-based pagination implementation.The modification to
PostPreviewListDTOcorrectly implements cursor-based pagination withnextCursorandhasNextfields. This aligns with the PR objectives and follows standard cursor pagination patterns for better performance and user experience.src/main/java/naughty/tuzamate/domain/post/service/query/PostQueryService.java (2)
4-4: LGTM: Added BoardType import for new functionality.The import is correctly added to support the new board type filtering capability.
9-9: LGTM: Interface updated for cursor-based pagination and board filtering.The method signature change properly defines the contract for cursor-based pagination with board type filtering. The parameters are well-named and follow standard pagination patterns.
src/main/java/naughty/tuzamate/domain/post/repository/PostRepository.java (1)
4-6: LGTM: Added necessary imports for cursor-based pagination.The imports for
BoardType,Pageable,Slice, andQueryare correctly added to support the new repository method.Also applies to: 8-8
src/main/java/naughty/tuzamate/domain/post/converter/PostConverter.java (3)
8-9: LGTM: Added imports for enhanced domain model.The imports for
BoardTypeandUserare correctly added to support the expanded post creation functionality.
18-18: LGTM: Enhanced post creation with board type and user context.The updated method signature and entity construction properly incorporate board type categorization and user association, aligning with the domain model enhancements.
Also applies to: 23-24
51-59: Verify if commented-out method removal is intentional.The
toPostPreviewListDTOmethod is commented out, which appears to be related to the cursor pagination refactoring. Please confirm this is intentional and the method is no longer needed.src/main/java/naughty/tuzamate/domain/post/service/query/PostQueryServiceImpl.java (1)
7-7: LGTM: Added necessary imports for cursor pagination implementation.The imports for
BoardType,PageRequest,Slice, andListare correctly added to support the new implementation.Also applies to: 11-12, 16-16
src/main/java/naughty/tuzamate/domain/post/service/command/PostCommandService.java (2)
3-3: LGTM: Added imports for enhanced authentication and board context.The imports for
PrincipalDetailsandBoardTypeare correctly added to support the improved API design with authenticated user context and board categorization.Also applies to: 7-7
11-11: LGTM: Enhanced interface design with proper authentication context.The method signature updates improve the API design by:
- Adding
BoardTypetocreatePostfor proper board categorization- Using
PrincipalDetailsinstead of simple user ID for better authentication context- Providing consistent parameter patterns across all user-dependent operations
These changes align well with the overall refactoring objectives and follow Spring Security best practices.
Also applies to: 14-17
src/main/java/naughty/tuzamate/domain/post/service/command/PostCommandServiceImpl.java (9)
4-4: Import addition looks good.The PrincipalDetails import is correctly added to support the authentication context refactoring.
9-9: BoardType import properly added.The BoardType enum import is correctly added to support the board categorization feature.
35-36: User retrieval pattern is consistent.The user retrieval using
userRepository.getReferenceById(principalDetails.getId())is consistent and efficient, using a reference instead of a full entity load since only the ID is needed for the relationship.
70-74: Authentication pattern correctly implemented.The postLike method properly uses PrincipalDetails to get the authenticated user ID, maintaining security by not relying on client-provided user IDs.
80-84: PostLike builder formatting improved.The PostLike builder has been reformatted for better readability with proper indentation and line breaks.
93-97: Consistent authentication pattern in deleteLike.The deleteLike method properly uses PrincipalDetails for user authentication, maintaining consistency with other methods.
108-112: Authentication pattern correctly applied to postScrap.The postScrap method uses PrincipalDetails consistently with other methods for secure user identification.
125-129: Consistent authentication in deleteScrap.The deleteScrap method properly implements the PrincipalDetails authentication pattern.
33-41: PostConverter.toPost signature correctly matches expected parametersThe
toPostmethod insrc/main/java/naughty/tuzamate/domain/post/converter/PostConverter.javais defined as:public static Post toPost(BoardType boardType, PostReqDTO.CreatePostRequestDTO reqDTO, User user)which aligns exactly with the call in
createPost. No further changes are needed.src/main/java/naughty/tuzamate/domain/post/controller/PostController.java (13)
3-7: Swagger and authentication imports properly added.The necessary imports for OpenAPI documentation and Spring Security authentication are correctly added.
11-11: BoardType enum import added.The BoardType enum import is correctly added to support board categorization.
16-16: Authentication principal import added.The @AuthenticationPrincipal import is correctly added for dependency injection of user authentication details.
22-22: API documentation tag added.The @tag annotation provides proper API documentation for the controller.
28-38: CreatePost endpoint properly refactored.The createPost endpoint has been correctly updated to include BoardType as a path variable and use PrincipalDetails for authentication. The API documentation is also properly added.
40-46: Single post endpoint updated with board context.The getPost endpoint now includes BoardType in the path, maintaining consistency with the board-centric URL structure.
48-57: Post list endpoint with pagination parameters.The getPostList endpoint properly includes cursor-based pagination parameters with appropriate validation (@max(30) for size). The BoardType path variable maintains consistency with the board-centric design.
59-67: Update endpoint includes board context.The updatePost endpoint maintains consistency with the board-centric URL structure by including BoardType in the path.
69-75: Delete endpoint updated with board context.The deletePost endpoint properly includes BoardType in the path for consistency.
77-85: Like endpoint uses authentication principal.The like endpoint correctly uses PrincipalDetails for authentication instead of explicit user ID parameters, improving security.
87-96: Delete like endpoint properly secured.The deleteLike endpoint uses PrincipalDetails for secure user authentication.
98-107: Scrap endpoint uses authentication principal.The postScrap endpoint correctly implements PrincipalDetails authentication for security.
109-118: Delete scrap endpoint properly secured.The deleteScrap endpoint uses PrincipalDetails for secure authentication.
| @@ -1,5 +1,7 @@ | |||
| package naughty.tuzamate.domain.post.dto; | |||
|
|
|||
| import naughty.tuzamate.domain.post.enums.BoardType; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify if BoardType import is needed.
The BoardType enum is imported but not used in any of the DTO records. Ensure this import is necessary or remove it if unused to keep the code clean.
🏁 Script executed:
#!/bin/bash
# Description: Check if BoardType is used in PostReqDTO or related validation
# Expected: Find usage of BoardType in PostReqDTO context
# Search for BoardType usage in the file
rg -A 5 -B 5 "BoardType" src/main/java/naughty/tuzamate/domain/post/dto/PostReqDTO.java
# Search for validation annotations or other usage patterns
rg -A 3 -B 3 "@Valid|@NotNull|@Validated" src/main/java/naughty/tuzamate/domain/post/dto/PostReqDTO.javaLength of output: 384
Remove unused BoardType import in PostReqDTO
The BoardType enum is imported in PostReqDTO.java but isn’t referenced anywhere in that file. Please remove the unused import to keep the code clean.
• File to update:
src/main/java/naughty/tuzamate/domain/post/dto/PostReqDTO.java
• Suggested diff:
--- a/src/main/java/naughty/tuzamate/domain/post/dto/PostReqDTO.java
+++ b/src/main/java/naughty/tuzamate/domain/post/dto/PostReqDTO.java
@@
-import naughty.tuzamate.domain.post.enums.BoardType;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| import naughty.tuzamate.domain.post.enums.BoardType; |
🤖 Prompt for AI Agents
In src/main/java/naughty/tuzamate/domain/post/dto/PostReqDTO.java at line 3, the
import statement for BoardType is unused. Remove the line importing
naughty.tuzamate.domain.post.enums.BoardType to clean up the code and avoid
unnecessary imports.
feat : BoardType ENUM 속성 POST에 추가, refactor : postController,Service,Repository,Converter 코드를 커서 페이지네이션 기반으로 수정
refactor : PostController, PostService, PostRepository, PostConverter 커서 페이지네이션에 맞게 코드 리팩토링
📍 PR 타입 (하나 이상 선택)
🏷️ 관련 이슈
Close #44
📌 개요
🔁 변경 사항
📸 스크린샷
✅ 체크리스트
💡 추가 사항 (리뷰어가 참고해야 할 것)
Summary by CodeRabbit
New Features
Improvements