This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
CheckingAccountTransfer.cs
63 lines (49 loc) · 2.66 KB
/
CheckingAccountTransfer.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
using System;
using Saga.Common.Messaging;
using Saga.Participants.Transfer.Factories;
using System.Collections.Generic;
namespace Saga.Participants.Transfer.Models
{
public class CheckingAccountTransfer
{
private readonly List<CheckingAccountLine> operations;
public IEnumerable<CheckingAccountLine> CheckingAccountLines => operations.AsReadOnly();
public CheckingAccountTransfer()
{
operations = new List<CheckingAccountLine>();
}
public Event TranferAmountBetweenAccounts(string transactionId, string fromAccountId, string toAccountId, decimal amount)
{
Validate(transactionId, fromAccountId, toAccountId, amount);
var debitFrom = new CheckingAccountLine(transactionId, fromAccountId, -amount, GetTransferingDescription(toAccountId));
operations.Add(debitFrom);
var creditTo = new CheckingAccountLine(transactionId, toAccountId, +amount, GetTransferingDescription(fromAccountId));
operations.Add(creditTo);
return TransferServiceEventFactory.BuildTransferSucceededEvent(transactionId);
}
private string GetTransferingDescription(string accountId)
{
return string.Format(ConstantStrings.TransferingMessage, accountId);
}
public Event CancelTranferAmountBetweenAccounts(string transactionId, string fromAccountId, string toAccountId, decimal amount)
{
Validate(transactionId, fromAccountId, toAccountId, amount);
var creditFrom = new CheckingAccountLine(transactionId, fromAccountId, +amount, GetRevertingMessage());
operations.Add(creditFrom);
var debitTo = new CheckingAccountLine(transactionId, toAccountId, -amount, GetRevertingMessage());
operations.Add(debitTo);
return TransferServiceEventFactory.BuildTransferCanceledEvent(transactionId);
}
private string GetRevertingMessage()
{
return ConstantStrings.RevertingMessage;
}
private void Validate(string transactionId, string fromAccountId, string toAccountId, decimal amount)
{
if (string.IsNullOrWhiteSpace(transactionId)) throw new NullReferenceException(nameof(transactionId));
if (string.IsNullOrWhiteSpace(fromAccountId)) throw new NullReferenceException(nameof(fromAccountId));
if (string.IsNullOrWhiteSpace(toAccountId)) throw new NullReferenceException(nameof(toAccountId));
if (amount <= 0) throw new ArgumentException("Amount needs to be greater than 0", nameof(amount));
}
}
}