public ActionResult Delete(int id, Enums.ManageType manageType)
        {
            using (var entities = new EntityModel.DatabaseContext())
            {
                switch (manageType)
                {
                case Enums.ManageType.Product:
                    entities.Products.SingleOrDefault(x => x.IsValid && x.Id == id).IsValid = false;
                    // Changelog
                    break;

                case Enums.ManageType.Category:
                    foreach (var product in entities.Products)
                    {
                        if (product.ProductCategoryId == id)
                        {
                            product.IsValid = false;
                        }
                    }
                    entities.ProductCategorys.SingleOrDefault(x => x.IsValid && x.Id == id).IsValid = false;
                    break;

                default:
                    break;
                }
                entities.SaveChanges();
                return(RedirectToAction("Index", new { HasBeenDeleted = true, ManageType = manageType }));
            }
        }
        public ActionResult Create(ViewModels.ProductViewModels.CreateProductViewModel viewmodel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewmodel));
            }

            using (var entities = new EntityModel.DatabaseContext())
            {
                viewmodel.ProductCategoryId         = entities.ProductCategorys.SingleOrDefault(x => x.Name == viewmodel.ProductCategoryName).Id;
                viewmodel.AvaiableProductCategories = Management.ActionHandler.SetupAvaiableCategoryTypes(entities.ProductCategorys.ToList());

                entities.Products.Add(Management.ActionHandler.Create(new EntityModel.Product(), viewmodel));
                entities.SaveChanges();

                var lastId = entities.Products.Where(c => c.IsValid).Count();
                return(RedirectToAction("ShowProduct", new { productId = lastId }));
            }
        }
Exemple #3
0
        public ActionResult Create(ViewModels.CategoryViewModels.CreateCategoryViewModel viewmodel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewmodel));
            }

            using (var entities = new EntityModel.DatabaseContext())
            {
                entities.ProductCategorys.Add(new EntityModel.ProductCategory
                {
                    Name    = viewmodel.Name,
                    IsValid = true,
                });
                entities.SaveChanges();
            }

            return(RedirectToAction("Index", "Home"));
        }
        public ActionResult Edit(ViewModels.ProductViewModels.EditProductViewModel viewmodel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewmodel));
            }
            using (var entities = new EntityModel.DatabaseContext())
            {
                viewmodel.AvaiableProductCategories = Management.ActionHandler.SetupAvaiableCategoryTypes(entities.ProductCategorys.ToList());

                var model = entities.Products.SingleOrDefault(x => x.Id == viewmodel.Id);
                model.Name              = viewmodel.Name;
                model.Description       = viewmodel.Description;
                model.Price             = viewmodel.Price;
                model.ProductCategoryId = entities.ProductCategorys.SingleOrDefault(x => x.Name == viewmodel.ProductCategoryName).Id;

                entities.SaveChanges();
                return(RedirectToAction("ShowProduct", new { productId = model.Id }));
            }
        }