public async Task <ActionResult> Staff(StaffListVM model, uint page) { if (!ModelState.IsValid) { return(View(model)); } StaffListDTO staffList = await _staffManager.Staff(new StaffFiltersDTO { NameOrEmail = model.NameOrEmail, Page = page, PageSize = PageSize }); model.Staff = staffList.Staff.Select(user => new StaffDataVM { Id = user.Id, Email = user.Email, LastName = user.LastName, FirtsName = user.FirstName, MiddelName = user.MiddleName, PhoneNumber = user.PhoneNumber }); model.PagingInfo = new PagingInfoVM { CurrentPage = staffList.PagingInfo.CurrentPage, TotalItems = staffList.PagingInfo.TotalItems, ItemsCurrentPage = staffList.PagingInfo.ItemsCurrentPage, ItemsPerPage = staffList.PagingInfo.ItemsPerPage }; return(View(model)); }
/// <summary> /// Возвращает список персонала отфильтрованный по <paramref name="filters"/>. /// </summary> public async Task <StaffListDTO> Staff(StaffFiltersDTO filters) { IQueryable <ApplicationUser> users = _userManager.Users.Where(u => u.UserRoles.All(r => r.Role.Name != "User" && r.Role.Name != "Admin") ); if (filters == null) { return(new StaffListDTO { PagingInfo = new PagingInfoDTO { CurrentPage = 1, ItemsPerPage = 15, TotalItems = await users.CountAsync() }, // Скипаем ноль объектов, и берем 15. Staff = await MapToDto(0, 15) }); } if (filters.Page < 1) { filters.Page = 1; } if (filters.PageSize < 1) { filters.PageSize = 1; } if (!string.IsNullOrEmpty(filters.NameOrEmail)) { users = users.Where(u => u.Email.Contains(filters.NameOrEmail) || (u.FirstName + " " + (string.IsNullOrWhiteSpace(u.MiddleName) ? "" : u.MiddleName + " ") + u.LastName).Contains(filters.NameOrEmail) ); } int totalItems = await users.CountAsync(); int skipCount = (int)(filters.Page - 1) * filters.PageSize; var staffListDto = new StaffListDTO { Staff = await MapToDto(skipCount, filters.PageSize), PagingInfo = new PagingInfoDTO { CurrentPage = (int)filters.Page, ItemsPerPage = filters.PageSize, TotalItems = totalItems } }; return(staffListDto); #region Local Functions async Task <IEnumerable <ApplicationUserDTO> > MapToDto(int skip, int pageSize) { var dtoList = await users.Select(p => new ApplicationUserDTO { Id = p.Id, AccessFailedCount = p.AccessFailedCount, Address = p.Address, ApplicationRoles = p.UserRoles.Select(ur => new ApplicationRoleDTO { Id = ur.Role.Id, ConcurrencyStamp = ur.Role.ConcurrencyStamp, Name = ur.Role.Name, NormalizedName = ur.Role.NormalizedName }).ToList(), ConcurrencyStamp = p.ConcurrencyStamp, UserName = p.UserName, Email = p.Email, LastName = p.LastName, MiddleName = p.MiddleName, FirstName = p.FirstName, EmailConfirmed = p.EmailConfirmed, LockoutEnabled = p.LockoutEnabled, LockoutEnd = p.LockoutEnd, NormalizedEmail = p.NormalizedEmail, NormalizedUserName = p.NormalizedUserName, PasswordHash = p.PasswordHash, PhoneNumber = p.PhoneNumber, PhoneNumberConfirmed = p.PhoneNumberConfirmed, SecurityStamp = p.SecurityStamp, TwoFactorEnabled = p.TwoFactorEnabled }).OrderBy(p => p.Id).Skip(skip).Take(pageSize).ToArrayAsync(); return(dtoList); } #endregion }