| | 1 | | using Domain.Common; |
| | 2 | |
|
| | 3 | | namespace Domain.Currencies |
| | 4 | | { |
| | 5 | | public record CurrencySnapshot |
| | 6 | | { |
| 12 | 7 | | protected internal CurrencySnapshot() { } |
| 1 | 8 | | private static Error AmountCannotBeLessThanZeroError = new Error(ErrorCode.BadInput, "The amount cannot be less |
| 1 | 9 | | private static Error IlligalConversionError = new Error(ErrorCode.BadInput, "The requested currency code is not |
| | 10 | |
|
| 1 | 11 | | public static readonly IReadOnlyCollection<CurrencyCode> IllegalConversions = new[] |
| 1 | 12 | | { |
| 1 | 13 | | CurrencyCode.Try, |
| 1 | 14 | | CurrencyCode.Pln, |
| 1 | 15 | | CurrencyCode.Thb, |
| 1 | 16 | | CurrencyCode.Mxn |
| 1 | 17 | | }; |
| | 18 | |
|
| 101 | 19 | | private CurrencySnapshot(CurrencyCode code, DateTime dateCaptured, List<Money> exchanges) |
| 101 | 20 | | { |
| 101 | 21 | | Code = code; |
| 101 | 22 | | DateCaptured = dateCaptured; |
| 101 | 23 | | ExchangeRates = exchanges; |
| 101 | 24 | | } |
| 151 | 25 | | public CurrencyCode Code { get; init; } |
| 172 | 26 | | public DateTime DateCaptured { get; init; } |
| 183 | 27 | | public List<Money> ExchangeRates { get; set; } |
| | 28 | |
|
| | 29 | | public static Result<CurrencySnapshot> Create( |
| | 30 | | string code, |
| | 31 | | DateTime dateCaptured, |
| | 32 | | List<(string Code, decimal Amount)> exchangeRates) |
| 103 | 33 | | { |
| 103 | 34 | | var currencyCodeResult = CurrencyCode.FromCode(code); |
| 103 | 35 | | if (currencyCodeResult.IsFailure) |
| 1 | 36 | | { |
| 1 | 37 | | return Result.Failure<CurrencySnapshot>(currencyCodeResult.Error); |
| | 38 | | } |
| | 39 | |
|
| 337 | 40 | | if (exchangeRates.Any(x => x.Amount < 0)) |
| 1 | 41 | | { |
| 1 | 42 | | return Result.Failure<CurrencySnapshot>(AmountCannotBeLessThanZeroError); |
| | 43 | | } |
| 335 | 44 | | var exchangeResults = exchangeRates.Select(x => new |
| 335 | 45 | | { |
| 335 | 46 | | Result = CurrencyCode.FromCode(x.Code), |
| 335 | 47 | | Original = x |
| 335 | 48 | | }).ToList(); |
| | 49 | |
|
| 335 | 50 | | var exchanges = exchangeResults.Where(x=>x.Result.IsSuccess) |
| 333 | 51 | | .Select(x => new Money(x.Result.Value, x.Original.Amount)).ToList(); |
| 101 | 52 | | return new CurrencySnapshot(currencyCodeResult.Value, dateCaptured, exchanges); |
| 103 | 53 | | } |
| | 54 | |
|
| | 55 | | public Result<Money> Convert(decimal amount, CurrencyCode currencyCode) |
| 23 | 56 | | { |
| 23 | 57 | | if (!IsLegalConversion(currencyCode)) |
| 4 | 58 | | { |
| 4 | 59 | | return Result.Failure<Money>(IlligalConversionError); |
| | 60 | | } |
| 44 | 61 | | var exchangeRate = ExchangeRates.FirstOrDefault(x => x.Code == currencyCode); |
| 19 | 62 | | if (exchangeRate == null) |
| 2 | 63 | | { |
| 2 | 64 | | return Result.Failure<Money>(Error.NotFound); |
| | 65 | | } |
| 17 | 66 | | return new Money(currencyCode, exchangeRate.Amount * amount); |
| 23 | 67 | | } |
| | 68 | | public static bool IsLegalConversion(CurrencyCode currencyCode) |
| 74 | 69 | | { |
| 74 | 70 | | return !IllegalConversions.Contains(currencyCode); |
| 74 | 71 | | } |
| | 72 | | } |
| | 73 | | } |