Handler.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. using Application.Abstractions.Data;
  2. using Domain.Entities.Common.ValueObject;
  3. using MediatR;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Member.Wallet.List.Charge;
  6. public sealed class Handler(IAppDbContext db) : IRequestHandler<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) throw new KeyNotFoundException("지갑을 찾을 수 없습니다.");
  12. var amount = Money.KRW(Math.Abs(request.Amount));
  13. var reason = request.Amount > 0 ? "관리자 충전" : "관리자 차감";
  14. if (request.Amount > 0)
  15. {
  16. wallet.AdjustIncrease(amount, reason, memo: request.Memo);
  17. }
  18. else if (request.Amount < 0)
  19. {
  20. wallet.AdjustDecrease(amount, reason, memo: request.Memo);
  21. }
  22. else
  23. {
  24. throw new ArgumentException("충전 금액을 입력해주세요.");
  25. }
  26. await db.SaveChangesAsync(ct);
  27. }
  28. }