< Summary

Information
Class: c:\Source 2025\CurrencyConverter\CurrencyConverter\src\Domain\Currencies\CurrencySnapshot.cs
Assembly: Default
File(s): c:\Source 2025\CurrencyConverter\CurrencyConverter\src\Domain\Currencies\CurrencySnapshot.cs
Line coverage
100%
Covered lines: 49
Uncovered lines: 0
Coverable lines: 49
Total lines: 73
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

c:\Source 2025\CurrencyConverter\CurrencyConverter\src\Domain\Currencies\CurrencySnapshot.cs

#LineLine coverage
 1using Domain.Common;
 2
 3namespace Domain.Currencies
 4{
 5    public record CurrencySnapshot
 6    {
 127        protected internal CurrencySnapshot() { }
 18        private static Error AmountCannotBeLessThanZeroError = new Error(ErrorCode.BadInput, "The amount cannot be less 
 19        private static Error IlligalConversionError = new Error(ErrorCode.BadInput, "The requested currency code is not 
 10
 111        public static readonly IReadOnlyCollection<CurrencyCode> IllegalConversions = new[]
 112        {
 113            CurrencyCode.Try,
 114            CurrencyCode.Pln,
 115            CurrencyCode.Thb,
 116            CurrencyCode.Mxn
 117         };
 18
 10119        private CurrencySnapshot(CurrencyCode code, DateTime dateCaptured, List<Money> exchanges)
 10120        {
 10121            Code = code;
 10122            DateCaptured = dateCaptured;
 10123            ExchangeRates = exchanges;
 10124        }
 15125        public CurrencyCode Code { get; init; }
 17226        public DateTime DateCaptured { get; init; }
 18327        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)
 10333        {
 10334            var currencyCodeResult = CurrencyCode.FromCode(code);
 10335            if (currencyCodeResult.IsFailure)
 136            {
 137                return Result.Failure<CurrencySnapshot>(currencyCodeResult.Error);
 38            }
 39
 33740            if (exchangeRates.Any(x => x.Amount < 0))
 141            {
 142                return Result.Failure<CurrencySnapshot>(AmountCannotBeLessThanZeroError);
 43            }
 33544            var exchangeResults = exchangeRates.Select(x => new
 33545            {
 33546                Result = CurrencyCode.FromCode(x.Code),
 33547                Original = x
 33548            }).ToList();
 49
 33550            var exchanges = exchangeResults.Where(x=>x.Result.IsSuccess)
 33351                .Select(x => new Money(x.Result.Value, x.Original.Amount)).ToList();
 10152            return new CurrencySnapshot(currencyCodeResult.Value, dateCaptured, exchanges);
 10353        }
 54
 55        public Result<Money> Convert(decimal amount, CurrencyCode currencyCode)
 2356        {
 2357            if (!IsLegalConversion(currencyCode))
 458            {
 459                return Result.Failure<Money>(IlligalConversionError);
 60            }
 4461            var exchangeRate = ExchangeRates.FirstOrDefault(x => x.Code == currencyCode);
 1962            if (exchangeRate == null)
 263            {
 264                return Result.Failure<Money>(Error.NotFound);
 65            }
 1766            return new Money(currencyCode, exchangeRate.Amount * amount);
 2367        }
 68        public static bool IsLegalConversion(CurrencyCode currencyCode)
 7469        {
 7470            return !IllegalConversions.Contains(currencyCode);
 7471        }
 72    }
 73}