public async Task <ActionResult> Edit(int?groupUId, int?establishmentUrn, int?removalGid, int?duplicateGovernorId, bool roleAlreadyExists = false) { Guard.IsTrue(groupUId.HasValue || establishmentUrn.HasValue, () => new InvalidParameterException($"Both parameters '{nameof(groupUId)}' and '{nameof(establishmentUrn)}' are null.")); var domainModel = await _governorsReadService.GetGovernorListAsync(establishmentUrn, groupUId, User); var governorPermissions = await _governorsReadService.GetGovernorPermissions(establishmentUrn, groupUId, User); var viewModel = new GovernorsGridViewModel(domainModel, true, groupUId, establishmentUrn, _nomenclatureService, await _cachedLookupService.NationalitiesGetAllAsync(), await _cachedLookupService.GovernorAppointingBodiesGetAllAsync(), governorPermissions); var applicableRoles = domainModel.ApplicableRoles.Cast <int>(); viewModel.GovernorRoles = (await _cachedLookupService.GovernorRolesGetAllAsync()).Where(x => applicableRoles.Contains(x.Id)).Select(x => new LookupItemViewModel(x)).ToList(); await _layoutHelper.PopulateLayoutProperties(viewModel, establishmentUrn, groupUId, User, x => viewModel.GovernanceMode = x.GovernanceMode, x => { viewModel.ShowDelegationAndCorpContactInformation = x.GroupTypeId.GetValueOrDefault() == (int)eLookupGroupType.MultiacademyTrust; viewModel.DelegationInformation = x.DelegationInformation; viewModel.CorporateContact = x.CorporateContact; }); viewModel.RemovalGid = removalGid; viewModel.GovernorShared = false; if (removalGid.HasValue) { var govToBeRemoved = domainModel.CurrentGovernors.SingleOrDefault(g => g.Id == removalGid.Value); if (govToBeRemoved != null && EnumSets.SharedGovernorRoles.Contains(govToBeRemoved.RoleId.Value)) { viewModel.GovernorShared = true; } } if (duplicateGovernorId.HasValue) { var duplicate = await _governorsReadService.GetGovernorAsync(duplicateGovernorId.Value, User); ViewData.Add("DuplicateGovernor", duplicate); } if (roleAlreadyExists) { ModelState.AddModelError("role", "The selected role already contains an appointee."); } return(View(VIEW_EDIT_GOV_VIEW_NAME, viewModel)); }
public static async Task <SharedGovernorViewModel> MapFromGovernor(GovernorModel governor, int establishmentUrn, ICachedLookupService cachedLookupService) { var dateNow = DateTime.Now.Date; var appointment = governor.Appointments?.SingleOrDefault(g => g.EstablishmentUrn == establishmentUrn); var sharedWith = governor.Appointments? .Where(a => a.AppointmentStartDate < dateNow && (a.AppointmentEndDate == null || a.AppointmentEndDate > dateNow)) .Select(a => new EstablishmentViewModel { Urn = a.EstablishmentUrn.Value, EstablishmentName = a.EstablishmentName }) .ToList(); var appointingBodies = await cachedLookupService.GovernorAppointingBodiesGetAllAsync(); var nationalities = await cachedLookupService.NationalitiesGetAllAsync(); return(new SharedGovernorViewModel { AppointingBodyName = appointingBodies.Single(g => g.Id == governor.AppointingBodyId).Name, AppointmentStartDate = appointment?.AppointmentStartDate != null ? new DateTimeViewModel(appointment.AppointmentStartDate) : new DateTimeViewModel(), AppointmentEndDate = appointment?.AppointmentEndDate != null ? new DateTimeViewModel(appointment.AppointmentEndDate) : new DateTimeViewModel(), DOB = governor.DOB, FullName = governor.GetFullName(), Id = governor.Id.Value, PostCode = governor.PostCode, Selected = appointment != null, PreExisting = appointment != null, SharedWith = sharedWith ?? new List <EstablishmentViewModel>(), MultiSelect = IsSharedGovernorRoleMultiSelect((eLookupGovernorRole)governor.RoleId) }); }