public async Task <FindResultResponse <RoleInfo> > ExecuteAsync(FindRolesFilter filter) { if (!_findFilterValidator.ValidateCustom(filter, out List <string> errors)) { _httpContextAccessor.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; return(new FindResultResponse <RoleInfo> { Status = OperationResultStatusType.Failed, Errors = errors }); } FindResultResponse <RoleInfo> result = new(); (List <(DbRole role, List <DbRightsLocalization> rights)> roles, int totalCount) = filter.IncludeDeactivated ? await _roleRepository.FindAllAsync(filter) : await _roleRepository.FindActiveAsync(filter); result.TotalCount = totalCount; List <Guid> usersIds = new(); foreach ((DbRole role, List <DbRightsLocalization> rights) in roles) { usersIds.Add(role.CreatedBy); if (role.ModifiedBy.HasValue) { usersIds.Add(role.ModifiedBy.Value); } } List <UserInfo> usersInfos = (await GetUsersAsync(usersIds.Distinct().ToList(), errors))? .Select(_userInfoMapper.Map) .ToList(); result.Body = roles.Select( pair => _roleInfoMapper.Map(pair.role, pair.rights.Select(_rightMapper.Map).ToList(), usersInfos)).ToList(); result.Errors = errors; result.Status = errors.Any() ? OperationResultStatusType.PartialSuccess : OperationResultStatusType.FullSuccess; return(result); }
public async Task <FindResultResponse <RoleInfo> > FindAsync( [FromServices] IFindRolesCommand command, [FromQuery] FindRolesFilter filter) { return(await command.ExecuteAsync(filter)); }
public async Task <(List <(DbRole role, List <DbRightsLocalization> rights)>, int totalCount)> FindAllAsync(FindRolesFilter filter) { int totalCount = await _provider.Roles.CountAsync(); return((await (from role in _provider.Roles join roleLocalization in _provider.RolesLocalizations on role.Id equals roleLocalization.RoleId where roleLocalization.IsActive join right in _provider.RoleRights on role.Id equals right.RoleId join rightLocalization in _provider.RightsLocalizations on right.RightId equals rightLocalization.RightId where (roleLocalization.Locale == filter.Locale || roleLocalization.Locale == null) && (rightLocalization.Locale == filter.Locale || rightLocalization.Locale == null) orderby role.Id select new { Role = role, RoleLocalization = roleLocalization, RightLocalization = rightLocalization }).ToListAsync()).AsEnumerable().GroupBy(r => r.Role.Id) .Select(x => { DbRole role = x.Select(x => x.Role).FirstOrDefault(); role.RoleLocalizations = x.Select(x => x.RoleLocalization).Where(x => x != null).GroupBy(x => x.Id).Select(x => x.First()).ToList(); return (role, x.Select(x => x.RightLocalization).ToList()); }).Skip(filter.SkipCount).Take(filter.TakeCount).ToList(), totalCount); }