using CMSMicroservice.Application.Common.FileManager; using CMSMicroservice.Application.Common.Interfaces; using CMSMicroservice.Domain.Entities.Blog; using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace CMSMicroservice.Application.BlogPostCQ.Commands.UpdateBlogPost; public class UpdateBlogPostCommandHandler : IRequestHandler { private readonly IApplicationDbContext _context; private readonly IFileManager _fileManager; private readonly ILogger _logger; public UpdateBlogPostCommandHandler( IApplicationDbContext context, IFileManager fileManager, ILogger logger) { _context = context; _fileManager = fileManager; _logger = logger; } public async Task Handle(UpdateBlogPostCommand request, CancellationToken cancellationToken) { var post = await _context.BlogPosts.FirstOrDefaultAsync(x => x.Id == request.Id && !x.IsDeleted, cancellationToken) ?? throw new KeyNotFoundException($"مقاله با شناسه {request.Id} یافت نشد"); post.Title = request.Title.Trim(); // حفظ slug قبلی اگر مقدار جدید خالی باشد if (!string.IsNullOrWhiteSpace(request.Slug)) post.Slug = request.Slug.Trim().ToLower(); post.Summary = request.Summary?.Trim(); post.HtmlContent = request.HtmlContent; post.FeaturedImagePath = request.FeaturedImagePath; post.FeaturedImageThumbnailPath = request.FeaturedImageThumbnailPath; post.IsFeatured = request.IsFeatured; post.SortOrder = request.SortOrder; // آپلود تصویر شاخص (اگر فایل جدید ارسال شده باشد) if (request.ImageFileBytes is { Length: > 0 }) { var result = await _fileManager.UploadImageAsync( "Images/BlogPosts", request.ImageFileBytes, request.ImageFileMime ?? "image/jpeg", request.ImageFileName, cancellationToken); post.FeaturedImagePath = result.Main.Path; post.FeaturedImageThumbnailPath = result.Thumbnail.Path; } // بروزرسانی دسته‌بندی‌ها var existingCategories = await _context.BlogPostCategories .Where(x => x.BlogPostId == post.Id).ToListAsync(cancellationToken); _context.BlogPostCategories.RemoveRange(existingCategories); foreach (var categoryId in request.CategoryIds) { _context.BlogPostCategories.Add(new BlogPostCategory { BlogPostId = post.Id, BlogCategoryId = categoryId }); } // بروزرسانی تگ‌ها var existingTags = await _context.BlogPostTags .Where(x => x.BlogPostId == post.Id).ToListAsync(cancellationToken); _context.BlogPostTags.RemoveRange(existingTags); foreach (var tagId in request.TagIds) { _context.BlogPostTags.Add(new BlogPostTag { BlogPostId = post.Id, TagId = tagId }); } await _context.SaveChangesAsync(cancellationToken); _logger.LogInformation("Blog post updated. Id: {Id}, Title: {Title}", post.Id, post.Title); return Unit.Value; } }