public ProductViewModel AutoMappingProductViewModel(Product p)
        {
            ProductViewModel model = new ProductViewModel();
            var soc = new SpecialOfferController();
            var rec = new RevisionController();

            //Add all revisions to product via viewmodels
            model.RevisionsInProduct = new List <RevisionViewModel>();
            foreach (Revision r in rec.RetrieveAllRevisionsByProductId(p.RetrieveProductId()))
            {
                var mapperOne            = mapextension.RevisionToRevisionViewModel();
                RevisionViewModel rmodel = mapperOne.Map <RevisionViewModel>(r);
                model.RevisionsInProduct.Add(rmodel);
            }

            //Add all categories from product to model via viewmodels
            model.ProductCategories = new List <CategoryOverviewViewModel>();
            foreach (Category c in categoryRep.RetrieveCategoriesFromProduct(p))
            {
                var mapperTwo = mapextension.CategoryToCategoryOverviewViewModel();
                CategoryOverviewViewModel cmodel = mapperTwo.Map <CategoryOverviewViewModel>(c);
                model.ProductCategories.Add(cmodel);
            }

            //Add all categories in system to model via viewmodels
            model.ListOfAllCategories = new List <CategoryOverviewViewModel>();
            foreach (Category c in categoryRep.RetrieveAllCategories())
            {
                var mapperThree = mapextension.CategoryToCategoryOverviewViewModel();
                CategoryOverviewViewModel cmodelsystem = mapperThree.Map <CategoryOverviewViewModel>(c);
                model.ListOfAllCategories.Add(cmodelsystem);
            }

            return(model);
        }
        public ActionResult Add(ProductViewModel model, List <int> selectedCategory, string uploadedImage)
        {
            /*New product to add*/
            var product = new Product(1, model.ProductName);

            product.ProductAmount     = model.ProductAmount;
            product.ProductDesc       = model.ProductDesc;
            product.ProductCategories = new List <Category>();
            product.ProductDiscount   = model.ProductDiscount;
            product.ProductPrice      = model.ProductPrice;
            product.ProductStatus     = model.ProductStatus;
            product.ImageUrl          = uploadedImage;
            product.BtwPercentage     = model.BtwPercentage;

            //Add all selected categories in system to model via viewmodels
            foreach (CategoryOverviewViewModel c in model.ProductCategories)
            {
                Category newcategory = categoryRep.RetrieveCategoryById(c.CategoryId);
                product.ProductCategories.Add(newcategory);
            }

            if (product.ProductDesc == null)
            {
                product.ProductDesc = "No description";
            }
            if (product.ImageUrl == null)
            {
                product.ImageUrl = "noimage.jpg";
            }
            if (model.ProductInStock == "Yes")
            {
                product.ProductInStock = 1;
            }
            else
            {
                product.ProductInStock = 0;
            }

            var offerController    = new SpecialOfferController();
            var revisionController = new RevisionController();

            /*Creating the product*/
            if (productRep.CreateNewProduct(product))
            {
                var selectedProduct = productRep.RetrieveProductByName(product.ProductName);
                InsertingCategoriesIntoProduct(selectedCategory, selectedProduct);
                offerController.Add(model, selectedProduct);
                revisionController.Add(selectedProduct);
                return(RedirectToAction("Overview", "Product"));
            }
            return(RedirectToAction("Single", "Product"));
        }
        public ActionResult Update(ProductViewModel p, int productToEdit, List <int> selectedCategory, string uploadedImage)
        {
            var soc             = new SpecialOfferController();
            var selectedProduct = productRep.RetrieveProductById(productToEdit);

            if (selectedProduct.RetrieveProductId() != 0)
            {
                Product newProduct = new Product(selectedProduct.RetrieveProductId(), p.ProductName);
                newProduct.ProductAmount     = p.ProductAmount;
                newProduct.ProductDesc       = p.ProductDesc;
                newProduct.ProductCategories = new List <Category>();
                newProduct.ProductDiscount   = p.ProductDiscount;
                newProduct.ImageUrl          = uploadedImage;
                newProduct.BtwPercentage     = p.BtwPercentage;

                //Add all selected categories in system to model via viewmodels
                foreach (CategoryOverviewViewModel c in p.ProductCategories)
                {
                    Category newcategory = categoryRep.RetrieveCategoryById(c.CategoryId);
                    newProduct.ProductCategories.Add(newcategory);
                }
                if (p.ProductInStock == "Yes")
                {
                    newProduct.ProductInStock = 1;
                }
                else
                {
                    newProduct.ProductInStock = 0;
                }
                if (newProduct.ProductDesc == null)
                {
                    newProduct.ProductDesc = "No description";
                }
                if (newProduct.ImageUrl == null)
                {
                    newProduct.ImageUrl = "NoImage.jpg";
                }

                newProduct.ProductPrice  = p.ProductPrice;
                newProduct.ProductStatus = p.ProductStatus;
                productRep.UpdateProductById(newProduct);
                soc.Update(p, newProduct);
                InsertingCategoriesIntoProduct(selectedCategory, newProduct);
                revisionRep.CreateNewRevision(new Revision(-1, newProduct));
                return(RedirectToAction("Overview", "Product", selectedProduct));
            }
            return(RedirectToAction("Single", "Product"));
        }