IndexShouldThrowHttpExceptionWithCode404AndSpecifiedMessageIfAnyRepositoryThrewItemNotFoundException()
        {
            _stateRepository.GetStates().Throws(new ItemNotFoundException());
            var controller = new ProductSelectionController(_stateRepository, _categoryRepository, _productRepository);

            ActionResult Call() => controller.Index(ProductName, ExampleCorrectPreferredPriceInput,
                                                    ExampleCorrectCount);

            var exception = Xunit.Assert.Throws <HttpException>(Call);

            var code    = exception.GetHttpCode();
            var message = exception.Message;

            Assert.That(code, Is(EqualTo(Http404)));
            Assert.That(message, Is(EqualTo(ExpectedMessageOf404Code)));
        }
            IndexShouldThrowHttpExceptionWithCode500AndSpecifiedMessageForAccessToDbIssuesIfAnyRepositoryThrewAnAccessToNotConnectedDatabaseException()
            {
                _stateRepository.GetStates().Throws(new AccessToNotConnectedDatabaseException());
                var controller =
                    new CategorySelectionController(_categoryRepository, _stateRepository, _productRepository);

                ActionResult Call() => controller.Index(CategoryName);

                var exception = Xunit.Assert.Throws <HttpException>(Call);

                var code    = exception.GetHttpCode();
                var message = exception.Message;

                Assert.That(code, Is(EqualTo(Http500)));
                Assert.That(message, Is(EqualTo(ExpectedMessageOf500CodeForAccessTrouble)));
            }
        public void IndexPageShouldThrowHttpExceptionWithStatusCodeEqualTo500AndSpecifiedMessageIfAnyRepositoryThrowErrorWithDbConnection()
        {
            _stateRepository.GetStates().Throws(new AccessToNotConnectedDatabaseException());

            var controller = new HomeController(_categoryRepository, _stateRepository, _productRepository);

            ActionResult Call() => controller.Index();

            var exception = Xunit.Assert.Throws <HttpException>(Call);

            var code    = exception.GetHttpCode();
            var message = exception.Message;

            Assert.That(code, Is(EqualTo(Http500)));
            Assert.That(message, Is(EqualTo(ExpectedMessageOf500CodeForAccessTrouble)));
        }
示例#4
0
        public ActionResult Index()
        {
            try
            {
                var mainViewModel = new MainViewModel
                {
                    ProductSelectList  = new SelectList(_productRepository.GetProducts(), "Name", "Name"),
                    CategorySelectList = new SelectList(_categoryRepository.GetCategories(), "Name", "Name"),
                    StateSelectList    = new SelectList(_stateRepository.GetStates(), "Name", "Name")
                };

                return(View(mainViewModel));
            }
            catch (AccessToNotConnectedDatabaseException)
            {
                throw new HttpException(500, "Server encountered the problem with access to data");
            }
            catch (Exception)
            {
                throw new HttpException(500, "Server encountered some problems, please contact support");
            }
        }
        public ActionResult Index(string category)
        {
            if (category == null || string.IsNullOrEmpty(category))
            {
                throw new HttpException(403, "The server cannot process request due to malformed or empty syntax");
            }

            try
            {
                var productModels        = _productRepository.GetProducts();
                var categoryModels       = _categoryRepository.GetCategories();
                var stateOfAmericaModels = _stateRepository.GetStates();

                var mainViewModel = new MainViewModel
                {
                    ProductSelectList  = new SelectList(productModels, "Name", "Name"),
                    CategorySelectList = new SelectList(categoryModels, "Name", "Name"),
                    StateSelectList    = new SelectList(stateOfAmericaModels, "Name", "Name"),
                    ProductList        =
                        productModels.Where(product => product.Category.Name.Equals(category.Trim())).ToList(),
                    Category = category
                };

                return(View(mainViewModel));
            }
            catch (AccessToNotConnectedDatabaseException)
            {
                throw new HttpException(500, "Server encountered the problem with access to data");
            }
            catch (ItemNotFoundException)
            {
                throw new HttpException(404, "Requested resource does not exist");
            }
            catch (Exception)
            {
                throw new HttpException(500, "Server encountered some problems, please contact support");
            }
        }
示例#6
0
        public ActionResult Index(string product, string preferredPriceInput, int count)
        {
            if (product == null || string.IsNullOrEmpty(product) || preferredPriceInput == null ||
                string.IsNullOrEmpty(preferredPriceInput) || count < 1)
            {
                throw new HttpException(403, "The server cannot process request due to malformed or empty syntax");
            }

            try
            {
                var states     = _stateDatabase.GetStates();
                var products   = _productDatabase.GetProducts();
                var categories = _categoryDatabase.GetCategories();

                var chosenProduct = products.FirstOrDefault(p => p.Name.Equals(product.Trim())) ??
                                    throw new HttpException(404, "Requested resource does not exist");

                chosenProduct.PreferredPrice = ParsePrice(preferredPriceInput);

                var stateNameList = new List <string>();
                var finalPrice    = new List <double>();
                var tax           = new List <double>();
                var margin        = new List <double>();

                foreach (var state in states)
                {
                    // State Name
                    stateNameList.Add(state.Name);

                    // Product final price in current state
                    Algorithm.CalculateFinalPrice(chosenProduct, state, count);
                    finalPrice.Add(chosenProduct.FinalPrice);

                    // Tax for current state
                    tax.Add(Algorithm.GetTax(chosenProduct, state, count));

                    // Margin for chosen product in current state
                    margin.Add(Algorithm.CalculateMargin(chosenProduct, count));
                }

                var mainViewModel = new MainViewModel
                {
                    ProductSelectList  = new SelectList(products, "Name", "Name"),
                    CategorySelectList = new SelectList(categories, "Name", "Name"),
                    StateSelectList    = new SelectList(states, "Name", "Name"),
                    PurchasePrice      = Math.Round(chosenProduct.PurchasePrice, 2),
                    PreferredPrice     = chosenProduct.PreferredPrice,
                    NumberOfProducts   = count,
                    Tax           = tax,
                    Margin        = margin,
                    FinalPrice    = finalPrice,
                    StateNameList = stateNameList,
                    ChosenProduct = chosenProduct
                };

                return(View(mainViewModel));
            }
            catch (HttpException)
            {
                throw;
            }
            catch (AccessToNotConnectedDatabaseException)
            {
                throw new HttpException(500, "Server encountered the problem with access to data");
            }
            catch (ItemNotFoundException)
            {
                throw new HttpException(404, "Requested resource does not exist");
            }
            catch (Exception)
            {
                throw new HttpException(500, "Server encountered some problems, please contact support");
            }
        }
        // GET: StateSelection
        public ActionResult Index(string product, string state, string preferredPriceInput = "0", int count = 1)
        {
            if (string.IsNullOrEmpty(product) || string.IsNullOrEmpty(state) ||
                string.IsNullOrEmpty(preferredPriceInput) || count < 1)
            {
                throw new HttpException(403, "The server cannot process request due to malformed or empty syntax");
            }

            try
            {
                var productModels        = _productDatabase.GetProducts();
                var categoryModels       = _categoryDatabase.GetCategories();
                var stateOfAmericaModels = _stateDatabase.GetStates();

                var chosenState   = _stateDatabase.GetStateByName(state.Trim());
                var chosenProduct = _productDatabase.GetProductByName(product.Trim());

                chosenProduct.PreferredPrice = ParsePrice(preferredPriceInput);

                // State Name
                var stateNameList = new List <string> {
                    chosenState.Name
                };

                // Product final price in current state
                Algorithm.CalculateFinalPrice(chosenProduct, chosenState, count);
                var finalPriceList = new List <double> {
                    chosenProduct.FinalPrice
                };

                // Tax for current state
                var taxes = new List <double> {
                    Algorithm.GetTax(chosenProduct, chosenState, count)
                };

                // Margin for chosen product in current state
                var margins = new List <double> {
                    Algorithm.CalculateMargin(chosenProduct, count)
                };

                var mainViewModel = new MainViewModel
                {
                    ProductSelectList  = new SelectList(productModels, "Name", "Name"),
                    CategorySelectList = new SelectList(categoryModels, "Name", "Name"),
                    StateSelectList    = new SelectList(stateOfAmericaModels, "Name", "Name"),
                    Tax              = taxes,
                    Margin           = margins,
                    FinalPrice       = finalPriceList,
                    StateNameList    = stateNameList,
                    ChosenProduct    = chosenProduct,
                    PurchasePrice    = Math.Round(chosenProduct.PurchasePrice, 2),
                    PreferredPrice   = chosenProduct.PreferredPrice,
                    NumberOfProducts = count,
                    ChosenState      = chosenState
                };

                return(View(mainViewModel));
            }
            catch (AccessToNotConnectedDatabaseException)
            {
                throw new HttpException(500, "Server encountered the problem with access to data");
            }
            catch (ItemNotFoundException)
            {
                throw new HttpException(404, "Requested resource does not exist");
            }
            catch (Exception)
            {
                throw new HttpException(500, "Server encountered some problems, please contact support");
            }
        }