public async Task <IActionResult> ChangeWebsite() { var epaoid = _contextAccessor.HttpContext.User.FindFirst("http://schemas.portal.com/epaoid")?.Value; try { var organisation = await _organisationsApiClient.GetEpaOrganisation(epaoid); var viewModel = new ChangeWebsiteViewModel { WebsiteLink = ModelState.IsValid ? organisation.OrganisationData?.WebsiteLink : ModelState[nameof(ChangeWebsiteViewModel.WebsiteLink)]?.AttemptedValue }; return(View(viewModel)); } catch (EntityNotFoundException e) { _logger.LogWarning(e, "Failed to find organisation"); return(RedirectToAction(nameof(HomeController.NotRegistered), nameof(HomeController).RemoveController())); } }
public async Task <IActionResult> ChangeWebsite(ChangeWebsiteViewModel vm) { var epaoid = _contextAccessor.HttpContext.User.FindFirst("http://schemas.portal.com/epaoid")?.Value; try { var organisation = await GetEpaOrganisation(epaoid); if (organisation == null) { return(RedirectToAction(nameof(HomeController.NotRegistered), nameof(HomeController).RemoveController())); } if (vm.ActionChoice == "Save") { if (ModelState.IsValid) { if (string.Equals(vm.WebsiteLink, organisation.OrganisationData?.WebsiteLink)) { return(RedirectToAction(nameof(OrganisationDetails))); } // only check if an web site link has been entered - model has required validator var encodedWebsiteUrl = HttpUtility.UrlEncode(vm.WebsiteLink); _logger.LogInformation($"VALIDATEWEBSITELINK - OrganisationController.ChangeWebsite: {vm.WebsiteLink}, {encodedWebsiteUrl}"); if (await _validationApiClient.ValidateWebsiteLink(encodedWebsiteUrl) == false) { ModelState.AddModelError(nameof(ChangeWebsiteViewModel.WebsiteLink), "Enter a valid website address"); } } if (!ModelState.IsValid) { return(RedirectToAction(nameof(ChangeWebsite))); } vm = new ChangeWebsiteViewModel { WebsiteLink = vm.WebsiteLink }; return(View("ChangeWebsiteConfirm", vm)); } else if (vm.ActionChoice == "Confirm") { var userId = _contextAccessor.HttpContext.User.FindFirst("UserId").Value; var request = new UpdateEpaOrganisationWebsiteLinkRequest { WebsiteLink = vm.WebsiteLink, OrganisationId = organisation.OrganisationId, UpdatedBy = Guid.Parse(userId) }; var notifiedContacts = await _organisationsApiClient.UpdateEpaOrganisationWebsiteLink(request); if (notifiedContacts == null) { throw new Exception("Unable to update the website address."); } vm = new ChangeWebsiteViewModel { WebsiteLink = vm.WebsiteLink, Contacts = notifiedContacts }; return(View("ChangeWebsiteUpdated", vm)); } } catch (Exception e) { _logger.LogError(e, "Failed to change website link"); ModelState.AddModelError(nameof(ChangeWebsiteViewModel.WebsiteLink), "Unable to update the website address at this time."); return(RedirectToAction(nameof(ChangeWebsite))); } return(RedirectToAction(nameof(OrganisationDetails))); }