Class PostServiceImpl
java.lang.Object
com.example.blogs.app.api.post.service.PostServiceImpl
- All Implemented Interfaces:
PostService
Orchestrates post retrieval operations by coordinating with the post repository adapter.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreatePost(long authorId, PostCreateRequestDTO requestDTO, MultipartFile previewImage) Creates a new post with a preview image and generates a unique slug.voiddeletePostById(Long postId) Deletes a post by its ID.getPostBySlug(String slug) Retrieves a post by its unique slug identifier with associated comments.getPostsByUserId(long userId) Retrieves all posts created by the specified user.updatePostById(long postId, PostUpdateRequestDTO requestDTO, MultipartFile previewImage) Updates a post by its ID with partial field updates.
-
Constructor Details
-
PostServiceImpl
public PostServiceImpl()
-
-
Method Details
-
getPostsByUserId
Retrieves all posts created by the specified user. Delegates to the repository adapter for data retrieval.- Specified by:
getPostsByUserIdin interfacePostService- Parameters:
userId- the ID of the user- Returns:
- list of posts created by the user
-
deletePostById
Deletes a post by its ID. Delegates to the repository adapter for deletion.- Specified by:
deletePostByIdin interfacePostService- Parameters:
postId- the ID of the post to delete
-
getPostBySlug
Retrieves a post by its unique slug identifier with associated comments. Delegates to the repository adapter for post retrieval and comment service for comments.- Specified by:
getPostBySlugin interfacePostService- Parameters:
slug- the unique slug of the post- Returns:
- post details with associated comments
-
updatePostById
@Transactional public PostUpdateResponseDTO updatePostById(long postId, PostUpdateRequestDTO requestDTO, MultipartFile previewImage) Updates a post by its ID with partial field updates. Regenerates slug when title is updated to maintain URL consistency.- Specified by:
updatePostByIdin interfacePostService- Parameters:
postId- the ID of the post to updaterequestDTO- the update request containing fields to updatepreviewImage- the new preview image file to upload (optional)- Returns:
- updated post details with new timestamp
-
createPost
@Transactional public PostCreateResponseDTO createPost(long authorId, PostCreateRequestDTO requestDTO, MultipartFile previewImage) Creates a new post with a preview image and generates a unique slug. Uploads the preview image to storage, saves the post entity, and returns the complete post details.- Specified by:
createPostin interfacePostService- Parameters:
authorId- the ID of the user creating the postrequestDTO- the post creation request containing title, description, and contentpreviewImage- the preview image file to upload- Returns:
- created post details with generated slug and preview image URL
- Throws:
FailedToCreatePostException- if file upload or post creation fails
-