Пример #1
0
        public async Task <PagedResult <ProductViewModel> > GetAllPaging(GetProductPagingRequest request)
        {
            // query
            var query = _context.Products.AsQueryable();

            //filter
            if (!string.IsNullOrEmpty(request.keyword))
            {
                query = query.Where(x => FormatVietnamese.convertToUnSign(x.name.ToLower())
                                    .Contains(FormatVietnamese.convertToUnSign(request.keyword.ToLower())));
            }
            if (request.categoryId.HasValue && request.categoryId.Value > 0)
            {
                query = query.Where(x => x.categoryId == request.categoryId.Value);
            }
            //pagination
            int totalRow = await query.CountAsync();

            var data = await query.Include(eva => eva.Evaluations.Where(e => e.status == EvaluationStatus.Confirm))
                       .Include(img => img.Images.Where(i => i.status == ActionStatus.Display))
                       .OrderBy(x => x.categoryId).Skip((request.pageIndex - 1) * request.pageSize)
                       .Take(request.pageSize).Select(rs => new ProductViewModel
            {
                id          = rs.id,
                name        = rs.name,
                price       = rs.price,
                importPrice = rs.importPrice,
                sale        = rs.sale,
                categoryId  = rs.categoryId,
                category    = rs.category,
                color       = rs.color,
                size        = rs.size,
                description = rs.description,
                Evaluations = rs.Evaluations,
                Images      = rs.Images,
                rating      = Convert.ToInt32(rs.Evaluations.Average(ave => ave.rating)),
                provider    = rs.provider,
                providerId  = rs.providerId,
                status      = rs.status
            }).ToListAsync();

            //
            var pagedResult = new PagedResult <ProductViewModel>
            {
                TotalRecord = totalRow,
                Items       = data
            };

            return(pagedResult);
        }
        public async Task <List <ProviderViewModel> > Search(string search)
        {
            var data = await _context.providers.ToListAsync();

            data = data.Where(ele => FormatVietnamese.convertToUnSign(ele.name.ToLower())
                              .Contains(FormatVietnamese.convertToUnSign(search.ToLower()))
                              ).ToList();

            return(data.Where(i => i.status == ActionStatus.Display).Select(rs => new ProviderViewModel
            {
                id = rs.id,
                name = rs.name,
                status = rs.status,
            }).ToList());
        }
        //



        public async Task <List <CategoryViewModel> > Search(string search)
        {
            var data = await _context.categories.ToListAsync();

            data = data.Where(ele => FormatVietnamese.convertToUnSign(ele.name.ToLower())
                              .Contains(FormatVietnamese.convertToUnSign(search.ToLower())) ||
                              FormatVietnamese.convertToUnSign(ele.generalityName.ToLower())
                              .Contains(FormatVietnamese.convertToUnSign(search.ToLower()))
                              ).ToList();

            return(data.Where(i => i.status == ActionStatus.Display).Select(rs => new CategoryViewModel
            {
                id = rs.id,
                name = rs.name,
                generalityName = rs.generalityName,
                status = rs.status,
                Products = rs.Products.Where(p => p.status == ActionStatus.Display).ToList()
            }).ToList());
        }