Example #1
0
        public Task <List <FunctionViewModel> > GetsByRole(string roles)
        {
            if (!string.IsNullOrEmpty(roles))
            {
                var lstRole     = roles.Split(CommonConstants.SepRoles);
                var functions   = _functionRepository.FindAll();
                var permissions = _permissionRepository.FindAll();
                var query       = from f in functions
                                  join p in permissions on f.Id equals p.FunctionId
                                  join r in _roleManager.Roles on p.RoleId equals r.Id
                                  where lstRole.Contains(r.Name) && ((p.CanCreate || p.CanRead || p.CanUpdate || p.CanDelete) || f.ParentId == null)
                                  select f;
                var lstFunction = query.OrderBy(x => x.ParentId).ProjectTo <FunctionViewModel>().ToList();
                var dictKey     = new Dictionary <string, int>();
                for (int i = 0; i < lstFunction.Count(); i++)
                {
                    if (lstFunction[i].ParentId != null)
                    {
                        string key = lstFunction[i].ParentId;
                        DictionaryHelper.AddOrUpdate(dictKey, key);
                    }
                    else
                    {
                        var key = lstFunction[i].Id;
                        DictionaryHelper.AddOrUpdate(dictKey, key);
                    }
                }

                foreach (var item in dictKey)
                {
                    var parent = lstFunction.FirstOrDefault(x => x.Id == item.Key);
                    if (item.Value == 0 && parent != null)
                    {
                        lstFunction.Remove(parent);
                    }
                }
                return(Task.Run(() => { return lstFunction; }));
            }
            return(Task.Run(() => { return new List <FunctionViewModel>(); }));
        }