示例#1
0
        public async Task <IActionResult> GetAll()
        {
            var actorOrgs = await _actorOrgService.GetAll();

            return(Ok(actorOrgs));
        }
        public async Task <BenefitMenuViewModel> GetMenu()
        {
            try
            {
                var benefitMenuViewModel = new BenefitMenuViewModel();

                var personnel = await _personnelService.GetAll();

                var personnelOrgs = personnel.Select(x => x.ActorOrganisationId).Distinct().ToList();

                var actorOrganisations = await _actorOrganisationService.GetAll();

                var actorOrganisationIds = actorOrganisations.Select(x => x.Id).ToList();

                var orphanedOrgs      = personnelOrgs.Where(x => !actorOrganisationIds.Contains(x));
                var orphanedPersonnel = personnel.Where(x => !string.IsNullOrEmpty(x.ActorOrganisationId) && orphanedOrgs.Contains(x.ActorOrganisationId) && x.Benefits != null && x.Benefits.Count > 0);

                foreach (var orphan in orphanedPersonnel)
                {
                    benefitMenuViewModel.MenuItems.Add(new BenefitMenuItem {
                        Id     = orphan.Id,
                        Title  = orphan.Name,
                        Type   = "Personnel",
                        Active = true
                    });
                }

                foreach (var orgs in actorOrganisations)
                {
                    var children   = personnel.Where(x => x.ActorOrganisationId == orgs.Id && x.Benefits != null && x.Benefits.Count > 0);
                    var childItems = new List <BenefitMenuItem>();

                    foreach (var child in children)
                    {
                        childItems.Add(new BenefitMenuItem {
                            Id     = child.Id,
                            Title  = child.Name,
                            Type   = "Personnel",
                            Active = true
                        });
                    }

                    var canBeActive = orgs.Benefits != null && orgs.Benefits.Count > 0;

                    if (!childItems.Any() && !canBeActive)
                    {
                        continue;
                    }

                    benefitMenuViewModel.MenuItems.Add(new BenefitMenuItem
                    {
                        Id       = orgs.Id,
                        Title    = orgs.Name,
                        Type     = "ActorOrganisation",
                        Active   = canBeActive,
                        Children = childItems
                    });
                }

                return(benefitMenuViewModel);
            }
            catch (Exception ex)
            {
                // log or manage the exception
                throw ex;
            }
        }