public async Task Customer_Null_BadRequest()
        {
            // Given
            var returnModel = new ReturnModel <int>();
            var model       = new ShoppingCartSave
            {
                CustomerInfo = new CustomerInfo
                {
                    CustomerId = 1
                }
            };

            var productServiceMock             = new Mock <IProductService>();
            var shoppingCartItemRepositoryMock = new Mock <IRepository <ShoppingCartItem> >();

            var customerServiceMock = new Mock <ICustomerService>();

            customerServiceMock.Setup(x => x.CheckCustomer(model.CustomerInfo.CustomerId.Value)).Returns(Task.FromResult(returnModel));

            var service    = new ShoppingCartItemService(productServiceMock.Object, customerServiceMock.Object, shoppingCartItemRepositoryMock.Object);
            var controller = new ShoppingCartController(service);

            // When
            var result = await controller.Post(model);

            // Assertions
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
        }
        public async Task Return_Ok()
        {
            // Given
            var cartItemReturnModel = new ReturnModel <ShoppingCartItem>()
            {
                Result = new ReturnResult {
                    Status = ReturnStatus.Success
                }
            };
            var model = new ShoppingCartSave
            {
                ProductId = 1
            };

            var shoppingCartItemServiceMock = new Mock <IShoppingCartItemService>();

            shoppingCartItemServiceMock.Setup(x => x.SaveCartItemAsync(model)).Returns(Task.FromResult(cartItemReturnModel));

            var controller = new ShoppingCartController(shoppingCartItemServiceMock.Object);

            // When
            var result = await controller.Post(model);

            // Assertions
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
        }
        public async Task <IActionResult> Post([FromBody] ShoppingCartSave item)
        {
            var saveCartItem = await _shoppingCartItemService.SaveCartItemAsync(item);

            if (saveCartItem.Result.Status == ReturnStatus.Success)
            {
                return(Ok(saveCartItem));
            }
            else
            {
                return(BadRequest(saveCartItem));
            }
        }
        public void Allow_Empty_DeliveryDateTime()
        {
            // Given
            var shoppingCartItemModel = new ShoppingCartSave
            {
                CustomerInfo = new CustomerInfo
                {
                    CustomerCode = "ABC"
                },
                ProductId = 1
            };

            // When
            var result = Validator.Validate(shoppingCartItemModel);

            // Assertions
            Assert.IsFalse(result.Errors.Any(x => x.PropertyName == "DeliveryDateTime"));
        }
        public void Not_Allow_Empty_Quantity()
        {
            // Given
            var shoppingCartItemModel = new ShoppingCartSave
            {
                CustomerInfo = new CustomerInfo
                {
                    CustomerCode = "ABC"
                },
                ProductId        = 1,
                DeliveryDateTime = DateTime.Now
            };

            // When
            var result = Validator.Validate(shoppingCartItemModel);

            // Assertions
            Assert.IsTrue(result.Errors.Any(x => x.PropertyName == "Quantity"));
        }
        public void CustomerId_May_Be_Empty()
        {
            // Given
            var shoppingCartItemModel = new ShoppingCartSave
            {
                CustomerInfo = new CustomerInfo
                {
                    CustomerCode = "A"
                },
                Quantity         = 1,
                DeliveryDateTime = DateTime.Now,
                ProductId        = 1
            };

            // When
            var result = Validator.Validate(shoppingCartItemModel);

            // Assertions
            Assert.IsTrue(!result.Errors.Any());
        }
        public async Task SaveCartItem_Error_BadRequest()
        {
            // Given
            var returnModel = new ReturnModel <ShoppingCartItem>();
            var model       = new ShoppingCartSave
            {
                ProductId = 1
            };

            var shoppingCartItemServiceMock = new Mock <IShoppingCartItemService>();

            shoppingCartItemServiceMock.Setup(x => x.SaveCartItemAsync(model)).Returns(Task.FromResult(returnModel));

            var controller = new ShoppingCartController(shoppingCartItemServiceMock.Object);

            // When
            var result = await controller.Post(model);

            // Assertions
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
        }
        public void CustomerInfo_Both_Cannot_Be_Full()
        {
            // Given
            var shoppingCartItemModel = new ShoppingCartSave
            {
                CustomerInfo = new CustomerInfo
                {
                    CustomerCode = "ABC",
                    CustomerId   = 1
                },
                Quantity         = 1,
                DeliveryDateTime = DateTime.Now
            };

            // When
            var result = Validator.Validate(shoppingCartItemModel);

            // Assertion
            string[] proprtyNames = { "CustomerInfo.CustomerCode", "CustomerInfo.CustomerId" };
            Assert.IsTrue(result.Errors.Any(x => proprtyNames.Contains(x.PropertyName)));
        }
        public async Task Product_Not_Valid_BadRequest()
        {
            // Given
            var returnModel      = new ReturnModel <ValidProductDto>();
            var validReturnModel = new ReturnModel <int>()
            {
                Result = new ReturnResult {
                    Status = ReturnStatus.Success
                }
            };
            var model = new ShoppingCartSave
            {
                ProductId    = 1,
                CustomerInfo = new CustomerInfo
                {
                    CustomerId = 1
                }
            };

            var shoppingCartItemRepositoryMock = new Mock <IRepository <ShoppingCartItem> >();

            var customerServiceMock = new Mock <ICustomerService>();

            customerServiceMock.Setup(x => x.CheckCustomer(model.CustomerInfo.CustomerId.Value)).Returns(Task.FromResult(validReturnModel));

            var productServiceMock = new Mock <IProductService>();

            productServiceMock.Setup(x => x.ValidForSale(model.ProductId)).Returns(Task.FromResult(returnModel));

            var service    = new ShoppingCartItemService(productServiceMock.Object, customerServiceMock.Object, shoppingCartItemRepositoryMock.Object);
            var controller = new ShoppingCartController(service);

            // When
            var result = await controller.Post(model);

            // Assertions
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
        }
        public async Task <ReturnModel <ShoppingCartItem> > SaveCartItemAsync(ShoppingCartSave item)
        {
            var model = new ReturnModel <ShoppingCartItem>();

            if (item.CustomerInfo.CustomerId.HasValue)
            {
                var customerCheck = await _customerService.CheckCustomer(item.CustomerInfo.CustomerId.Value);

                if (customerCheck.Result.Status != ReturnStatus.Success)
                {
                    model.Result = customerCheck.Result;
                    return(model);
                }
            }

            var productValid = await _productService.ValidForSale(item.ProductId);

            if (productValid.Result.Status != ReturnStatus.Success)
            {
                model.Result = productValid.Result;
                return(model);
            }

            ShoppingCartItem shoppingCartItem = await _shoppingCartItemRepository.Table.FirstOrDefaultAsync(x => ((!string.IsNullOrEmpty(item.CustomerInfo.CustomerCode) && x.CustomerCode == item.CustomerInfo.CustomerCode) || ((item.CustomerInfo.CustomerId.HasValue && x.CustomerId == item.CustomerInfo.CustomerId))) && x.ProductId == item.ProductId);

            bool insert = false;

            //Ürün ilk kez sepete atılıyor.
            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    CurrentPriceInclTax = productValid.Data.PriceInclTax,
                    ProductId           = item.ProductId,
                    Quantity            = item.Quantity,
                    CreateDate          = DateTime.Now,
                    UpdateDate          = DateTime.Now,
                    DeliveryDateTime    = item.DeliveryDateTime
                };

                if (item.CustomerInfo.CustomerId.HasValue)
                {
                    shoppingCartItem.CustomerId = item.CustomerInfo.CustomerId;
                }
                else
                {
                    shoppingCartItem.CustomerCode = item.CustomerInfo.CustomerCode;
                }

                insert = true;
            }
            //Ürün zaten müşterinin sepetinde var.
            else
            {
                shoppingCartItem.CurrentPriceInclTax = productValid.Data.PriceInclTax;
                shoppingCartItem.DeliveryDateTime    = item.DeliveryDateTime;
                shoppingCartItem.Quantity           += item.Quantity;
                shoppingCartItem.UpdateDate          = DateTime.Now;
            }

            int totalStock = productValid.Data.TotalStock;

            if (shoppingCartItem.Quantity > totalStock)
            {
                shoppingCartItem.Quantity = totalStock;
                model.Result.Message      = $"Üründen maksimum {totalStock} adet satın alınabilir. Sepete eklenen ürün sayınız {totalStock} olarak güncellenmiştir.";
            }

            try
            {
                if (insert)
                {
                    await _shoppingCartItemRepository.AddAsync(shoppingCartItem);
                }
                else
                {
                    await _shoppingCartItemRepository.SaveChangesAsync();
                }

                model.Data          = shoppingCartItem;
                model.Result.Status = ReturnStatus.Success;
            }
            catch
            {
                model.Result.Message = "Ürün sepete eklenirken problem meydana geldi!";
                //Log...
            }

            return(model);
        }