예제 #1
0
        public async Task <IActionResult> GetProducts([FromQuery] ProductQueryDto queryDto)
        {
            IEnumerable <ProductViewModel> products = default;

            if (!queryDto.Cacheable)
            {
                products = await _mediator.Send(queryDto);

                return(Ok(products));
            }

            var cacheKey = $"product-{queryDto.ProductName}-{queryDto.CategoryId}";

            products = await _redisCacheClient.GetAsync <IEnumerable <ProductViewModel> >(cacheKey);

            if (products != null)
            {
                Response.Headers.Add("X-DataSource", $"From-Cache");
                return(Ok(products));
            }

            if (products.Any())
            {
                await _redisCacheClient.AddAsync(cacheKey, products, 300);
            }

            return(Ok(products));
        }
        public async Task <ActionResult <List <Product> > > GetProducts([FromQuery] ProductQueryDto filterDto)
        {
            var filter   = _mapper.Map <ProductQueryDto, ProductQuery>(filterDto);
            var products = await _productRepo.GetAllProducts(filter);

            return(Ok(_mapper.Map <QueryResult <Product>, QueryResultDto <ProductDto> >(products)));
        }
예제 #3
0
        public async Task <IActionResult> GetValidProducts(ProductQueryDto productQueryDto)
        {
            var productQuery = mapper.Map <ProductQueryDto, ProductQuery>(productQueryDto);
            var queryResult  = await productService.GetValidProducts(productQuery);

            var resultQuery = mapper.Map <QueryResult <Product>, QueryResultDto <ProductDto> >(queryResult);

            return(Ok(resultQuery));
        }
예제 #4
0
        public async Task <IEnumerable <ProductDto> > GetListAsync(ProductQueryDto query)
        {
            var task = await Task.Run(() =>
            {
                Thread.Sleep(100);
                return(products.AsEnumerable());
            });

            return(task);
        }
예제 #5
0
        public async Task <IEnumerable <ProductDto> > GetListAsync(ProductQueryDto query)
        {
            var task = await Task.Run(() =>
            {
                Thread.Sleep(100);
                return(_products.Select(p => new ProductDto
                {
                    Id = p.Id,
                    Name = p.Name
                }).AsEnumerable());
            });

            return(task);
        }
예제 #6
0
        public ListResultDto <ProductListDto> GetAll(ProductQueryDto request)
        {
            var query = this.productRepository.GetAll()

                        .WhereIf(!string.IsNullOrWhiteSpace(request.Name), r => r.Name.Contains(request.Name) || r.PYCode.Contains(request.Name.ToUpper()))
                        .WhereIf(!string.IsNullOrWhiteSpace(request.Unit), r => r.Unit == request.Unit)
                        .WhereIf(request.CategoryId != Guid.Empty, r => r.CategoryId == request.CategoryId);

            query = !string.IsNullOrWhiteSpace(request.Sorting)
                        ? query.OrderBy(request.Sorting)
                        : query.OrderBy(r => r.Name);
            var count = query.Count();
            var list  = query.AsQueryable().PageBy(request).ToList();
            var dtos  = list.MapTo <List <ProductListDto> >();

            return(new PagedResultDto <ProductListDto>(count, dtos));
        }
예제 #7
0
        public async Task <IEnumerable <ProductListingViewModel> > GetProducts(ProductQueryDto query, CancellationToken cancellationToken = default)
        {
            // Format filter
            string filter = query.Filter == null
                ? string.Empty
                : query.Filter.Trim().ToLower();

            Expression <Func <Product, object> > sortPredicate = null;

            // Determine sort predicate
            switch (query.SortCriteria)
            {
            case SortCriteria.Bestseller:
                sortPredicate = product => - product.Orders.Count();
                break;

            case SortCriteria.BestRated:
                sortPredicate = product => - product.Reviews.Average(r => r.Stars);
                break;

            case SortCriteria.Cheapest:
                sortPredicate = product => product.Price;
                break;

            case SortCriteria.MostExpensive:
                sortPredicate = product => - product.Price;
                break;
            }

            // Query + map products
            IEnumerable <ProductListingViewModel> products = await _context.Products
                                                             .AsNoTracking()
                                                             .Include(p => p.Orders)
                                                             .Include(p => p.Images)
                                                             .Include(p => p.Reviews)
                                                             .Include(p => p.WishlistItems)
                                                             .Where(p => p.Title.ToLower().Contains(filter))
                                                             .OrderBy(sortPredicate)
                                                             .ProjectTo <ProductListingViewModel>(_mapper.ConfigurationProvider)
                                                             .ToListAsync(cancellationToken);

            return(products);
        }
예제 #8
0
        public async Task <IEnumerable <ProductViewModel> > Handle(ProductQueryDto request, CancellationToken cancellationToken)
        {
            var query = _productDbContext.Products.AsQueryable();

            if (!string.IsNullOrEmpty(request.ProductName))
            {
                query = query.Where(p => p.Name.StartsWith(request.ProductName));
            }

            if (request.CategoryId is not null)
            {
                query = query.Where(p => p.ProductCategoryId == request.CategoryId);
            }

            var products = await query
                           .ProjectTo <ProductViewModel>(_mapper.ConfigurationProvider)
                           .ToListAsync();

            return(products);
        }
예제 #9
0
        public async Task <IActionResult> GetProducts([FromQuery] ProductQueryDto queryObj)
        {
            var queryObject = _mapper.Map <ProductQuery>(queryObj);

            var query = _context.Products.Include(p => p.Category).AsQueryable();

            // filtering by title
            if (!String.IsNullOrWhiteSpace(queryObj.Title))
            {
                query = query.Where(p => p.Title == queryObj.Title);
            }

            //filter by category
            if (!String.IsNullOrWhiteSpace(queryObj.Category))
            {
                query = query.Where(p => p.Category.Name == queryObj.Category);
            }

            var columnsMap = new Dictionary <string, Expression <Func <Product, object> > >()
            {
                ["Title"] = p => p.Title,
                ["Price"] = p => p.Price
            };

            // sorting
            query = query.ApplySorting(columnsMap, queryObject);
            // pagination
            query = query.ApplyPagination(queryObject);

            var products = await query.ToListAsync();

            if (products == null)
            {
                return(NotFound());
            }
            return(Ok(_mapper.Map <List <ProductDto> >(products)));
        }
예제 #10
0
 public async Task <IEnumerable <ProductDto> > GetListAsync([FromQuery] ProductQueryDto query)
 {
     return(await _productService.GetListAsync(query));
 }
예제 #11
0
        public async Task <ActionResult <IEnumerable <ProductListingViewModel> > > GetProducts([FromQuery] ProductQueryDto query, CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            IEnumerable <ProductListingViewModel> products = await _productService.GetProducts(query, cancellationToken);

            return(Ok(products));
        }