Example #1
0
        public ActionResult ProductCreate()
        {
            //ProductCreateVM pcVM = new ProductCreateVM();
            //pcVM.ProductVariant = new ProductVariant();
            //pcVM.ProductVariant.Product = new Product();

            // TODO: For future
            // pcVM.Products = Dictionary<int, string>  ProductID and ProductName
            // pcVM.Buttons = same as above - already done in edit product

            // Need dictionary to retrieve ButtonID as string since IQueryable cannot !
            Dictionary<int, string> newButtons = buttonRepo.Buttons.ToDictionary(b => b.ButtonID, b => b.Name);
            Dictionary<int, string> newCategories = categoryRepo.Categories.ToDictionary(c => c.CategoryID, c => c.Name);

            ProductCreateVM pcVM = new ProductCreateVM
            {
                ProductVariant = new ProductVariant
                {
                    Product = new Product
                    {   ProductID = 0 ,
                        // Categories = new List<ProductCategory>()
                    }

                },

                //Product = new Product {
                //               Variants = new List<ProductVariant>(),
                //               Categories = new List<ProductCategory>()
                //          } ,

                Buttons = newButtons.Select(b => new SelectListItem
                {
                    Value = b.Key.ToString(),
                    Text = b.Value,
                    Selected = false
                }).ToList(),

                Categories = newCategories.Select(c => new SelectListItem
                {
                    Value = c.Key.ToString(),
                    Text = c.Value,
                    Selected = false
                }).ToList() ,

                NewSKU = productRepo.ProductVariants
                                    .OrderByDescending(pv => pv.VariantID)
                                    .First()
                                    .SKU
            };

            return View(pcVM);
        }
Example #2
0
        public ActionResult ProductCreate(ProductCreateVM pcVM, HttpPostedFileBase file )
        {
            if (ModelState.IsValid)
            {
                // product image file upload
                //if (file.ContentLength > 0)
                //{
                //    string fileName = "Product-" + peVM.ProductVariant.Image.ImageID.ToString() + "-mid";
                //    string path = Path.Combine(Server.MapPath("~/Contents/Site-Image"), fileName);
                //    file.SaveAs(path);
                //}

                // if ProductType (DropDownList) = Product , exist choose so and create ProductVariant only ! , else if not create new Product & ProductVariant

                // TODO : many to many relationship for dbcontext - use Fluent API ?
                //Product newProduct = productRepo.Products
                //                                .Include("Variants")
                //                                .Include("Categories")
                //                                .FirstOrDefault(p => p.ProductID == 1);

                // newProduct.Variants.Add(pcVM.ProductVariant);

                // Create new dummy product for now , ProductID  = auto generated by DB
                pcVM.ProductVariant.Product.ProductGUID = "N/A";
                pcVM.ProductVariant.Product.Name = "N/A";
                pcVM.ProductVariant.Product.Categories = new List<ProductCategory>();

                // Save category info to product
                // if category was chosen from DDL
                if (!String.IsNullOrEmpty(pcVM.SelectedCategoryID))
                {
                    pcVM.ProductVariant.Product.Categories = new List<ProductCategory>();

                    int selectedCatID = Int32.Parse(pcVM.SelectedCategoryID);

                    ProductCategory category = categoryRepo.Categories
                                                    .Where(c => c.CategoryID == selectedCatID)
                                                    .FirstOrDefault();

                    ProductCategory newCategory = new ProductCategory
                    {
                        CategoryID = category.CategoryID ,
                        CategoryGUID = category.CategoryGUID ?? "",
                        Description = category.Description ?? "" ,
                        Name = category.Name ?? "" ,
                        Products = category.Products
                    };

                    //pcVM.ProductVariant.Product.Categories.Add(category);

                    categoryRepo.SaveCategory(category);
                }

                // implement create new category later
                //else
                //{
                //    // New category added from "Create Category Field" - for now just create new instand of obj with ID = 0
                //    ProductCategory category = new ProductCategory { CategoryID = 0 };
                //    pcVM.ProductVariant.Product.Categories.Add(category);
                //}

                // productRepo.SaveProduct(newProduct);

                // Try to make below into one single method and DB access

                // Create new SKU
                productRepo.SaveProductVariant(pcVM.ProductVariant);

                // Save Button
                if (!String.IsNullOrEmpty(pcVM.SelectedButtonID))
                {
                    int selectedButtonID = Int32.Parse(pcVM.SelectedButtonID);  // == null ? 0 : Int32.Parse(peVM.SelectedButtonID);

                    Button modifiedButton = buttonRepo.Buttons
                                                      .Where(b => b.ButtonID == selectedButtonID)
                                                      .FirstOrDefault();

                    modifiedButton.VariantID = pcVM.ProductVariant.VariantID;
                    //modifiedButton.Variant = pcVM.ProductVariant;

                    buttonRepo.SaveButton(modifiedButton);
                }

                return View("TaskCompleted");
            }

            return View(pcVM);
        }