コード例 #1
0
        public OperationValidationResult SaveProductQuantity(string productName, string productQuantityString)
        {
            var result = new OperationValidationResult
            {
                Valid = true
            };

            var isParsed = int.TryParse(productQuantityString, out int parsedQuantity);

            if (!isParsed || parsedQuantity < 1)
            {
                result.Valid = false;
                result.AddErrorMessage("Quantity must be a whole positive number.");
                return(result);
            }

            try
            {
                var productFromDB = _candyStoreRepository.GetAll <Product>().FirstOrDefault(pro => pro.Name == productName);
                productFromDB.Count += parsedQuantity;
                _candyStoreRepository.Update(productFromDB);
            }
            catch (Exception ex)
            {
                result.Valid = false;
                result.AddErrorMessage(ex.Message);
                return(result);
            }

            return(result);
        }
コード例 #2
0
        public OperationValidationResult LoginAdministrator(string identificationNumberAsString)
        {
            var result = new OperationValidationResult
            {
                Valid = true
            };

            int identificationNumber;
            var parsed = int.TryParse(identificationNumberAsString, out identificationNumber);

            if (!parsed)
            {
                result.Valid = false;
                result.AddErrorMessage("Enter a correct whole number value.");

                return(result);
            }

            var user = _candyStoreRepository.GetAll <Employee>()
                       .FirstOrDefault(u => u.IdentificationNumber == identificationNumber);

            if (user == null)
            {
                result.Valid = false;
                result.AddErrorMessage("There is no such employee");
            }

            return(result);
        }
コード例 #3
0
        public OperationValidationResult AddNewProduct(string productPriceString, string productName, string categoryName, byte[] image)
        {
            var result = new OperationValidationResult
            {
                Valid = true
            };

            var isParsed = double.TryParse(productPriceString, out double productPrice);

            if (!isParsed || productPrice < 0)
            {
                result.Valid = false;
                result.AddErrorMessage("Price must be a positive number.");
                return(result);
            }

            if (image == null)
            {
                result.Valid = false;
                result.AddErrorMessage("Select an image for the product.");
                return(result);
            }

            if (string.IsNullOrEmpty(productName) || string.IsNullOrEmpty(categoryName))
            {
                result.Valid = false;
                result.AddErrorMessage("Type in a valid name for product and category.");
                return(result);
            }

            try
            {
                var product = new Product
                {
                    Name  = productName,
                    Price = productPrice
                };
                var category = _candyStoreRepository.GetAll <Category>().FirstOrDefault(c => c.Name == categoryName);
                product.Category     = category;
                product.ProductImage = image;

                _candyStoreRepository.Insert(product);
            }
            catch (Exception ex)
            {
                result.Valid = false;
                result.AddErrorMessage(ex.Message);
                return(result);
            }

            return(result);
        }
コード例 #4
0
        public OperationValidationResult AddProductToCart(string productIdString, string quantityString)
        {
            var result = new OperationValidationResult
            {
                Valid = true
            };

            bool parsedQuantity = int.TryParse(quantityString, out int quantityToNumber);

            if (!parsedQuantity || quantityToNumber <= 0)
            {
                result.Valid = false;
                result.AddErrorMessage("Quantity must be a whole positive number");
                return(result);
            }

            var confirmationResult = View.GetProductAddToCartConfirmationResult();

            if (!confirmationResult)
            {
                result.Valid = false;
                return(result);
            }

            var productId = int.Parse(productIdString);
            var product   = _candyStoreRepository.GetAll <Product>().FirstOrDefault(p => p.ProductID == productId);

            int productCountInSession = _session
                                        .Get <ShoppingCart>(Constants.SHOPPING_CART_KEY)
                                        .GetProductQuantity(product);

            if (product.Count - productCountInSession < quantityToNumber)
            {
                result.Valid = false;
                result.AddErrorMessage("Not enough quantity on stock");
                return(result);
            }

            _session.Get <ShoppingCart>(Constants.SHOPPING_CART_KEY).AddQuantityToProduct(product, quantityToNumber);

            result.Object = product.Count - _session.Get <ShoppingCart>(Constants.SHOPPING_CART_KEY)
                            .GetProductQuantity(product);

            return(result);
        }
コード例 #5
0
        public OperationValidationResult LoginCustomer(string firstName, string lastName)
        {
            var result = new OperationValidationResult
            {
                Valid = true
            };

            if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName))
            {
                result.Valid = false;
                result.AddErrorMessage("Some of the values are empty");
                return(result);
            }

            _session.Add(Constants.FIRST_NAME_KEY, firstName);
            _session.Add(Constants.LAST_NAME_KEY, lastName);
            _session.Add(Constants.SHOPPING_CART_KEY, new ShoppingCart());

            return(result);
        }
コード例 #6
0
        private OperationValidationResult PerformPlusValidationOperation(Product product, int productQuantityFromSession)
        {
            var result = new OperationValidationResult
            {
                Valid  = true,
                Object = product
            };

            var productCount = product.Count - productQuantityFromSession;

            if (productCount - 1 < 0)
            {
                result.Valid = false;
                result.AddErrorMessage("No more products in stock.");
            }

            View.UpdateTotalPrice(product.Price);

            return(result);
        }
コード例 #7
0
        public OperationValidationResult AddNewCategory(string name, byte[] image)
        {
            var result = new OperationValidationResult()
            {
                Valid = true
            };

            if (string.IsNullOrEmpty(name) || image == null)
            {
                result.Valid = false;
                result.AddErrorMessage("Category image or category name were not set");
                return(result);
            }

            _candyStoreRepository.Insert(new Category
            {
                Name          = name,
                CategoryImage = image
            });

            return(result);
        }
コード例 #8
0
        public OperationValidationResult PerformProdcutQuantityChange(int productId, string operation)
        {
            var result = new OperationValidationResult();

            var productFromDB = _candyStoreRepository.GetAll <Product>().FirstOrDefault(p => p.ProductID == productId);
            var productQuantityFromSession = ShoppingCart.GetProductQuantity(productFromDB);

            switch (operation)
            {
            case Constants.PLUS_OPERATION:
                result = PerformPlusValidationOperation(productFromDB, productQuantityFromSession);
                break;

            case Constants.MINUS_OPERATION:
                result = PerformMinusValidationOperation(productFromDB, productQuantityFromSession);
                break;

            default: throw new ArgumentException("Valid operations for quantity on the shopping cart are \"plus\" and \"minus\"");
            }

            return(result);
        }
コード例 #9
0
        public OperationValidationResult DeleteProduct(string productName)
        {
            var operationResult = new OperationValidationResult
            {
                Valid = true
            };

            var confirmationResult = View.GetConfirmationResult();

            if (!confirmationResult)
            {
                operationResult.Valid = false;
                return(operationResult);
            }

            if (string.IsNullOrEmpty(productName))
            {
                operationResult.Valid = false;
                operationResult.AddErrorMessage("You haven't selected product name");
                return(operationResult);
            }

            try
            {
                var productToDelete = _candyStoreRepository.GetAll <Product>().FirstOrDefault(p => p.Name == productName);
                _candyStoreRepository.Delete(productToDelete);
            }
            catch (Exception ex)
            {
                operationResult.Valid = false;
                operationResult.AddErrorMessage(ex.Message);
                return(operationResult);
            }

            return(operationResult);
        }
コード例 #10
0
        private OperationValidationResult PerformMinusValidationOperation(Product product, int productQuantityFromSession)
        {
            var result = new OperationValidationResult
            {
                Valid  = true,
                Object = product
            };

            if (productQuantityFromSession - 1 < 0)
            {
                var removalResult = View.ConfirmShoppingCartProductRemoval();
                if (removalResult)
                {
                    ShoppingCart.RemoveProduct(product);
                }
            }
            else
            {
                ShoppingCart.AddQuantityToProduct(product, -1);
                View.UpdateTotalPrice(-product.Price);
            }

            return(result);
        }