コード例 #1
0
        public IHttpResponse Edit(DetailedProductViewModel model)
        {
            if (model == null)
            {
                return(this.BadRequestErrorWithView("Please fill out the form"));
            }

            var product = this.Db.Products.FirstOrDefault(p => p.Id == model.Id);

            if (product == null)
            {
                return(this.BadRequestError("Product not found"));
            }

            var parseResult = Enum.TryParse <Models.Type>(model.Type, out Models.Type type);

            if (!parseResult)
            {
                return(this.BadRequestErrorWithView("Invalid product type"));
            }

            product.Name        = model.Name;
            product.Description = model.Description;
            product.Type        = type;
            product.Price       = model.Price;
            this.Db.SaveChanges();

            return(this.Redirect($"/Products/Details?id={model.Id}"));
        }
コード例 #2
0
        public void Create_InvalidModelState()
        {
            var _mockRepo   = new Mock <IStoreRepository>();
            var httpContext = new DefaultHttpContext();
            var tempData    = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>());

            tempData["adminLoc"] = storeLoc;
            var controller = new ProductController(_mockRepo.Object, new NullLogger <ProductController>())
            {
                TempData = tempData
            };

            controller.ModelState.AddModelError("", "Invalid input format");

            var viewDP = new DetailedProductViewModel
            {
                Name     = "Dying Light 3",
                Category = "Game",
                Price    = 79.99,
                Quantity = 100,
            };

            // act
            IActionResult actionResult = controller.Create(viewDP);

            // assert
            Assert.False(controller.ModelState.IsValid);
            Assert.Equal(2, controller.ModelState.ErrorCount);
            var viewResult = Assert.IsAssignableFrom <ViewResult>(actionResult);
        }
コード例 #3
0
        public IHttpResponse Create(DetailedProductViewModel model)
        {
            if (model == null)
            {
                return(this.BadRequestErrorWithView("Please fill out the form."));
            }

            var parseResult = Enum.TryParse <Models.Type>(model.Type, out Models.Type type);

            if (!parseResult)
            {
                return(this.BadRequestErrorWithView("Invalid product type"));
            }

            var product = new Product
            {
                Description = model.Description,
                Name        = model.Name,
                Price       = model.Price,
                Type        = type
            };

            this.Db.Products.Add(product);
            this.Db.SaveChanges();

            return(this.Redirect($"/Products/Details?id={product.Id}"));
        }
コード例 #4
0
        public ActionResult Create(DetailedProductViewModel viewDP)
        {
            string storeLoc = TempData.Peek("adminLoc").ToString();

            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("", "Invalid input format");
                    return(View());
                }
                // avoid duplicate
                var foundProduct = _storeRepo.GetOneProductByNameAndCategory(viewDP.Name, viewDP.Category);
                if (foundProduct != null)
                {
                    ModelState.AddModelError("", "This product already exist in this category");
                    return(View());
                }
                // a new randomly generated id for a new product
                string productID = Guid.NewGuid().ToString().Substring(0, 10);
                var    cProduct  = new CProduct(productID, viewDP.Name, viewDP.Category, viewDP.Price, viewDP.Quantity);
                _storeRepo.StoreAddOneProduct(storeLoc, cProduct, viewDP.Quantity);

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "error while tring to add a product");
                ModelState.AddModelError("", "failed to create a product");
                return(View());
            }
        }
コード例 #5
0
        public ViewResult Detailed(string id)
        {
            DetailedProductViewModel model = new DetailedProductViewModel
            {
                Product = productServ.GetProduct(id),
                //Reviews = reviewServ.GetReviews(id)
            };

            return(View(model));
        }
コード例 #6
0
        static public DetailedProductViewModel MapSingleDetailedProductWithoutTotal(CProduct product)
        {
            var viewProduct = new DetailedProductViewModel
            {
                UniqueID = product.UniqueID,
                Name     = product.Name,
                Category = product.Category,
                Price    = product.Price,
                Quantity = product.Quantity,
            };

            return(viewProduct);
        }
コード例 #7
0
        public void Create_ValidState()
        {
            // arrange
            var _mockRepo   = new Mock <IStoreRepository>();
            var httpContext = new DefaultHttpContext();
            var tempData    = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>());

            tempData["adminLoc"] = storeLoc;
            var controller = new ProductController(_mockRepo.Object, new NullLogger <ProductController>())
            {
                TempData = tempData
            };

            // bypass duplicate checking
            _mockRepo.Setup(x => x.GetOneProductByNameAndCategory(It.IsAny <string>(), It.IsAny <string>()))
            .Returns((CProduct)null);

            string   loc      = null;
            CProduct product  = null;
            int      quantity = 0;

            _mockRepo.Setup(x => x.StoreAddOneProduct(It.IsAny <string>(), It.IsAny <CProduct>(), It.IsAny <int>()))
            .Callback <string, CProduct, int>((x, y, z) =>
            {
                loc      = x;
                product  = y;
                quantity = z;
            });

            var viewDP = new DetailedProductViewModel
            {
                // ID is automatically assigned
                Name     = "Dying Light 3",
                Category = "Game",
                Price    = 79.99,
                Quantity = 100
            };

            // act
            IActionResult actionResult = controller.Create(viewDP);

            // assert
            Assert.True(controller.ModelState.IsValid);
            _mockRepo.Verify(r => r.StoreAddOneProduct(It.IsAny <string>(), It.IsAny <CProduct>(), It.IsAny <int>()), Times.Once);
            Assert.Equal(viewDP.Name, product.Name);
            Assert.Equal(viewDP.Category, product.Category);
            Assert.Equal(viewDP.Price, product.Price);
            Assert.Equal(viewDP.Quantity, quantity);
            Assert.IsAssignableFrom <RedirectToActionResult>(actionResult);
        }
コード例 #8
0
        private List <BasicPromotionInfo> GetPromotions(DetailedProductViewModel product)
        {
            var promotions = _unitOfWork.Promotion
                             .Get(p => p.ProductId == product.Id)
                             .OrderByDescending(p => p.ExpiresOn)
                             .Select(p => new BasicPromotionInfo()
            {
                ExpiresOn  = p.ExpiresOn,
                Percentage = p.Percentage,
                Value      = p.Value
            })
                             .ToList();

            return(promotions);
        }
コード例 #9
0
        public IHttpResponse Delete(DetailedProductViewModel model)
        {
            if (model == null)
            {
                return(this.BadRequestErrorWithView("Please fill out the form"));
            }

            var product = this.Db.Products.FirstOrDefault(p => p.Id == model.Id);

            if (product == null)
            {
                return(this.BadRequestError("Product not found"));
            }

            this.Db.Products.Remove(product);
            this.Db.SaveChanges();

            return(this.Redirect($"/"));
        }
コード例 #10
0
        private List <BasicProdutInfo> GetCompatibleProductDetails(DetailedProductViewModel product)
        {
            if (product.ProductGroupId != null)
            {
                var productsInGroup = _unitOfWork.Product.Get(p => p.ProductGroupId == product.ProductGroupId && p.Id != product.Id);
                var compatible      = productsInGroup
                                      .Select(p => new BasicProdutInfo()
                {
                    Id            = p.Id,
                    Name          = p.Name,
                    Code          = p.Code,
                    IsMainInGroup = p.IsMainInGroup
                })
                                      .OrderBy(b => b.Name)
                                      .ToList();

                return(compatible);
            }
            return(new List <BasicProdutInfo>());
        }
コード例 #11
0
        public IHttpResponse Edit(int id)
        {
            var product = this.Db.Products.FirstOrDefault(p => p.Id == id);

            if (product == null)
            {
                return(this.BadRequestError("Product not found"));
            }

            var model = new DetailedProductViewModel
            {
                Id          = product.Id,
                Name        = product.Name,
                Price       = Math.Round(product.Price, 2),
                Description = product.Description,
                Type        = product.Type.ToString()
            };

            return(this.View <DetailedProductViewModel>(model));
        }
コード例 #12
0
        public ActionResult Edit(string id, DetailedProductViewModel viewDP)
        {
            string storeLoc = TempData.Peek("adminLoc").ToString();

            try
            {
                if (!ModelState.IsValid)
                {
                    return(View());
                }

                // concurrent
                var foundProduct = _storeRepo.GetOneProduct(id);
                if (foundProduct == null)
                {
                    ModelState.AddModelError("", "Another admin has just deleted this product");
                    return(View());
                }

                // check if you have changed the name or category
                if (foundProduct.Name != viewDP.Name || foundProduct.Category != viewDP.Category)
                {
                    // see if the edited version already exist
                    var editedProduct = _storeRepo.GetOneProductByNameAndCategory(viewDP.Name, viewDP.Category);
                    if (editedProduct != null)
                    {
                        ModelState.AddModelError("", "A record with the same data already exist in this category");
                        return(View());
                    }
                }
                foundProduct = new CProduct(foundProduct.UniqueID, viewDP.Name, viewDP.Category, viewDP.Price);
                _storeRepo.EditOneProduct(storeLoc, foundProduct, viewDP.Quantity);
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "error while trying to edit a product");
                ModelState.AddModelError("", "failed to edit a product");
                return(View());
            }
        }
コード例 #13
0
        public ActionResult Delete(string id, DetailedProductViewModel viewDP)
        {
            string          path     = "../../SimplyWriteData.json";
            JsonFilePersist persist  = new JsonFilePersist(path);
            List <CProduct> products = persist.ReadProductsTempData(TempData.Peek("Cart").ToString());

            if (products == null)
            {
                return(RedirectToAction("CheckCart"));
            }
            foreach (var product in products)
            {
                if (product.UniqueID == id)
                {
                    products.Remove(product);
                    break;
                }
            }
            string cart = persist.WriteProductsTempData(products);

            TempData["Cart"] = cart;
            TempData.Keep("Cart");
            return(RedirectToAction("CheckCart"));
        }