public void ShouldEqualPageNumber() { var expectedPageNumber = 1; var sut = Paginable.Empty <int>(); sut.PageNumber.ShouldBeEquivalentTo(expectedPageNumber); }
public async Task <IActionResult> GetBranchesOfCompanyTrusted( [FromRoute] int companyId, [FromRoute] string token, [FromQuery] BranchFilter filterModel, [FromQuery] Sortable sortable, [FromQuery] Paginable paginable) { if (token != Consts.StaticToken) { return(new ContentResult { StatusCode = 403 }); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var branchList = await _branchManager.GetBranchesByCompany(companyId, filterModel, sortable, paginable); return(Ok(branchList)); } catch (KeyNotFoundException ex) { return(NotFound(ex.Message)); } }
public void ShouldEqualTotalItemCount() { var expectedTotalItemCount = 0; var sut = Paginable.Empty <int>(); sut.TotalItemCount.ShouldBeEquivalentTo(expectedTotalItemCount); }
public void ShouldEqualItemCountPerPage() { var expectedItemCountPerPage = EmptyPaginable <int> .DefaultItemCountPerPage; var sut = Paginable.Empty <int>(); sut.ItemCountPerPage.ShouldBeEquivalentTo(expectedItemCountPerPage); }
/// <inheritdoc /> private IQueryable <Branch> Pagination(Paginable paginable, IQueryable <Branch> query) { if (paginable.Take.HasValue && paginable.Skip.HasValue) { return(query.Skip(paginable.Skip.Value).Take(paginable.Take.Value)); } return(query); }
/// <summary> /// Обновить пагинируемую сущность на текущую страницу /// </summary> public void RefreshPaginable() { this.Count = Paginable.Count; if (this.CurrentPage > this.MaxPages) { this.CurrentPage = 1; } Paginable.Refresh(this.CurrentPage - 1, this.ElementsPerPage); }
public async Task <IActionResult> Get( [FromQuery] BranchFilter filterModel, [FromQuery] Sortable sortable, [FromQuery] Paginable paginable) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = new PagingResult <BranchModel>(); if (filterModel.BranchIds.Any()) { return(Ok(await _branchManager.GetBranchesById(filterModel.BranchIds))); } result = await _branchManager.GetBranches(filterModel, sortable, paginable); return(Ok(result)); }
public async Task <IActionResult> GetBranchesOfCompany( [FromRoute] int companyId, [FromQuery] BranchFilter filterModel, [FromQuery] Sortable sortable, [FromQuery] Paginable paginable) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var branchList = await _branchManager.GetBranchesByCompany(companyId, filterModel, sortable, paginable); return(Ok(branchList)); } catch (KeyNotFoundException ex) { return(NotFound(ex.Message)); } }
public async Task <IActionResult> Get( [FromQuery] CompanyFilter filterModel, [FromQuery] Sortable sortable, [FromQuery] Paginable paginable) { if (!ModelState.IsValid) { return(BadRequest()); } var result = new PagingResult <CompanyModel>(); if (filterModel.CompanyIds.Any()) { return(Ok(await _companyManager.GetCompanyNameById(filterModel.CompanyIds))); } result = await _companyManager.GetCompanies(filterModel, sortable, paginable); return(Ok(result)); }
/// <inheritdoc /> public async Task <PagingResult <BranchModel> > GetBranches(BranchFilter filter = null, Sortable sortable = null, Paginable paginable = null) { try { IQueryable <Branch> query = null; query = await _branchRepository.GetAsync(); if (filter != null) { query = Filtering(filter, query); } // Calculate total count var queryableCount = query; int totalCount = queryableCount.Count(); if (sortable != null) { query = Sorting(sortable, query); } if (paginable != null) { query = Pagination(paginable, query); } var branches = query.ToList(); var branchModelList = new List <BranchModel>(); var allUsersQuery = await _userRepository.GetAsync(x => !string.IsNullOrEmpty(x.Role)?x.Role.Trim().Equals(salesRoleId) : false); var allUsers = allUsersQuery.ToList().GroupBy(x => x.BranchId); foreach (var branch in branches) { var branchModel = branch.Adapt <BranchModel>(); branchModel.CountSalesRep = allUsers.Where(x => x.Key == branch.Id).FirstOrDefault()?.Count(); branchModelList.Add(branchModel); } return(new PagingResult <BranchModel> { Total = totalCount, ItemsPerPage = paginable?.Take ?? default(int), Data = branchModelList }); } catch (Exception ex) { throw ex; } }
/// <inheritdoc /> public async Task <PagingResult <BranchModel> > GetBranchesByCompany(int companyId, BranchFilter filter, Sortable sortable, Paginable paginable) { var company = await _companyRepository.GetAsync(companyId); if (company == null) { throw new KeyNotFoundException(); } IQueryable <Branch> query = await _branchRepository.GetAsync(x => x.Company.Id == companyId); query = Filtering(filter, query); var queryableCount = query; int totalCount = queryableCount.Count(); query = Sorting(sortable, query); query = Pagination(paginable, query); var branches = query.ToList(); var branchModelList = new List <BranchModel>(); var allUsersQuery = await _userRepository.GetAsync(x => !string.IsNullOrEmpty(x.Role)?x.Role.Trim().Equals(salesRoleId) : false); foreach (var branch in branches) { var branchModel = branch.Adapt <BranchModel>(); branchModel.CountSalesRep = allUsersQuery.Where(x => x.BranchId == branch.Id)?.Count(); branchModelList.Add(branchModel); } return(new PagingResult <BranchModel> { Total = totalCount, ItemsPerPage = paginable.Take ?? 0, Data = branchModelList }); }
/// <inheritdoc /> public async Task <PagingResult <CompanyModel> > GetCompanies(CompanyFilter filter = null, Sortable sortable = null, Paginable paginable = null) { _logger.LogInformation("GET Companies requested with filters: {filter}, sort params {sortable}, paging params {paginable}", filter, sortable, paginable); IQueryable <Company> query = await _companyRepository.GetAsync(); if (filter != null) { query = Filtering(filter, query); } var queryableCount = query; int totalCount = queryableCount.Count(); if (sortable != null) { query = Sorting(sortable, query); } if (paginable != null) { query = Pagination(paginable, query); } var companies = query.ToList(); var companyModelList = new List <CompanyModel>(); var allUsersQuery = await _userRepository.GetAsync(x => !string.IsNullOrEmpty(x.Role)?x.Role.Trim().Equals(salesRoleId) : false); var allUsers = allUsersQuery.GroupBy(x => x.CompanyId).ToList(); var Tenants = (await _TenantRep.GetAsync()).ToHashSet(); foreach (var company in companies) { var companyModel = company.Adapt <CompanyModel>(); var Tenant = Tenants.FirstOrDefault(x => x.Companies.Select(c => c.CompanyId).ToHashSet().Contains(company.Id)); companyModel.CountSalesRep = allUsers.FirstOrDefault(x => x.Key == company.Id)?.Count(); companyModel.CountBranch = company.Branches.Count; companyModel.Tenant = Tenant?.Adapt <TenantSingleModel>(); companyModelList.Add(companyModel); } _logger.LogInformation("GET Companies ended, total companies fetched: {total}", totalCount); return(new PagingResult <CompanyModel> { Total = totalCount, ItemsPerPage = paginable?.Take ?? default, Data = companyModelList });