Class BlogService
- Namespace
- Builvero.Application.Services
- Assembly
- Builvero.Application.dll
Provides business logic for blog post management including creation, retrieval, updates, deletion, and image upload URL generation.
public class BlogService : IBlogService
- Inheritance
-
BlogService
- Implements
- Inherited Members
Remarks
This service handles all blog-related operations:
- Blog post CRUD operations with slug generation and uniqueness validation
- HTML content sanitization to prevent XSS attacks
- Publication status management (draft vs published)
- Presigned URL generation for blog image uploads
Constructors
BlogService(IBlogRepository, IObjectStorage, ILogger<BlogService>)
Initializes a new instance of the BlogService class.
public BlogService(IBlogRepository blogRepository, IObjectStorage objectStorage, ILogger<BlogService> logger)
Parameters
blogRepositoryIBlogRepositoryRepository for blog post data access operations.
objectStorageIObjectStorageService for object storage operations including presigned URL generation.
loggerILogger<BlogService>Logger for recording service operations and errors.
Methods
CreatePostAsync(CreateBlogPostRequest, Guid, CancellationToken)
Creates a new blog post.
public Task<BlogPostDetailsDto> CreatePostAsync(CreateBlogPostRequest request, Guid createdByUserId, CancellationToken cancellationToken = default)
Parameters
requestCreateBlogPostRequestThe blog post creation request.
createdByUserIdGuidThe unique identifier of the user creating the post.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task<BlogPostDetailsDto>
The created blog post details.
DeletePostAsync(Guid, CancellationToken)
Deletes a blog post.
public Task<bool> DeletePostAsync(Guid id, CancellationToken cancellationToken = default)
Parameters
idGuidThe unique identifier of the blog post to delete.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
GenerateImageUploadUrlAsync(string, string, CancellationToken)
Generates a presigned URL for uploading a blog image to S3.
public Task<BlogImageUploadResponse> GenerateImageUploadUrlAsync(string fileExtension, string contentType, CancellationToken cancellationToken = default)
Parameters
fileExtensionstringThe file extension of the image (e.g., "jpg", "png").
contentTypestringThe MIME type of the image (e.g., "image/jpeg", "image/png").
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task<BlogImageUploadResponse>
The upload URL, object key, and read URL for the image.
GetAllPostsAsync(bool, CancellationToken)
Retrieves all blog posts, optionally filtered by publication status.
public Task<List<BlogPostSummaryDto>> GetAllPostsAsync(bool publishedOnly = false, CancellationToken cancellationToken = default)
Parameters
publishedOnlyboolIf true, returns only published posts; otherwise returns all posts.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task<List<BlogPostSummaryDto>>
A list of blog post summaries ordered by creation date (newest first).
GetPostByIdAsync(Guid, CancellationToken)
Retrieves a blog post by its unique identifier.
public Task<BlogPostDetailsDto?> GetPostByIdAsync(Guid id, CancellationToken cancellationToken = default)
Parameters
idGuidThe unique identifier of the blog post to retrieve.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task<BlogPostDetailsDto>
The blog post details if found; otherwise, null.
GetPostBySlugAsync(string, CancellationToken)
Retrieves a published blog post by its slug.
public Task<BlogPostDetailsDto?> GetPostBySlugAsync(string slug, CancellationToken cancellationToken = default)
Parameters
slugstringThe slug of the blog post to retrieve.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task<BlogPostDetailsDto>
The blog post details if found and published; otherwise, null.
UpdatePostAsync(Guid, UpdateBlogPostRequest, CancellationToken)
Updates an existing blog post.
public Task<BlogPostDetailsDto?> UpdatePostAsync(Guid id, UpdateBlogPostRequest request, CancellationToken cancellationToken = default)
Parameters
idGuidThe unique identifier of the blog post to update.
requestUpdateBlogPostRequestThe blog post update request.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task<BlogPostDetailsDto>
The updated blog post details if found; otherwise, null.