Handler.cs 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using SharedKernel;
  4. using SharedKernel.Results;
  5. using Microsoft.EntityFrameworkCore;
  6. namespace Application.Features.Api.DonationAlert.ToggleActive;
  7. internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result<Response>>
  8. {
  9. public async Task<Result<Response>> Handle(Command r, CancellationToken ct)
  10. {
  11. var config = await db.DonationAlertConfig
  12. .FirstOrDefaultAsync(c => c.ID == r.ID && c.ChannelID == r.ChannelID, ct);
  13. if (config is null)
  14. {
  15. return Result.Failure<Response>(Error.NotFound("DonationAlertConfig.NotFound", "설정을 찾을 수 없습니다."));
  16. }
  17. if (config.IsActive == r.IsActive)
  18. {
  19. return Result.Success(new Response(config.ID, config.IsActive));
  20. }
  21. config.SetActive(r.IsActive);
  22. await db.SaveChangesAsync(ct);
  23. return Result.Success(new Response(config.ID, config.IsActive));
  24. }
  25. }