public async Task <IActionResult> GetProducts(ResourceParameter parameter) { log.Info("获取所有的产品list"); List <Product> products = new List <Product>(); var query = products.AsQueryable(); int count = 0; try { if (!_propertyMappingContainer.ValidMappingExistsFor <ResourceParameter, Product>(parameter.OrderBy)) { return(BadRequest("找不到要排序的字段")); } ProductWebApiContext productWebApiContext = new ProductWebApiContext(conn.DefaultConnection); var propertiesMap = new Dictionary <string, Expression <Func <Product, object> > > { { "Id", p => p.Id }, { "Name", p => p.Name }, { "Price", p => p.Price } }; if (!string.IsNullOrEmpty(parameter.Name)) { query = (from p in productWebApiContext.Product where p.Name.Contains(parameter.Name) select p) .AsQueryable(); count = await(from p in productWebApiContext.Product where p.Name.Contains(parameter.Name) select p).CountAsync(); } else { query = (from p in productWebApiContext.Product select p) .AsQueryable(); count = await(from p in productWebApiContext.Product select p).CountAsync(); } query = query.ApplySort(parameter.OrderBy, _propertyMappingContainer.Resolve <ResourceParameter, Product>()); products = query.Skip(parameter.PageIndex * parameter.PageSize).Take(parameter.PageSize).ToList(); var returnlist = new PaginatedList <Product>(parameter.PageIndex, parameter.PageSize, count, products); var preLink = returnlist.HasPrevious ? new CreateProductResourceUrl(_urlHelper).CreateResouceUrl(parameter, PaginationResourceUriType.PreviousPage, "GetProducts"):null; var nextLink = returnlist.HasNext ? new CreateProductResourceUrl(_urlHelper).CreateResouceUrl(parameter, PaginationResourceUriType.NextPage, "GetProducts") : null; var mata = new { returnlist.TotalItemsCount, returnlist.PaginationBase.PageSize, returnlist.PaginationBase.PageIndex, returnlist.PageCount, preLink, nextLink }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(mata)); } catch (Exception e) { log.Error(e); } return(Ok(products)); }
public async Task <IReadOnlyList <T> > ListWithOrderAsync <VM>(ISpecification <T> spec, string orderBy) { var query = ApplySpecification(spec); #if (RESTFULAPIHELPER) query = query.ApplySort(orderBy, _propertyMappingContainer.Resolve <VM, T>()); #endif return(await query.AsNoTracking().ToListAsync()); }
public PaginatedResultList <Article> GetArticlesSummary(ArticlePaginationParameter pagination) { var query = _dbSet.Where(x => x.Status.IsPublished && !x.Status.Deleted).AsQueryable(); if (!string.IsNullOrWhiteSpace(pagination.Title)) { query = query.Where(x => x.Content.Title.Contains(pagination.Title.Trim().ToLowerInvariant())); } //数据库中已发表文章总数 var itemsCount = query.Select(x => x.Id).Count(); query = query.Skip(pagination.GetSkipCount()).Take(pagination.PageSize); query = query.ApplySort(pagination.OrderBy, _mappingContainer.Resolve <ArticleDto, Article>()); var articles = query.Select( x => new Article( new Domain.Models.ValueObjects.ArticleContent(x.Content.Title, x.Content.CoverUrl, x.Content.Summary, null, null), x.Status, x.Settings, x.Author) ).AsNoTracking().ToList(); return(new PaginatedResultList <Article>(pagination, articles, itemsCount)); }
public async Task <PaginatedList <Article> > GetAllArticlesAsync(ArticleParameters request) { var query = _context.Articles.AsQueryable(); if (!string.IsNullOrEmpty(request.Title)) { query = query.Where(r => r.Title.ToLowerInvariant().Contains(request.Title.ToLowerInvariant())); } query = query.ApplySort(request.OrderBy, _propertyMappingContainer.Resolve <ArticleViewModel, Article>()); var count = await query.CountAsync(); var data = await query.Skip(request.PageIndex *request.PageSize).Take(request.PageSize).ToListAsync(); return(new PaginatedList <Article>(request.PageIndex, request.PageSize, count, data)); }
public async Task <PaginatedList <Menu> > GetAllGroupsAsync(MenuParameters parameters) { var query = context.Menus.AsQueryable(); if (!string.IsNullOrEmpty(parameters.Search)) { var serach = parameters.Search.ToLowerInvariant(); query = query.Where(t => t.MenuCode.ToLowerInvariant().Contains(serach) || t.MenuName.ToLowerInvariant().Contains(serach)); } query = query.ApplySort(parameters.OrderBy, propertyMappingContainer.Resolve <MenuViewModel, Menu>()); var count = await query.CountAsync(); var data = await query.Skip(parameters.PageIndex - 1 *parameters.PageSize).Take(parameters.PageSize).ToListAsync(); return(new PaginatedList <Menu>(parameters.PageIndex - 1, parameters.PageSize, count, data)); }
public async Task <PaginatedList <Order> > GetOrdersAsync(OrderQueryParameter orderQueryParameter) { var query = _dbContext.Orders.OrderBy(x => x.Id).AsQueryable(); if (!string.IsNullOrEmpty(orderQueryParameter.OrderNo)) { query = query.Where(x => x.OrderNo == orderQueryParameter.OrderNo); } if (!string.IsNullOrEmpty(orderQueryParameter.ReciverName)) { query = query.Where(x => x.ReciverName == orderQueryParameter.ReciverName); } if (!string.IsNullOrEmpty(orderQueryParameter.ReciverMobile)) { query = query.Where(x => x.ReciverMobile == orderQueryParameter.ReciverMobile); } if (!string.IsNullOrEmpty(orderQueryParameter.ReciverProvince)) { query = query.Where(x => x.ReciverProvince == orderQueryParameter.ReciverProvince); } if (!string.IsNullOrEmpty(orderQueryParameter.ReciverCity)) { query = query.Where(x => x.ReciverCity == orderQueryParameter.ReciverCity); } if (!string.IsNullOrEmpty(orderQueryParameter.ReciverDistrict)) { query = query.Where(x => x.ReciverDistrict == orderQueryParameter.ReciverDistrict); } _logger.LogInformation($"Order By => {orderQueryParameter.OrderBy}"); query = query.ApplySort(orderQueryParameter.OrderBy, _propertyMappingContainer.Resolve <OrderResourceModel, Order>()); var count = await query.CountAsync(); query = query.Skip(orderQueryParameter.PageIndex * orderQueryParameter.PageSize) .Take(orderQueryParameter.PageSize); var data = await query.ToListAsync(); var paginatedList = new PaginatedList <Order>(orderQueryParameter.PageIndex, orderQueryParameter.PageSize, count, data); return(paginatedList); }
public async Task <PaginatedList <Post> > GetAllPostsAsync(PostParameters postParameters) { var query = myContext.Post.AsQueryable(); if (!string.IsNullOrEmpty(postParameters.Title)) { var title = postParameters.Title;//不知道为什么转大小写后会报错 所以没有ToUpperInvariant query = query.Where(x => x.Title.Contains(title)); } query = query.ApplySort(postParameters.OrderBy, propertyMappingContainer.Resolve <PostDTO, Post>()); //query = query.OrderBy(x => x.Id); var count = await query.CountAsync(); var data = await query.Skip(postParameters.PageIndex *postParameters.PageSize).Take(postParameters.PageSize).ToListAsync(); return(new PaginatedList <Post>(postParameters.PageIndex, postParameters.PageSize, count, data)); }
public async Task <PaginatedList <User> > GetAllUsersAsync(UserParameters parameters) { var query = context.Users.AsQueryable(); //搜索,按登录名、用户名 if (!string.IsNullOrEmpty(parameters.Search)) { var serach = parameters.Search.ToLowerInvariant(); query = query.Where(t => t.Account.ToLowerInvariant().Contains(serach) || t.UserName.ToLowerInvariant().Contains(serach)); } //自定义排序 query = query.ApplySort(parameters.OrderBy, propertyMappingContainer.Resolve <UserViewModel, User>()); var count = await query.CountAsync(); var data = await query.Skip(parameters.PageIndex - 1 *parameters.PageSize).Take(parameters.PageSize).ToListAsync(); return(new PaginatedList <User>(parameters.PageIndex - 1, parameters.PageSize, count, data)); }
public async Task <PaginatedList <Post> > GetPostsAsync(PostParameters parameters) { var Query = _myContext.Posts.AsQueryable(); if (!string.IsNullOrEmpty(parameters.Title)) { var title = parameters.Title.ToLowerInvariant(); Query = Query.Where(x => x.Title.ToLowerInvariant().Contains(title)); } Query = Query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve <PostDTO, Post>()); // Query = Query.OrderBy(x => x.Id); var count = await Query.CountAsync(); var data = await Query.Skip(parameters.PageIndex *parameters.PageSize).Take(parameters.PageSize).ToListAsync(); return(new PaginatedList <Post>(parameters.PageIndex, parameters.PageSize, count, data)); }
public async Task <PaginatedList <Post> > RetriveAllEntityAsync(PostParameter postParameter) { var query = _context.Post.AsQueryable(); if (!string.IsNullOrEmpty(postParameter.Title)) { var title = postParameter.Title.ToLowerInvariant(); query = query.Where(p => p.Title.ToLowerInvariant() == title); } query = query.ApplySort(postParameter.OrderBy, _propertyMappingContainer.Resolve <PostModel, Post>()); var count = await query.CountAsync(); var data = await query.Skip(postParameter.PageIndex *postParameter.PageSize) .Take(postParameter.PageSize) .ToListAsync(); return(new PaginatedList <Post>(postParameter.PageIndex, postParameter.PageSize, count, data)); }
public async Task <PaginatedList <Product> > GetProductsAsync(ProductResourceParameters parameters) { var query = _myContext.Products.AsQueryable(); if (!string.IsNullOrEmpty(parameters.Name)) { var name = parameters.Name.Trim().ToLowerInvariant(); query = query.Where(x => x.Name.ToLowerInvariant() == name); } query = query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve <ProductResource, Product>()); var count = await query.CountAsync(); var items = await query .Skip(parameters.PageSize *parameters.PageIndex) .Take(parameters.PageSize).ToListAsync(); return(new PaginatedList <Product>(parameters.PageIndex, parameters.PageSize, count, items)); }
public async Task <PaginatedList <Post> > GetAllPostsAsync(PostParameters parameters) { var query = _myContext.Posts.AsQueryable(); if (!string.IsNullOrEmpty(parameters.Title)) { var whereStr = parameters.Title.ToLower(); query = query.Where(x => x.Title.ToLower().Contains(parameters.Title)); } query = query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve <Resources.PostResource, Post>()); var totalCount = query.Count(); var data = await query .Skip((parameters.PageIndex - 1) *parameters.PageSize) .Take(parameters.PageSize) .ToListAsync(); return(new PaginatedList <Post>(parameters.PageSize, parameters.PageIndex, totalCount, data)); }
public async Task <PaginatedList <Post> > GetAllPostsAsync(PostParameters postParameters) { var query = _myContext.Posts.AsQueryable(); if (!string.IsNullOrWhiteSpace(postParameters.Title)) { var title = postParameters.Title.ToLowerInvariant(); query = query.Where(x => x.Title.ToLowerInvariant() == title); } query = query.ApplySort(postParameters.OrderBy, _propertyMappingContainer.Resolve <PostResource, Post>()); var count = await query.CountAsync(); var data = await query.Skip(postParameters.PageIndex *postParameters.PageSize) .Take(postParameters.PageSize) .ToListAsync(); return(new PaginatedList <Post>(postParameters.PageIndex, postParameters.PageSize, count, data)); }
public async Task <PaginatedList <Post> > GetAllPostsAsync(PostQueryParameters postQueryParameters) { var query = myContext.Posts.AsQueryable(); #region 根据需求 过滤相关字段 if (!string.IsNullOrEmpty(postQueryParameters.Title)) { var title = postQueryParameters.Title.Trim().ToLowerInvariant(); //直接检索 //query = query.Where(x => x.Title.ToLowerInvariant() == title); //模糊查询 query = query.Where(x => x.Title.ToLowerInvariant().Contains(title)); //将pageindex设置为第一页起始,防止出现检索的数据不显示的问题 //需要考虑一下怎么解决 //postQueryParameters.PageIndex = 0; } #endregion //query = query.OrderBy(x => x.id); //传入参数,实现属性排序,传入需要排序ResourceModel的属性 query = query.ApplySort(postQueryParameters.OrderBy, propertyMappingContainer.Resolve <PostResource, Post>()); var count = await query.CountAsync(); var data = await query .Skip(postQueryParameters.PageIndex *postQueryParameters.PageSize) .Take(postQueryParameters.PageSize) .ToListAsync(); var paginatedList = new PaginatedList <Post>(postQueryParameters.PageIndex, postQueryParameters.PageSize, count, data); return(paginatedList); //return await myContext.Posts.ToListAsync(); }
public async Task <PaginatedList <Post> > GetPostsAsync(PostParameters postParameters) { var query = _myContext.Posts.AsQueryable(); // 客户端有可能根据标题 Title 查询 if (!string.IsNullOrEmpty(postParameters.Title)) { string title = postParameters.Title.ToLowerInvariant(); query = query.Where(p => p.Title.Contains(title)); } //query = query.OrderBy(p => p.Id); query = query.ApplySort(postParameters.OrderBy, _propertyMappingContainer.Resolve <PostResource, Post>()); int count = await query.CountAsync(); var data = await query .Skip((postParameters.PageIndex - 1) *postParameters.PageSize) .Take(postParameters.PageSize).ToListAsync(); return(new PaginatedList <Post>(postParameters.PageIndex, postParameters.PageSize, count, data)); }
public async Task <PaginatedList <Score> > GetAllScoresAsync(ScoreParameters scoreParameters) { var query = _myContext.Scores.AsQueryable(); if (!string.IsNullOrEmpty(scoreParameters.GameTitle)) { var title = scoreParameters.GameTitle.ToLowerInvariant(); query = query.Where(x => x.GameTitle.ToLowerInvariant().Contains(title)); } query = query.ApplySort(scoreParameters.OrderBy, _propertyMappingContainer.Resolve <ScoreResource, Score>()); var count = await query.CountAsync(); var data = await query .Skip(scoreParameters.PageIndex *scoreParameters.PageSize) .Take(scoreParameters.PageSize) .ToListAsync(); return(new PaginatedList <Score>(scoreParameters.PageIndex, scoreParameters.PageSize, count, data)); }
public async Task <PagedList <SysUser> > GetAllUsersAsync(UserParameters parameters) { var query = _userRepository.Query("Role"); if (!string.IsNullOrEmpty(parameters.UserName)) { var userName = parameters.UserName.ToLowerInvariant(); query = query.Where(x => x.UserName.ToLowerInvariant().Contains(userName)); } if (!string.IsNullOrEmpty(parameters.RealName)) { var realName = parameters.RealName.ToLowerInvariant(); query = query.Where(x => x.RealName.ToLowerInvariant().Contains(realName)); } if (parameters.Status.HasValue) { var status = parameters.Status.Value; query = query.Where(x => x.Status == status); } if (!string.IsNullOrEmpty(parameters.CreateTime)) { var time = parameters.CreateTime.Trim().Split('-'); var start = DateTime.Parse(time[0] + '-' + time[1] + '-' + time[2]); var end = DateTime.Parse(time[3] + '-' + time[4] + '-' + time[5]); query = query.Where(x => x.CreateTime >= start && x.CreateTime <= end); } //var query = _dbContext.Posts.OrderBy(x => x.Id); //query = query.OrderBy(x => x.Id); query = query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve <UserViewModel, SysUser>()); //排序 var count = await query.CountAsync(); var data = await query .Skip((parameters.PageIndex - 1) *parameters.PageSize) .Take(parameters.PageIndex * parameters.PageSize) .ToListAsync(); return(new PagedList <SysUser>(parameters.PageIndex, parameters.PageSize, count, data)); }
public async Task <IActionResult> Get([FromQuery] LoverAlbumParameters parameters) { PaginatedList <LoverAlbum> albums = await _albumRepository.GetLoverAlbumsAsync(this.GetUserId(), parameters); IQueryable <LoverAlbum> sortedAlbums = albums.AsQueryable() .ApplySort( parameters.OrderBy, _propertyMappingContainer.Resolve <LoverAlbumResource, LoverAlbum>()); IEnumerable <LoverAlbumResource> albumResources = _mapper.Map <IEnumerable <LoverAlbumResource> >(sortedAlbums); foreach (LoverAlbumResource source in albumResources) { source.PhotosCount = await _albumRepository.GetPhotosCount(source.Id); LoverPhoto converImage = await _albumRepository.GetCoverImage(source.Id); if (converImage != null) { source.CoverImageUrl = Url.LinkRelative("GetPhoto", new { id = converImage.Id }); } } IEnumerable <ExpandoObject> shapedAlbumResources = albumResources .ToDynamicObject(parameters.Fields) .AddLinks( this, parameters.Fields, "album", "GetLoverAlbum", "DeleteLoverAlbum", "PartiallyUpdateLoverAlbum"); var result = new { value = shapedAlbumResources, links = this.CreatePaginationLinks("GetLoverAlbums", parameters, albums.HasPrevious, albums.HasNext) }; this.AddPaginationHeaderToResponse(albums); return(Ok(result)); }
public async Task <PaginatedList <Dictionaries> > GetAllDicAsync(DictionariesParameters parameters) { var query = context.Dictionaries.AsQueryable(); //搜索,按登录名、用户名 if (!string.IsNullOrEmpty(parameters.Search)) { var search = parameters.Search.ToLowerInvariant(); query = query.Where(t => t.DataCode.ToLowerInvariant().Contains(search) || t.EnglishName.ToLowerInvariant().Contains(search) || t.NativeName.ToLowerInvariant().Contains(search) ); } //自定义排序 query = query.ApplySort(parameters.OrderBy, propertyMappingContainer.Resolve <DictionariesViewModel, Dictionaries>()); var count = await query.CountAsync(); var data = await query.Skip(parameters.PageIndex - 1 *parameters.PageSize).Take(parameters.PageSize).ToListAsync(); return(new PaginatedList <Dictionaries>(parameters.PageIndex - 1, parameters.PageSize, count, data)); }
public async Task <PaginatedList <Post> > GetAllPostsAsync(PostParameters parameters) { var query = _myContext.Posts.AsQueryable(); if (!string.IsNullOrEmpty(parameters.Title)) { var title = parameters.Title.ToLowerInvariant(); query = query.Where(x => x.Title.ToLowerInvariant() == title); // 字段查询 } //query = query.OrderBy(x=>x.Id); //自定义排序扩展方法 query = query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve <PostResource, Post>()); var totalItemsCount = await query.CountAsync(); var data = await query.Skip(parameters.PageIndex *parameters.PageSize) .Take(parameters.PageSize).ToListAsync(); var paginatedList = new PaginatedList <Post>(parameters.PageIndex, parameters.PageSize, totalItemsCount, data); return(paginatedList); }
public virtual async Task <PaginatedList <TEntity> > GetListByPagingParameterAsync <TResource>( PagingParam parameter) where TResource : class { var query = Table.AsQueryable(); var filter = parameter.BuildFilterCondition(); if (filter != null) { query = query.Where(filter.Item1, filter.Item2); } query = query.ApplySort(parameter.OrderBy, _pmContainer.Resolve <TResource, TEntity, TPrimaryKey>()); var count = await query.CountAsync(); var data = await query .Skip(parameter.PageIndex) .Take(parameter.PageSize) .ToListAsync(); return(new PaginatedList <TEntity>(parameter.PageIndex, parameter.PageSize, count, data)); }
public async Task <IActionResult> Get([FromQuery] LoverLogParameters parameters) { string userId = this.GetUserId(); PaginatedList <LoverLog> logs = await _repository.GetLoverLogsAsync(userId, parameters); IQueryable <LoverLog> sortedLogs = logs.AsQueryable() .ApplySort( parameters.OrderBy, _propertyMappingContainer.Resolve <LoverLogResource, LoverLog>()); IEnumerable <LoverLogResource> loverLogResources = _mapper.Map <IEnumerable <LoverLogResource> >(sortedLogs) .Select(x => { Parallel.ForEach(x.LoverPhotos, photo => { photo.Url = Url.LinkRelative("GetPhoto", new { id = photo.Id }); }); return(x); }); IEnumerable <ExpandoObject> shapedLoverLogResources = loverLogResources.ToDynamicObject(parameters.Fields) .AddLinks(this, parameters.Fields, "GetLoverLog", "DeleteLoverLog", "PartiallyUpdateLoverLog"); var result = new { value = shapedLoverLogResources, links = this.CreatePaginationLinks("GetLoverLogs", parameters, logs.HasPrevious, logs.HasNext) }; this.AddPaginationHeaderToResponse(logs); return(Ok(result)); }
GetProducts(ProductQuery parameters) { var query = _context.Products.AsQueryable(); if (!string.IsNullOrEmpty(parameters.Name)) { var name = parameters.Name.Trim().ToLowerInvariant(); query = query.Where(x => x.Name.ToLowerInvariant() == name); } if (!string.IsNullOrEmpty(parameters.Description)) { var description = parameters.Description.Trim().ToLowerInvariant(); query = query.Where(x => x.Description.ToLowerInvariant().Contains(description)); } #if (RESTFULAPIHELPER) query = query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve <ProductDTO, Product>()); #endif var count = await query.CountAsync(); var data = await query #if (RESTFULAPIHELPER) .Skip(parameters.PageSize *parameters.PageIndex) .Take(parameters.PageSize) #endif .AsNoTracking().ToListAsync(); #if (RESTFULAPIHELPER) return(new PagedListBase <Product>(parameters.PageIndex, parameters.PageSize, count, data)); #else return(data); #endif }
public async Task <PaginatedList <Stock> > GetAllAsync(StockParameters parameters) { var query = _myContext.Stocks.Include(x => x.StockCategories) .ThenInclude(x => x.Category) .AsQueryable(); //if (!string.IsNullOrEmpty(parameters.Title)) //{ // var title = parameters.Title.ToLowerInvariant(); // query = query.Where(x => x.Title.ToLowerInvariant().Contains(title)); //} query = query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve <StockResource, Stock>()); var count = await _myContext.Stocks.CountAsync(); var data = await query .Skip(parameters.PageIndex *parameters.PageSize) .Take(parameters.PageSize) .ToListAsync(); return(new PaginatedList <Stock>(parameters.PageIndex, parameters.PageSize, count, data)); }