public IActionResult Clone(string id) { PType typeToClone = _unityOfWork.Type.GetById(Convert.ToInt32(id)); var typeList = _unityOfWork.Type.GetAll(); var attributeList = _unityOfWork.Attribute.GetAll(); var featureList = _unityOfWork.Feature.GetAll(); ViewBag.ListOfAttribute = attributeList; ViewBag.ListOfType = typeList; ViewBag.ListOfFeature = featureList; TypeCreateViewModel newType = new TypeCreateViewModel() { Description = typeToClone.Description, Name = typeToClone.Name }; List <long> featuresID = new List <long>(); List <string> attributesID = new List <string>(); List <long> childsID = new List <long>(); foreach (var feature in typeToClone.TypeFeatures) { featuresID.Add(feature.FeatureID); } newType.SelectedFeatures = featuresID.ToArray(); foreach (var attribute in typeToClone.TypeAttributes) { attributesID.Add(attribute.AttributeID.ToString()); } newType.SelectedAttributes = attributesID.ToArray(); foreach (var child in typeToClone.Childs) { childsID.Add(child.ChildID); } ViewBag.selectedChilds = Newtonsoft.Json.JsonConvert.SerializeObject(newType.TypeChild, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); ViewBag.selectedAttributes = Newtonsoft.Json.JsonConvert.SerializeObject(typeToClone.TypeAttributes, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); ViewBag.selectedFeatures = Newtonsoft.Json.JsonConvert.SerializeObject(newType.SelectedFeatures, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); ViewBag.selectedAttributesArray = Newtonsoft.Json.JsonConvert.SerializeObject(newType.SelectedAttributes, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); return(View(newType)); }
public ActionResult Create(TypeCreateViewModel model) { if (!Security.IsAuthenticate) { return RedirectToAction("index","type"); } if (!ModelState.IsValid) { return View("create",model); } Types.Create(model); return RedirectToAction("index", "type"); }
public IActionResult Create(TypeCreateViewModel type) { int arrIndex = 0; PType myType = new PType() { Name = type.Name, Description = type.Description, }; if (type.SelectedFeatures != null) { foreach (long feature in type.SelectedFeatures) { myType.TypeFeatures.Add(new TypeFeature() { FeatureID = feature }); } } if (type.SelectedAttributes != null) { foreach (string item in type.SelectedAttributes) { Attribute attr = _unityOfWork.Attribute.GetByName(item); myType.TypeAttributes.Add(new TypeAttribute() { AttributeID = attr.ID, Value = type.ValueSelectedAttributes[arrIndex] } );; arrIndex++; } } List <long> existingChilds = new List <long>(); if (type.TypeChild != null) { foreach (long child in type.TypeChild) { if (!existingChilds.Contains(child)) { myType.Childs.Add(new TypeChild() { ChildID = child, Quantity = type.TypeChild.Where(p => p == child).Count() });; existingChilds.Add(child); } } } _unityOfWork.Type.Insert(myType); _unityOfWork.Save(); LoggingController.writeLog(myType, User.Identity.Name, this.ControllerContext.RouteData.Values["action"].ToString(), this.ControllerContext.RouteData.Values["controller"].ToString()); return(RedirectToAction("Index")); }
public IActionResult Edit(TypeCreateViewModel type) { int arrIndex = 0; PType myType = _unityOfWork.Type.GetById(type.ID); myType.ID = type.ID; myType.Name = type.Name; myType.Description = type.Description; if (type.SelectedFeatures != null) { myType.TypeFeatures.Clear(); foreach (long feature in type.SelectedFeatures) { myType.TypeFeatures.Add(new TypeFeature() { FeatureID = feature }); } } if (type.SelectedAttributes != null) { myType.TypeAttributes = new List <TypeAttribute>(); foreach (string item in type.SelectedAttributes) { myType.TypeAttributes.Add(new TypeAttribute() { DeviceTypeID = type.ID, AttributeID = _unityOfWork.Attribute.GetByName(item).ID, Value = type.ValueSelectedAttributes[arrIndex] } );; arrIndex++; } } List <long> existingChilds = new List <long>(); if (type.TypeChild != null) { myType.Childs.Clear(); foreach (var childID in type.TypeChild) { int quantity = type.TypeChild.Where(b => b == childID).Count(); TypeChild model = new TypeChild() { TypeID = myType.ID, ChildID = childID, Quantity = quantity }; type.TypeChild = type.TypeChild.Where(b => b != childID).ToArray(); myType.Childs.Add(model); } } _unityOfWork.Type.Update(myType); _unityOfWork.Save(); // This part Update also Product Attribute of Existing Products var products = _unityOfWork.Product.getProductsByType(myType.ID); foreach (var product in products) { List <long> existingAttributes = new List <long>(); foreach (var p in product.ProductAttributes) { existingAttributes.Add(p.AttributeID); } foreach (var name in type.SelectedAttributes) { if (!existingAttributes.Contains(_unityOfWork.Attribute.GetByName(name).ID)) { ProductAttribute pa = new ProductAttribute { ProductID = product.ID, AttributeID = _unityOfWork.Attribute.GetByName(name).ID, }; product.ProductAttributes.Add(pa); } } _unityOfWork.Product.Update(product); existingAttributes.Clear(); } _unityOfWork.Save(); LoggingController.writeLog(myType, User.Identity.Name, this.ControllerContext.RouteData.Values["action"].ToString(), this.ControllerContext.RouteData.Values["controller"].ToString()); return(RedirectToAction("Index")); }