-
Notifications
You must be signed in to change notification settings - Fork 27
/
OpenAccountUseCase.cs
47 lines (40 loc) · 1.92 KB
/
OpenAccountUseCase.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
using MediatR;
using Optivem.Kata.Banking.Core.Domain.BankAccounts;
using Optivem.Kata.Banking.Core.Domain.Time;
using Optivem.Kata.Banking.Core.Exceptions;
namespace Optivem.Kata.Banking.Core.UseCases.OpenAccount
{
public class OpenAccountUseCase : IRequestHandler<OpenAccountRequest, OpenAccountResponse>
{
private readonly IAccountIdGenerator _accountIdGenerator;
private readonly IAccountNumberGenerator _accountNumberGenerator;
private readonly IDateTimeService _dateTimeService;
private readonly IBankAccountRepository _bankAccountRepository;
public OpenAccountUseCase(IAccountIdGenerator accountIdGenerator,
IAccountNumberGenerator accountNumberGenerator,
IDateTimeService dateTimeService,
IBankAccountRepository bankAccountRepository)
{
_accountIdGenerator = accountIdGenerator;
_accountNumberGenerator = accountNumberGenerator;
_dateTimeService = dateTimeService;
_bankAccountRepository = bankAccountRepository;
}
public Task<OpenAccountResponse> Handle(OpenAccountRequest request, CancellationToken cancellationToken)
{
var accountHolderName = AccountHolderName.From(request.FirstName, request.LastName);
var balance = Balance.From(request.Balance);
var accountId = _accountIdGenerator.Next();
var accountNumber = _accountNumberGenerator.Next();
var openingDateTime = _dateTimeService.Now();
var openingDate = DateOnly.FromDateTime(openingDateTime);
var bankAccount = new BankAccount(accountId, accountNumber, accountHolderName, openingDate, balance);
_bankAccountRepository.Add(bankAccount);
var response = new OpenAccountResponse
{
AccountNumber = accountNumber.Value,
};
return Task.FromResult(response);
}
}
}