using Application.Abstractions.Data; using SharedKernel.Storage; using MediatR; using Microsoft.EntityFrameworkCore; namespace Application.Features.Banner.Item.Update; public sealed class Handler(IAppDbContext db, IFileStorage fileStorage) : IRequestHandler { public async Task Handle(Command request, CancellationToken ct) { var bannerItem = await db.BannerItem.FirstOrDefaultAsync(x => x.ID == request.ID, ct); if (bannerItem is null) { throw new KeyNotFoundException("¹è³Ê¸¦ ãÀ» ¼ö ¾ø½À´Ï´Ù."); } if (!await db.BannerPosition.AnyAsync(x => x.ID == request.PositionID, ct)) { throw new KeyNotFoundException("¹è³Ê À§Ä¡¸¦ ãÀ» ¼ö ¾ø½À´Ï´Ù."); } FileStoragePath uploadPath = new FileStoragePath(UploadTarget.Upload, UploadFolder.Banner, bannerItem.ID); string[] allowedFileExtensions = [".jpg", ".jpeg", ".png", ".gif", ".webp"]; string? desktopImgPath = bannerItem.DesktopImage; string? mobileImgPath = bannerItem.MobileImage; if (request.DesktopImage is not null) { if (!string.IsNullOrEmpty(bannerItem.DesktopImage)) { fileStorage.DeleteByUrl(bannerItem.DesktopImage); } desktopImgPath = (await fileStorage.SaveFileAsync(request.DesktopImage, uploadPath, allowedFileExtensions, ct))?.Url; } if (request.MobileImage is not null) { if (!string.IsNullOrEmpty(bannerItem.MobileImage)) { fileStorage.DeleteByUrl(bannerItem.MobileImage); } mobileImgPath = (await fileStorage.SaveFileAsync(request.MobileImage, uploadPath, allowedFileExtensions, ct))?.Url; } bannerItem.Update( request.Subject, desktopImgPath, mobileImgPath, request.Link, request.Order, request.IsActive, request.StartAt, request.EndAt ); await db.SaveChangesAsync(ct); } }