Table of Contents

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
All write operations require admin authorization (enforced at controller level).

Constructors

BlogService(IBlogRepository, IObjectStorage, ILogger<BlogService>)

Initializes a new instance of the BlogService class.

public BlogService(IBlogRepository blogRepository, IObjectStorage objectStorage, ILogger<BlogService> logger)

Parameters

blogRepository IBlogRepository

Repository for blog post data access operations.

objectStorage IObjectStorage

Service for object storage operations including presigned URL generation.

logger ILogger<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

request CreateBlogPostRequest

The blog post creation request.

createdByUserId Guid

The unique identifier of the user creating the post.

cancellationToken CancellationToken

Cancellation 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

id Guid

The unique identifier of the blog post to delete.

cancellationToken CancellationToken

Cancellation token to cancel the operation.

Returns

Task<bool>

True if the post was deleted; otherwise, false.

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

fileExtension string

The file extension of the image (e.g., "jpg", "png").

contentType string

The MIME type of the image (e.g., "image/jpeg", "image/png").

cancellationToken CancellationToken

Cancellation 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

publishedOnly bool

If true, returns only published posts; otherwise returns all posts.

cancellationToken CancellationToken

Cancellation 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

id Guid

The unique identifier of the blog post to retrieve.

cancellationToken CancellationToken

Cancellation 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

slug string

The slug of the blog post to retrieve.

cancellationToken CancellationToken

Cancellation 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

id Guid

The unique identifier of the blog post to update.

request UpdateBlogPostRequest

The blog post update request.

cancellationToken CancellationToken

Cancellation token to cancel the operation.

Returns

Task<BlogPostDetailsDto>

The updated blog post details if found; otherwise, null.