Ejemplo n.º 1
0
        public async Task <ActionResult <Pageination <ProductForViewDto> > > GetProducts([FromQuery] ProductParams productParams)
        {
            // var products = await _repository.GetProducts ();

            var spec      = new ProductWithTypeAndBrandSpecification(productParams);
            var countSpec = new ProductWithCountSpecification(productParams);

            var count = await _productRepo.GetCountAsync(countSpec);

            var products = await _productRepo.GetEntityListWithSpec(spec);

            var data = _mapper.Map <IReadOnlyList <Product>, IReadOnlyList <ProductForViewDto> >(products);

            var result = new Pageination <ProductForViewDto>(productParams.PageIndex, productParams.PageSize, count, data);

            return(Ok(result));
        }
Ejemplo n.º 2
0
        public Page <K> FindPage <K>(DbContext dbContext, Pageination page)
        {
            if (page.PageIndex == 0)
            {
                page.PageIndex = 1;
            }
            if (page.PageSize == 0)
            {
                page.PageIndex = 10;
            }

            SqlParameter[] paras = new SqlParameter[11];
            paras[0]       = new SqlParameter("strTable", DbType.String);
            paras[0].Value = page.StrTable;

            paras[1]       = new SqlParameter("strField", DbType.String);
            paras[1].Value = page.StrField;

            paras[2]       = new SqlParameter("pageSize", DbType.Int16);
            paras[2].Value = page.PageSize;

            paras[3]       = new SqlParameter("pageIndex", DbType.Int16);
            paras[3].Value = page.PageIndex;

            paras[4]       = new SqlParameter("strWhere", DbType.String);
            paras[4].Value = page.StrWhere;

            paras[5]       = new SqlParameter("strSortKey", DbType.String);
            paras[5].Value = page.StrSortKey;

            paras[6]       = new SqlParameter("strSortField", DbType.String);
            paras[6].Value = page.StrSortField;

            paras[7]       = new SqlParameter("strOrderBy", DbType.Boolean);
            paras[7].Value = page.StrOrderBy;

            paras[8]       = new SqlParameter("strGroupField", DbType.String);
            paras[8].Value = page.StrGroupField;

            paras[9]           = new SqlParameter("RecordCount", DbType.Int16);
            paras[9].Value     = page.RecordCount;
            paras[9].Direction = ParameterDirection.Output;

            paras[10]           = new SqlParameter("UsedTime", DbType.Int16);
            paras[10].Value     = page.UsedTime;
            paras[10].Direction = ParameterDirection.Output;

            string   sql    = @"exec Pager @strTable,@strField,@pageSize,@pageIndex,@strWhere,@strSortKey,@strSortField,@strOrderBy,@strGroupField,@RecordCount output,@UsedTime output";
            List <K> result = dbContext.Database.SqlQuery <K>(sql, paras).ToList();

            Page <K> p = new Page <K>
            {
                TotalRecord = (int)paras[9].Value,
                UsedTime    = (int)paras[10].Value,
                ResultList  = result,
                page        = page.PageIndex,
                rows        = page.PageSize
            };

            p.PageCount = (p.TotalRecord + p.rows - 1) / p.rows;

            return(p);
        }