public async Task <ActionResult> SelectSharedGovernor(int establishmentUrn, eLookupGovernorRole role) { var roleName = (await _cachedLookupService.GovernorRolesGetAllAsync()).Single(x => x.Id == (int)role).Name; var governors = (await _governorsReadService.GetSharedGovernorsAsync(establishmentUrn, User)).Where(g => RoleEquivalence.GetEquivalentRole(role).Contains((eLookupGovernorRole)g.RoleId)).ToList(); var sharedGovernors = governors.Select(async g => await SharedGovernorViewModel.MapFromGovernor(g, establishmentUrn, _cachedLookupService)); var viewModel = new SelectSharedGovernorViewModel { Governors = (await Task.WhenAll(sharedGovernors)).ToList(), GovernorType = roleName.ToLowerInvariant(), }; await _layoutHelper.PopulateLayoutProperties(viewModel, establishmentUrn, null, User); return(View(viewModel)); }
public async Task <ActionResult> SelectSharedGovernor(SelectSharedGovernorViewModel model) { if (ModelState.IsValid) { foreach (var governor in model.Governors.Where(g => (g.Selected && !g.PreExisting) || string.Equals(g.Id.ToString(), model.SelectedGovernorId))) { var response = await _governorsWriteService.AddSharedGovernorAppointmentAsync(governor.Id, model.Urn.Value, governor.AppointmentStartDate.ToDateTime().Value, governor.AppointmentEndDate.ToDateTime(), User); if (!response.Success) { response.ApplyToModelState(ControllerContext); } } if (ModelState.IsValid) { var url = $"{Url.RouteUrl("EstabDetails", new { id = model.Urn, saved = true })}#school-governance"; return(Redirect(url)); } } var governors = (await Task.WhenAll( (await _governorsReadService.GetSharedGovernorsAsync(model.Urn.Value, User)) .Where(g => RoleEquivalence.GetEquivalentRole(model.Role).Contains((eLookupGovernorRole)g.RoleId)) .Select(g => SharedGovernorViewModel.MapFromGovernor(g, model.Urn.Value, _cachedLookupService)))).ToList(); foreach (var previousGovernor in model.Governors) { var newGovernor = governors.Single(g => g.Id == previousGovernor.Id); if (!newGovernor.PreExisting) { newGovernor.Selected = previousGovernor.Selected; } } model.Governors = governors; await _layoutHelper.PopulateLayoutProperties(model, model.Urn.Value, null, User); return(View(model)); }