using Application.Abstractions.Messaging; using Application.Abstractions.Data; using SharedKernel.Storage; using Microsoft.EntityFrameworkCore; namespace Application.Features.Admin.Forum.Post.Update; public sealed class Handler(IAppDbContext db, IFileStorage fileStorage) : ICommandHandler { public async Task Handle(Command request, CancellationToken ct) { var post = await db.Post.FirstOrDefaultAsync(x => x.ID == request.ID, ct); if (post is null) { throw new KeyNotFoundException("게시글을 찾을 수 없습니다."); } post.Subject = request.Subject; post.Content = request.Content ?? string.Empty; post.IsNotice = request.IsNotice; post.IsSecret = request.IsSecret; post.IsAnonymous = request.IsAnonymous; post.UpdatedAt = DateTime.UtcNow; if (request.ThumbnailFile is not null) { FileStoragePath uploadPath = new(UploadTarget.Upload, UploadFolder.Post, post.ID); string[] allowedFileExtensions = [".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"]; if (!string.IsNullOrEmpty(post.Thumbnail)) { fileStorage.DeleteByUrl(post.Thumbnail); } var result = await fileStorage.SaveFileAsync(request.ThumbnailFile, uploadPath, allowedFileExtensions, ct); post.Thumbnail = result?.Url; } await db.SaveChangesAsync(ct); } }