public void EditCentreWebsiteDetails_should_show_validation_error_to_user_when_given_invalid_postcode()
        {
            // Given
            var model = new EditCentreWebsiteDetailsViewModel {
                CentrePostcode = "aaaaaaaaa"
            };

            A.CallTo(() => mapsApiHelper.GeocodePostcode(A <string> ._))
            .Returns(new MapsResponse {
                Status = "ZERO_RESULTS"
            });

            // When
            var result = controller.EditCentreWebsiteDetails(model);

            // Then
            result.Should().BeViewResult().WithDefaultViewName()
            .ModelAs <EditCentreWebsiteDetailsViewModel>();
            Assert.IsFalse(controller.ModelState.IsValid);
        }
Example #2
0
        public IActionResult EditCentreWebsiteDetails(EditCentreWebsiteDetailsViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            model.CentrePostcode = model.CentrePostcode !.Trim();
            var mapsResponse = mapsApiHelper.GeocodePostcode(model.CentrePostcode).Result;

            if (mapsResponse.HasNoResults())
            {
                ModelState.AddModelError(nameof(model.CentrePostcode), "Enter a UK postcode");
                return(View(model));
            }

            if (mapsResponse.ApiErrorOccurred())
            {
                logger.LogWarning
                (
                    $"Failed Maps API call when trying to get postcode {model.CentrePostcode} " +
                    $"- status of {mapsResponse.Status} - error message: {mapsResponse.ErrorMessage}"
                );
                return(new StatusCodeResult(500));
            }

            var latitude  = double.Parse(mapsResponse.Results[0].Geometry.Location.Latitude);
            var longitude = double.Parse(mapsResponse.Results[0].Geometry.Location.Longitude);

            var centreId = User.GetCentreId();

            centresDataService.UpdateCentreWebsiteDetails(
                centreId,
                model.CentrePostcode,
                latitude,
                longitude,
                model.CentreTelephone,
                model.CentreEmail !,
                model.OpeningHours,
                model.CentreWebAddress,
                model.OrganisationsCovered,
                model.TrainingVenues,
                model.OtherInformation
                );

            return(RedirectToAction("Index"));
        }