public BasketDTO AddProduct(ProductInsertDTO productInsertDTO)
        {
            if (productInsertDTO.ProductId > 0)
            {
                var newProductDTO = GetProductById(productInsertDTO.ProductId);

                for (int i = 0; i < productInsertDTO.Amount; i++)
                {
                    productInsertDTO.CurrentBasketProducts.Add(newProductDTO);
                }
            }

            decimal totalSum      = productInsertDTO.CurrentBasketProducts.Select(x => x.Price).Sum();
            var     productIdList = productInsertDTO.CurrentBasketProducts.Select(x => x.Id).ToList();
            var     discountDTO   = _discountService.CalculateDiscount(productIdList);

            BasketDTO basketDTO = new BasketDTO()
            {
                CurrentBasketProducts = productInsertDTO.CurrentBasketProducts,
                DiscountDTO           = discountDTO,
                TotalCost             = totalSum - discountDTO.TotalDiscount
            };

            _logService.LogBasketDetails(basketDTO);

            return(basketDTO);
        }
        public IActionResult Insert([FromForm] ProductInsertDTO dto)
        {
            var result = ProductService.Insert(dto);

            if (result)
            {
                return(Ok());
            }
            else
            {
                throw new Exception();
            }
        }
예제 #3
0
        public ActionResult <ProductViewDataDTO> InsertProduct(ProductInsertDTO p)
        {
            var product = _mapper.Map <Product>(p);

            product.AddedDate = DateTime.UtcNow;
            _repo.Insert(product);
            _repo.SaveChanges();
            var productViewData = _mapper.Map <ProductViewDataDTO>(product);

            return(CreatedAtRoute(nameof(GetProductById),
                                  new { id = productViewData.id },
                                  productViewData));
        }
예제 #4
0
        public void Test_AddProduct_Scenario6()
        {
            ProductInsertDTO productInsertDTO = new ProductInsertDTO
            {
                CurrentBasketProducts = new List <ProductDTO>(),
                ProductId             = milk.Id,
                Amount = 1
            };

            decimal expected  = 1.15M;
            var     basketDTO = _shoppingBasketService.AddProduct(productInsertDTO);

            Assert.AreEqual(expected, basketDTO.TotalCost);
        }
예제 #5
0
        public void Test_AddProduct_Scenario2()
        {
            ProductInsertDTO productInsertDTO = new ProductInsertDTO
            {
                CurrentBasketProducts = new List <ProductDTO>()
                {
                    butter, butter, bread, bread
                },
                ProductId = 0,
                Amount    = 0
            };

            decimal expected  = 3.1M;
            var     basketDTO = _shoppingBasketService.AddProduct(productInsertDTO);

            Assert.AreEqual(expected, basketDTO.TotalCost);
        }
예제 #6
0
        public bool Insert(ProductInsertDTO dto)
        {
            bool result    = false;
            var  imagePath = ImageExtension.SaveToCdn(dto.Img, CDNConfig.Cdn, CDNConfig.Path);
            var  pr        = new Product()
            {
                Name      = dto.Name,
                Price     = dto.Price,
                Quantity  = dto.Quantity,
                ImagePath = imagePath
            };

            int inserted = _repository.Insert(pr);

            if (inserted > 0)
            {
                result = true;
            }
            return(result);
        }
 public void AddProduct([FromBody] ProductInsertDTO productInsertDTO)
 {
     _shoppingBasketService.AddProduct(productInsertDTO);
 }