Class PostServiceImpl

java.lang.Object
com.example.blogs.app.api.post.service.PostServiceImpl
All Implemented Interfaces:
PostService

@Service public class PostServiceImpl extends Object implements PostService
Orchestrates post retrieval operations by coordinating with the post repository adapter.
  • Constructor Details

    • PostServiceImpl

      public PostServiceImpl()
  • Method Details

    • getPostsByUserId

      public List<PostEntity> getPostsByUserId(long userId)
      Retrieves all posts created by the specified user. Delegates to the repository adapter for data retrieval.
      Specified by:
      getPostsByUserId in interface PostService
      Parameters:
      userId - the ID of the user
      Returns:
      list of posts created by the user
    • deletePostById

      public void deletePostById(Long postId)
      Deletes a post by its ID. Delegates to the repository adapter for deletion.
      Specified by:
      deletePostById in interface PostService
      Parameters:
      postId - the ID of the post to delete
    • getPostBySlug

      @Transactional(readOnly=true) public PostDTO getPostBySlug(String slug)
      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:
      getPostBySlug in interface PostService
      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:
      updatePostById in interface PostService
      Parameters:
      postId - the ID of the post to update
      requestDTO - the update request containing fields to update
      previewImage - 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:
      createPost in interface PostService
      Parameters:
      authorId - the ID of the user creating the post
      requestDTO - the post creation request containing title, description, and content
      previewImage - 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