Esempio n. 1
0
        public ActionResult Create(Product_Option model)
        {
            var product           = db.Products.Find(model.ProductId);
            var messageCollection = new List <Message>();

            if (ModelState.IsValid)
            {
                if (model.variantTypes != null)
                {
                    model.Product_Variants = new List <Product_Variant>();
                    foreach (var variantType in model.variantTypes)
                    {
                        foreach (var variantId in variantType.Value)
                        {
                            var variant = db.Product_Variants.Find(variantId);
                            model.Product_Variants.Add(variant);
                        }
                    }
                }

                var userId         = User.Identity.GetUserId();
                var currentTimeUtc = DateTime.UtcNow;
                foreach (var detail in model.Product_Options_Details)
                {
                    detail.CreateUserId    = userId;
                    detail.CreateTime      = currentTimeUtc;
                    db.Entry(detail).State = System.Data.Entity.EntityState.Added;
                }
                model.CreateUserId = userId;
                model.CreateTime   = currentTimeUtc;

                db.Entry(model).State = System.Data.Entity.EntityState.Added;
                db.SaveChanges();
                messageCollection.Add(new Message {
                    MessageType = MessageTypes.Success, MessageContent = "Create successfully!"
                });
                TempData[ConstantKeys.ACTION_RESULT_MESSAGES] = messageCollection;

                return(RedirectToAction("Edit", new { id = model.Id }));
            }

            ViewData["ProductName"] = product.Name;
            ViewData[ConstantKeys.PRODUCT_VARIANT_TYPES] = GetVariantTypes();
            ViewData[ConstantKeys.CUSTOM_FIELDS]         = GetCustomFields();

            if (model.PreviewImageId != null)
            {
                model.PreviewImage = db.Files.Find(model.PreviewImageId);
            }

            foreach (var item in model.Product_Options_Details)
            {
                if (item.Type == ((int)FieldTypes.File).ToString())
                {
                    item.File = db.Files.Find(item.FileId);
                }
            }
            return(View(model));
        }
        public ActionResult Add()
        {
            Product_Option model = new Product_Option();

            model.Status = true;

            return(View(model));
        }
Esempio n. 3
0
        public ActionResult CreateOption(ProductOptionViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var model = new Product_Option
                {
                    Name           = viewModel.name,
                    Description    = viewModel.description,
                    ProductId      = viewModel.productId,
                    PreviewImageId = viewModel.previewImageId,
                    Status         = viewModel.status,
                    Files          = new List <File>(),
                    CreateUserId   = User.Identity.GetUserId(),
                    CreateTime     = DateTime.Now
                };

                if (viewModel.variants != null)
                {
                    foreach (var variant in viewModel.variants)
                    {
                        foreach (var variantId in variant.Value)
                        {
                            var variantModel = db.Product_Variants.Find(variantId);
                            if (variantModel != null)
                            {
                                variantModel.Product_Options.Add(model);
                            }
                        }
                    }
                }

                if (viewModel.imageFiles != null)
                {
                    foreach (var file in viewModel.imageFiles)
                    {
                        var fileModel = db.Files.Find(file);
                        if (fileModel != null)
                        {
                            model.Files.Add(fileModel);
                        }
                    }
                }

                if (viewModel.details != null)
                {
                    foreach (var pair in viewModel.details)
                    {
                        if (pair.Key != string.Empty && pair.Value != string.Empty)
                        {
                            var detailModel = new Product_Options_Details
                            {
                                Name  = pair.Key,
                                Value = pair.Value,
                            };
                            db.Product_Options_Details.Add(detailModel);
                            model.Product_Options_Details.Add(detailModel);
                        }
                    }
                }

                db.Product_Options.Add(model);
                db.SaveChanges();
                return(RedirectToAction("CreateOption", new { productId = model.ProductId, success = true, successObjectName = model.Name }));
            }

            var variantCategories = db.Categories.Where(o => o.CategoryTypeId == (int)CategoryTypes.ProductVariant);

            ViewBag.VariantCategories = variantCategories;

            var product = db.Products.Find(viewModel.productId);
            var options = product.Product_Options;

            ViewBag.Options = options;

            return(View(viewModel));
        }
Esempio n. 4
0
        public ActionResult Edit(Product_Option model)
        {
            var modelTarget       = db.Product_Options.Find(model.Id);
            var messageCollection = new List <Message>();

            if (ModelState.IsValid)
            {
                modelTarget.Product_Variants.Clear();
                if (model.variantTypes != null)
                {
                    foreach (var variantType in model.variantTypes)
                    {
                        foreach (var variantId in variantType.Value)
                        {
                            var variant = db.Product_Variants.Find(variantId);
                            modelTarget.Product_Variants.Add(variant);
                        }
                    }
                }

                var userId         = User.Identity.GetUserId();
                var currentTimeUtc = DateTime.UtcNow;

                //Remove absent details
                var existDetailId   = modelTarget.Product_Options_Details.Select(o => o.Id);
                var exceptDetailIds = existDetailId.Except(model.Product_Options_Details.Select(o => o.Id)).ToList();
                foreach (var exceptDetailId in exceptDetailIds)
                {
                    var details = db.Product_Options_Details.Find(exceptDetailId);
                    db.Entry(details).State = System.Data.Entity.EntityState.Deleted;
                }

                //Modify or add details
                foreach (var detail in model.Product_Options_Details)
                {
                    if (detail.Id != 0)
                    {
                        var detailTarget = modelTarget.Product_Options_Details.FirstOrDefault(o => o.Id == detail.Id);
                        db.Entry(detailTarget).CurrentValues.SetValues(detail);
                        db.Entry(detailTarget).Property("ProductOptionId").IsModified = false;
                        db.Entry(detailTarget).Property("CreateUserId").IsModified    = false;
                        db.Entry(detailTarget).Property("CreateTime").IsModified      = false;
                    }
                    else
                    {
                        detail.CreateUserId    = userId;
                        detail.CreateTime      = currentTimeUtc;
                        db.Entry(detail).State = System.Data.Entity.EntityState.Added;
                    }
                }

                db.Entry(modelTarget).CurrentValues.SetValues(model);
                db.Entry(modelTarget).Property("CreateUserId").IsModified = false;
                db.Entry(modelTarget).Property("CreateTime").IsModified   = false;
                db.SaveChanges();

                messageCollection.Add(new Message {
                    MessageType = MessageTypes.Success, MessageContent = "Update successfully!"
                });
                TempData[ConstantKeys.ACTION_RESULT_MESSAGES] = messageCollection;
                return(RedirectToAction("Edit", new { id = model.Id }));
            }

            ViewData[ConstantKeys.PRODUCT_VARIANT_TYPES] = GetVariantTypes();
            ViewData[ConstantKeys.CUSTOM_FIELDS]         = GetCustomFields();

            return(View(model));
        }
        public ActionResult Update(Product_Option model, IEnumerable <HttpPostedFileBase> FileUp, string Status)
        {
            if (string.IsNullOrEmpty(model.Name))
            {
                ViewBag.Error = "Please enter field \"Name\"";

                return(View("Add", model));
            }

            if (string.IsNullOrEmpty(model.InternalName))
            {
                model.InternalName = model.Name;
            }

            Product_Option current_item = new Product_Option();

            if (model.Id > 0)
            {
                var z = Db.Where <Product_Option>(m => m.Id == model.Id);
                if (z.Count == 0)
                {
                    // the ID is not exist
                    return(JsonError("Please dont try to hack us"));
                }
                else
                {
                    current_item = z.First();
                }
            }
            if (FileUp != null && FileUp.Count() > 0 && FileUp.First() != null)
            {
                model.Thumbnail = UploadFile(model.Id, model.Thumbnail, "", FileUp);
            }
            else
            {
                model.Thumbnail = current_item.Thumbnail;
            }
            if (model.Id == 0)
            {
                model.CreatedOn = DateTime.Now;
                model.CreatedBy = AuthenticatedUserID;
            }
            else
            {
                model.CreatedOn = current_item.CreatedOn;
                model.CreatedBy = current_item.CreatedBy;
                model.Code      = current_item.Code;
            }

            if (model.Id == 0)
            {
                Db.Insert <Product_Option>(model);
                // generate OptionCode
                model.Id   = Db.GetLastInsertId();
                model.Code = "OPT" + model.Id.ToString("00000");
                Db.Update <Product_Option>(model);
                return(RedirectToAction("Index"));
            }
            else
            {
                if (string.IsNullOrEmpty(model.Code))
                {
                    model.Code = "OPT" + model.Id.ToString("00000");
                }
                Db.Update <Product_Option>(model);
                return(RedirectToAction("Index"));
            }
        }