public async Task <IActionResult> Program(int id, bool?list = null, int?ageGroup = null) { var settings = await _performerSchedulingService.GetSettingsAsync(); var schedulingStage = _performerSchedulingService.GetSchedulingStage(settings); if (schedulingStage < PsSchedulingStage.SchedulingPreview) { return(RedirectToAction(nameof(Index))); } PsProgram program; try { program = await _performerSchedulingService.GetProgramByIdAsync(id, includeAgeGroups : true, includeImages : true, onlyApproved : true); } catch (GraException gex) { ShowAlertDanger("Unable to view program: ", gex); return(RedirectToAction(nameof(Programs))); } var selectedAgeGroup = program.AgeGroups.FirstOrDefault(_ => _.Id == ageGroup); var performer = await _performerSchedulingService.GetPerformerByIdAsync( program.PerformerId); var system = await _performerSchedulingService .GetSystemWithoutExcludedBranchesAsync(GetId(ClaimType.SystemId)); var viewModel = new ProgramViewModel { AgeGroup = selectedAgeGroup, AllBranches = performer.AllBranches, List = list == true, Program = program, SchedulingOpen = schedulingStage == PsSchedulingStage.SchedulingOpen, System = system, CanSchedule = UserHasPermission(Permission.SchedulePerformers) }; if (!performer.AllBranches) { viewModel.BranchAvailability = await _performerSchedulingService .GetPerformerBranchIdsAsync(performer.Id, system.Id); } if (program.Images?.Count > 0) { viewModel.Image = _pathResolver.ResolveContentPath( program.Images[0].Filename); } if (viewModel.SchedulingOpen) { viewModel.AgeGroupList = new SelectList(program.AgeGroups, "Id", "Name"); var branches = new List <Branch>(); foreach (var branch in system.Branches) { if (!performer.AllBranches && !viewModel.BranchAvailability.Contains(branch.Id)) { continue; } else { var branchSelections = await _performerSchedulingService .GetSelectionsByBranchIdAsync(branch.Id); if (branchSelections.Count >= settings.SelectionsPerBranch) { continue; } else { var selectedAgeGroups = branchSelections.Select(_ => _.AgeGroupId); var availableAgeGroups = program.AgeGroups .Any(_ => !selectedAgeGroups.Contains(_.Id)); if (!availableAgeGroups) { continue; } } } branches.Add(branch); } viewModel.BranchList = new SelectList(branches, "Id", "Name"); } if (viewModel.List) { var programIndexList = await _performerSchedulingService.GetProgramIndexListAsync( ageGroupId : selectedAgeGroup?.Id, onlyApproved : true); var index = programIndexList.IndexOf(id); viewModel.ReturnPage = (index / ProgramsPerPage) + 1; if (index != 0) { viewModel.PrevProgram = programIndexList[index - 1]; } if (programIndexList.Count != index + 1) { viewModel.NextProgram = programIndexList[index + 1]; } } return(View(viewModel)); }