public IHttpActionResult GetProduct(string productName)
        {
            ProductResponseDto product = _productsMapper.ToDto(_productService.FindProduct(productName));

            if (product == null)
            {
                return(NotFound());
            }

            return(Ok(product));
        }
Beispiel #2
0
        public async Task GetProductShouldReturnNotFound()
        {
            // Arrange
            ProductResponseDto dto = null;
            var responseDto        = new QueryResponseDto <ProductResponseDto>(dto);

            _productInterface.Setup(x => x.GetById(It.IsAny <Guid>())).ReturnsAsync(responseDto);

            var controller = new ProductsController(_productInterface.Object);

            // Act
            var response = await controller.GetProduct(Guid.NewGuid());

            // Assert
            Assert.IsInstanceOfType(response, typeof(NotFoundResult));
        }
        // POST: odata/Products1
        public async Task <IHttpActionResult> Post(ProductResponseDto product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (product == null)
            {
                return(BadRequest("Product is required"));
            }
            await productServices.AddAsync(new AddProductDto
            {
                Name        = product.Name,
                Description = product.Description,
                Price       = product.Price,
                CategoryId  = product.Category?.Id ?? 0
            });

            UOW.Commit();
            return(Created(product));
        }
Beispiel #4
0
        public IHttpActionResult PutProducts([FromBody] IEnumerable <ProductDto> productDtos)
        {
            if (productDtos == null || !productDtos.Any())
            {
                return(BadRequest("Empty product data in request body."));
            }

            foreach (var dto in productDtos)
            {
                Validate(dto);
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var productDtosWithId = productDtos
                                    .Where(p => p.Id != null)
                                    .Select(i => i.Id.GetValueOrDefault()).ToList();

            if (productDtosWithId.Count() > 0)
            {
                var productsToBeUpdated = _unitOfWork.Products.GetProductsByIds(productDtosWithId);
                var invalidProductIds   = productDtosWithId.Except(productsToBeUpdated.Select(p => p.Id));

                if (invalidProductIds.Count() > 0)
                {
                    return(BadRequest($"Product(s) not found for the following invalid product Id(s) in the list: {string.Join(",", invalidProductIds)}."));
                }
            }

            var processedProducts = new List <Product>();

            foreach (var productDto in productDtos)
            {
                if (productDto.Id.GetValueOrDefault() == 0)
                {
                    processedProducts.Add(_unitOfWork.Products.Add(Mapper.Map <ProductDto, Product>(productDto)));
                    continue;
                }

                var product = _unitOfWork.Products.GetProduct(productDto.Id.GetValueOrDefault());

                if (product != null)
                {
                    product.Modify(productDto.Name, productDto.Quantity, productDto.SaleAmount);
                    processedProducts.Add(product);
                }
            }

            _unitOfWork.Complete();

            var response = new ProductResponseDto()
            {
                Id        = Request.GetCorrelationId().ToString(),
                Timestamp = DateTime.UtcNow,
                Products  = Mapper.Map <List <Product>, List <ProductDto> >(processedProducts)
            };

            return(Ok(response));
        }