Handler.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Domain.Entities.Common.ValueObject;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Admin.Member.Wallet.List.Charge;
  6. public sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
  7. {
  8. public async Task Handle(Command request, CancellationToken ct)
  9. {
  10. var wallet = await db.Wallet.Include(x => x.Balances).Include(x => x.Transactions).FirstOrDefaultAsync(x => x.ID == request.WalletID, ct);
  11. if (wallet is null)
  12. {
  13. throw new KeyNotFoundException("지갑을 찾을 수 없습니다.");
  14. }
  15. var amount = Money.KRW(Math.Abs(request.Amount));
  16. var reason = request.Amount > 0 ? "관리자 충전" : "관리자 차감";
  17. if (request.Amount > 0)
  18. {
  19. wallet.AdjustIncrease(amount, reason, memo: request.Memo);
  20. }
  21. else if (request.Amount < 0)
  22. {
  23. wallet.AdjustDecrease(amount, reason, memo: request.Memo);
  24. }
  25. else
  26. {
  27. throw new ArgumentException("충전 금액을 입력해주세요.");
  28. }
  29. await db.SaveChangesAsync(ct);
  30. }
  31. }