Ejemplo n.º 1
0
        public async Task <WeekendsLeftResponse> GetWeekendsLeftAsync(WeekendsLeftRequest weekendsLeftRequest)
        {
            var weekendsLeftResponse = new WeekendsLeftResponse();

            // Model Validation
            var validator = new WeekendsLeftRequestValidator();
            var results   = validator.Validate(weekendsLeftRequest);

            if (!results.IsValid)
            {
                weekendsLeftResponse.Errors  = results.Errors.Select(errors => errors.ErrorMessage).ToList();
                weekendsLeftResponse.Message = "Errors in request, please correct and resubmit";
                return(weekendsLeftResponse);
            }

            // County Code Validation
            if (!_countriesService.GetCountryData().ContainsKey(weekendsLeftRequest.Country.ToUpper()))
            {
                weekendsLeftResponse.Errors  = new[] { "Country Code is not valid" };
                weekendsLeftResponse.Message = "Errors in request, please correct and resubmit";
                return(weekendsLeftResponse);
            }

            // Life Expectancy Lookup
            var remainingLifeExpectancyYears = await _lifeExpectancyService.GetRemainingLifeExpectancyYearsAsync(weekendsLeftRequest);

            // Life Expectancy Calculations
            weekendsLeftResponse = _lifeExpectancyService.LifeExpectancyCalculations(weekendsLeftRequest.Age, remainingLifeExpectancyYears);

            return(weekendsLeftResponse);
        }
        public WeekendsLeftShould(ITestOutputHelper output)
        {
            var mockConfiguration    = new Mock <IConfiguration>();    // Moq
            var mockCountriesService = new Mock <ICountriesService>(); // Moq
            var mockDict             = new Dictionary <string, string>()
            {
                { "USA", "United States" },
                { "NZL", "New Zealand" }
            };

            mockCountriesService.Setup(x => x.GetCountryData()).Returns(mockDict);

            var mockLifeExpectancyService = new Mock <ILifeExpectancyService>(); // Moq
            var wlr = new WeekendsLeftResponse
            {
                EstimatedWeekendsLeft = 1623,
                EstimatedAgeOfDeath   = 86,
                EstimatedDayOfDeath   = new DateTime(2051, 11, 13, 02, 48, 17),
                Message = "You have an estimated 1623 weekends left in your life, get out there and enjoy it!",
                Errors  = null
            };

            mockLifeExpectancyService.Setup(x => x.LifeExpectancyCalculations(Moq.It.IsAny <int>(), Moq.It.IsAny <double>())).Returns(wlr);
            mockLifeExpectancyService.Setup(x => x.GetRemainingLifeExpectancyYearsAsync(It.IsAny <WeekendsLeftRequest>())).ReturnsAsync(34.8);

            weekendsLeftService = new WeekendsLeftService(mockConfiguration.Object, mockCountriesService.Object, mockLifeExpectancyService.Object);

            _output = output;
        }
        public WeekendsLeftResponse LifeExpectancyCalculations(int age, double remainingLifeExpectancyYears)
        {
            var weekendsLeftResponse = new WeekendsLeftResponse();

            var estimatedAgeOfDeath   = (int)(age + remainingLifeExpectancyYears);
            var estimatedDaysLeft     = (int)(remainingLifeExpectancyYears * 365);
            var estimatedDayOfDeath   = DateTime.Now.AddDays(estimatedDaysLeft);
            var estimatedWeekendsLeft = estimatedDaysLeft / 7;

            weekendsLeftResponse.EstimatedAgeOfDeath   = estimatedAgeOfDeath;
            weekendsLeftResponse.EstimatedDayOfDeath   = estimatedDayOfDeath;
            weekendsLeftResponse.EstimatedWeekendsLeft = estimatedWeekendsLeft;
            weekendsLeftResponse.Message = $"You have an estimated {estimatedWeekendsLeft} weekends left in your life, get out there and enjoy it all";
            return(weekendsLeftResponse);
        }