Handler.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Application.Abstractions.Cache;
  4. using Domain.Entities.Crypto;
  5. using SharedKernel.Storage;
  6. using Microsoft.EntityFrameworkCore;
  7. namespace Application.Features.Admin.Crypto.List.Create
  8. {
  9. public sealed class Handler(IAppDbContext db, IFileStorage fileStorage, ICacheService cache) : ICommandHandler<Command>
  10. {
  11. public async Task Handle(Command request, CancellationToken ct)
  12. {
  13. if (await db.Coin.AnyAsync(c => c.Symbol == request.Symbol.ToUpper(), ct))
  14. {
  15. throw new InvalidOperationException($"`{request.Symbol.ToUpper()}`는 이미 등록된 심볼입니다.");
  16. }
  17. var coin = Coin.Create(
  18. request.Symbol,
  19. request.KorName,
  20. request.EngName,
  21. request.Description,
  22. request.ContractAddress,
  23. request.WebsiteUrl,
  24. request.WhitepaperUrl,
  25. request.TwitterUrl,
  26. request.TelegramUrl,
  27. request.IsActive,
  28. request.IsWarning,
  29. request.IsNew,
  30. request.IsDelisted
  31. );
  32. await db.Coin.AddAsync(coin, ct);
  33. await db.SaveChangesAsync(ct);
  34. FileStoragePath uploadPath = new FileStoragePath(UploadTarget.Upload, UploadFolder.Crypto, coin.ID);
  35. string[] allowedFileExtensions = [".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg"];
  36. if (request.LogoImageFile is not null)
  37. {
  38. coin.SetLogoImage(
  39. (await fileStorage.SaveFileAsync(request.LogoImageFile, uploadPath, allowedFileExtensions, ct))?.Url
  40. );
  41. }
  42. foreach (var categoryID in request.CategoryIDs)
  43. {
  44. db.CoinCategoryMap.Add(new CoinCategoryMap { CoinID = coin.ID, CategoryID = categoryID });
  45. }
  46. await db.SaveChangesAsync(ct);
  47. await cache.RemoveByPrefixAsync("crypto:", ct);
  48. }
  49. }
  50. }