Handler.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Application.Abstractions.Cache;
  4. using Domain.Entities.Common;
  5. using Microsoft.EntityFrameworkCore;
  6. namespace Application.Features.Admin.Crypto.TickerConfig.Save;
  7. public sealed class Handler(IAppDbContext db, ICacheService cache) : ICommandHandler<Command>
  8. {
  9. public async Task Handle(Command request, CancellationToken ct)
  10. {
  11. var config = await db.Config.OrderByDescending(x => x.ID).FirstOrDefaultAsync(ct);
  12. if (config is null)
  13. {
  14. config = Domain.Entities.Common.Config.Create();
  15. db.Config.Add(config);
  16. }
  17. var crypto = new CryptoConfig
  18. {
  19. TickerRefreshSeconds = request.TickerRefreshSeconds,
  20. SurgeThreshold = request.SurgeThreshold,
  21. PlungeThreshold = request.PlungeThreshold,
  22. MainPageCoinCount = request.MainPageCoinCount
  23. };
  24. config.Update(
  25. config.Basic,
  26. config.Images,
  27. config.Meta,
  28. config.Company,
  29. config.Account,
  30. config.EmailTemplate,
  31. config.External,
  32. config.Payment,
  33. crypto
  34. );
  35. await db.SaveChangesAsync(ct);
  36. await cache.RemoveAsync(CacheKeys.Config, ct);
  37. }
  38. }