using CMSMicroservice.Application.Common.Interfaces; using CMSMicroservice.Domain.Enums; using MediatR; using Microsoft.EntityFrameworkCore; namespace CMSMicroservice.Application.BlogPostCQ.Queries.GetBlogPost; public class GetBlogPostQueryHandler : IRequestHandler { private readonly IApplicationDbContext _context; public GetBlogPostQueryHandler(IApplicationDbContext context) { _context = context; } public async Task Handle(GetBlogPostQuery 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.Id == request.Id && !x.IsDeleted, cancellationToken) ?? throw new KeyNotFoundException($"مقاله با شناسه {request.Id} یافت نشد"); 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 = 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() }; } public static string GetStatusName(BlogPostStatus status) => status switch { BlogPostStatus.Draft => "پیش‌نویس", BlogPostStatus.Published => "منتشرشده", BlogPostStatus.Scheduled => "زمانبندی‌شده", BlogPostStatus.Archived => "آرشیو", _ => "نامشخص" }; }