public HttpResponseMessage AddProduct(JObject jsonBody) { JObject specifications = (JObject)jsonBody["SpecificationsInProduct"]; // this variable must be present in the javascript jsonBody.Remove("SpecificationsInProduct"); Product product = jsonBody.ToObject <Product>(); // the job card object\ db.Products.Add(product); db.SaveChanges(); // save the shit int productId = product.ProductId; // the foregin key to be used for the -> proudcts JEnumerable <JToken> tokens = (JEnumerable <JToken>)specifications.Children <JToken>(); foreach (JToken token in tokens) { JToken specificationJson = token.Children().First(); SpecificationInProduct specificationInstance = specificationJson.ToObject <SpecificationInProduct>(); specificationInstance.ProductId = productId; db.SpecificationInProduct.Add(specificationInstance); } db.SaveChanges(); try { //var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\products", Server.MapPath(@"\"))); //Directory.CreateDirectory("@\\Images\\products\\"+productId); //Directory.CreateDirectory("@\\Images\\products\\1"); //Directory.Move("@\\Images\\products\\temporary", "@\\Images\\products\\1"); var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\products", HostingEnvironment.MapPath(@"\"))); string newPath = System.IO.Path.Combine(originalDirectory.ToString(), "" + productId + ""); string oldPath = System.IO.Path.Combine(originalDirectory.ToString(), "temporary"); //bool isExists = System.IO.Directory.Exists(newPath); //if (!isExists) //{ // Directory.CreateDirectory(newPath); //} Directory.Move(oldPath, newPath); }catch (DirectoryNotFoundException ex) { System.Diagnostics.Debug.Write(ex.Data); } return(this.Request.CreateResponse(HttpStatusCode.Created, product)); }
public IHttpActionResult UpdateProductSpecifications(SpecificationInProduct specification) { if (!SpecificationInProductExist(specification.SpecificationInProductId)) { return(StatusCode(HttpStatusCode.NotModified)); } // else SpecificationInProduct original = db.SpecificationInProduct.Find(specification.SpecificationInProductId); original.Specification = specification.Specification; original.Value = specification.Value; db.Entry(original).State = EntityState.Modified; db.SaveChanges(); return(StatusCode(HttpStatusCode.Created)); }
public IHttpActionResult AddProduct(JObject jsonBody) { JObject specifications = (JObject)jsonBody["SpecificationsInProduct"]; // this variable must be present in the javascript JObject materials = (JObject)jsonBody["MaterialsInProduct"]; // this variable must be present in the javascript jsonBody.Remove("SpecificationsInProduct"); jsonBody.Remove("MaterialsInProduct"); Product product= jsonBody.ToObject<Product>(); // the job card object db.Products.Add(product); db.SaveChanges(); // save the shit int productId = product.ProductId; // the foregin key to be used for the -> proudcts JEnumerable<JToken> tokens = (JEnumerable<JToken>)specifications.Children<JToken>(); foreach (JToken token in tokens) { JToken specificationJson = token.Children().First(); SpecificationInProduct specificationInstance = specificationJson.ToObject<SpecificationInProduct>(); specificationInstance.ProductId = productId; db.SpecificationInProduct.Add(specificationInstance); } JEnumerable<JToken> materialsTokens = (JEnumerable<JToken>)materials.Children<JToken>(); foreach (JToken token in materialsTokens) { JToken materialJson = token.Children().First(); MaterialInProduct materialInstance = materialJson.ToObject<MaterialInProduct>(); materialInstance.ProductId = productId; db.MaterialInProducts.Add(materialInstance); } db.SaveChanges(); return StatusCode(HttpStatusCode.Created); }