public override async Task <IList <ProductViewModel> > ExecuteQuery(ListProductsQuery query, CancellationToken cancellationToken)
        {
            IList <ProductViewModel> productsViewMiodel = new List <ProductViewModel>();
            var products = await _unitOfWork.ProductRepository.ListAll(cancellationToken);

            if (string.IsNullOrEmpty(query.Currency))
            {
                throw new InvalidDataException("Currency code cannot be empty.");
            }

            var currency = Currency.FromCode(query.Currency);

            foreach (var product in products)
            {
                var convertedPrice = _currencyConverter.Convert(currency, product.Price);

                productsViewMiodel.Add(new ProductViewModel
                {
                    Id             = product.Id,
                    Name           = product.Name,
                    Price          = convertedPrice.Value.ToString(),
                    CurrencySymbol = currency.Symbol
                });
            }

            return(productsViewMiodel);
        }
Example #2
0
 public ProductTests()
 {
     _getCategoryBaseQuery     = new GetCategoryBaseQuery(DbContext, Cache);
     _getProductQuery          = new GetProductQuery(DbContext, _getCategoryBaseQuery);
     _listProductsQuery        = new ListProductsQuery(DbContext);
     _getProductQueryHandler   = new GetProductQueryHandler(_getProductQuery);
     _listProductsQueryHandler = new ListProductsQueryHandler(_listProductsQuery);
 }
Example #3
0
        public async Task <IActionResult> GetProducts([FromRoute] string currency)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var query = new ListProductsQuery(currency);

            return(Response(await _mediator.Send(query)));
        }
Example #4
0
        public async Task <IActionResult> Get([FromQuery] ListProductsQuery query)
        {
            if (User.IsInRole("Distributor"))
            {
                query.DistributorId = User?.FindFirstValue("BusinessUserId");
            }

            var result = await Mediator.Send(query);

            return(Ok(result));
        }
Example #5
0
        public async Task ShouldListProducts()
        {
            // Arrange

            // Create product brand
            var brandCommand = new CreateBrandCommand {
                Name = "Test Brand"
            };
            var brandId = await SendAsync(brandCommand);

            // Create product category
            var productCategoryCommand = new CreateProductCategoryCommand {
                Name = "Test Product Category"
            };
            var productCategoryId = await SendAsync(productCategoryCommand);

            // Create product
            var createFirstProductCommand = new CreateProductCommand
            {
                AvailableToSell = true,
                // created brand id
                BrandId = brandId,
                // created product category id
                ProductCategoryId = productCategoryId,
                Name     = "Test Product",
                PhotoUrl = "Test Product",
                Barcode  = "Test Product"
            };

            // Create product
            var createSecondProductCommand = new CreateProductCommand
            {
                AvailableToSell = true,
                // created brand id
                BrandId = brandId,
                // created product category id
                ProductCategoryId = productCategoryId,
                Name     = "Test Product",
                PhotoUrl = "Test Product",
                Barcode  = "Test Product"
            };

            await SendAsync(createFirstProductCommand);
            await SendAsync(createSecondProductCommand);

            var productsQuery = new ListProductsQuery();
            var products      = await SendAsync(productsQuery);

            products.Data.Should().NotBeNull();
            products.Data.Should().HaveCount(2);
        }
Example #6
0
    public async Task <IActionResult> GetProducts([FromRoute] string currency)
    {
        var query = new ListProductsQuery(currency);

        return(await Response(query));
    }