-
-
Notifications
You must be signed in to change notification settings - Fork 677
/
Withdraw.cs
76 lines (69 loc) · 2.63 KB
/
Withdraw.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
namespace Application.UseCases
{
using System.Threading.Tasks;
using Application.Boundaries.Withdraw;
using Application.Services;
using Domain.Accounts;
using Domain.Accounts.Debits;
using Domain.Accounts.ValueObjects;
/// <summary>
/// Withdraw <see href="https://github.com/ivanpaulovich/clean-architecture-manga/wiki/Domain-Driven-Design-Patterns#use-case">Use Case Domain-Driven Design Pattern</see>.
/// </summary>
public sealed class Withdraw : IUseCase
{
private readonly AccountService _accountService;
private readonly IOutputPort _outputPort;
private readonly IAccountRepository _accountRepository;
private readonly IUnitOfWork _unitOfWork;
/// <summary>
/// Initializes a new instance of the <see cref="Withdraw"/> class.
/// </summary>
/// <param name="accountService">Account Service.</param>
/// <param name="outputPort">Output Port.</param>
/// <param name="accountRepository">Account Repository.</param>
/// <param name="unitOfWork">Unit Of Work.</param>
public Withdraw(
AccountService accountService,
IOutputPort outputPort,
IAccountRepository accountRepository,
IUnitOfWork unitOfWork)
{
this._accountService = accountService;
this._outputPort = outputPort;
this._accountRepository = accountRepository;
this._unitOfWork = unitOfWork;
}
/// <summary>
/// Executes the Use Case.
/// </summary>
/// <param name="input">Input Message.</param>
/// <returns>Task.</returns>
public async Task Execute(WithdrawInput input)
{
try
{
var account = await this._accountRepository.Get(input.AccountId);
var debit = await this._accountService.Withdraw(account, input.Amount);
await this._unitOfWork.Save();
this.BuildOutput(debit, account);
}
catch (AccountNotFoundException notFoundEx)
{
this._outputPort.NotFound(notFoundEx.Message);
return;
}
catch (MoneyShouldBePositiveException outOfBalanceEx)
{
this._outputPort.OutOfBalance(outOfBalanceEx.Message);
return;
}
}
private void BuildOutput(IDebit debit, IAccount account)
{
var output = new WithdrawOutput(
debit,
account.GetCurrentBalance());
this._outputPort.Standard(output);
}
}
}