using CMSMicroservice.Application.BlogPostCQ.Queries.GetBlogPost; using CMSMicroservice.Application.Common.Interfaces; using CMSMicroservice.Domain.Enums; using MediatR; using Microsoft.EntityFrameworkCore; namespace CMSMicroservice.Application.BlogPostCQ.Queries.GetBlogPostBySlug; public class GetBlogPostBySlugQueryHandler : IRequestHandler { private readonly IApplicationDbContext _context; public GetBlogPostBySlugQueryHandler(IApplicationDbContext context) { _context = context; } public async Task Handle(GetBlogPostBySlugQuery request, CancellationToken cancellationToken) { var post = await _context.BlogPosts .Include(x => x.BlogPostCategories).ThenInclude(x => x.BlogCategory) .Include(x => x.BlogPostTags).ThenInclude(x => x.Tag) .FirstOrDefaultAsync(x => x.Slug == request.Slug && !x.IsDeleted, cancellationToken) ?? throw new KeyNotFoundException($"مقاله با نشانی '{request.Slug}' یافت نشد"); return new BlogPostDto { Id = post.Id, Title = post.Title, Slug = post.Slug, Summary = post.Summary, HtmlContent = post.HtmlContent, FeaturedImagePath = post.FeaturedImagePath, FeaturedImageThumbnailPath = post.FeaturedImageThumbnailPath, Status = post.Status, StatusName = GetBlogPostQueryHandler.GetStatusName(post.Status), PublishedAt = post.PublishedAt, ViewCount = post.ViewCount, AuthorUserId = post.AuthorUserId, IsFeatured = post.IsFeatured, SortOrder = post.SortOrder, Created = post.Created, LastModified = post.LastModified, Categories = post.BlogPostCategories.Select(c => new BlogPostCategoryDto { Id = c.BlogCategory.Id, Title = c.BlogCategory.Title, Slug = c.BlogCategory.Slug }).ToList(), Tags = post.BlogPostTags.Select(t => new BlogPostTagDto { Id = t.Tag.Id, Title = t.Tag.Title, Name = t.Tag.Name }).ToList() }; } }