public IHttpActionResult RequestQuotation(JObject jsonBody) { // set quotations status to request // add Request for Quotation JObject products = (JObject)jsonBody["ProductsInQuotation"]; // this variable must be present in the javascript jsonBody.Remove("ProductsInQuotation"); Quotation quotation = jsonBody.ToObject <Quotation>(); // the job card object\ quotation.Status = "Request"; db.Quotations.Add(quotation); db.SaveChanges(); // save the shit int quotationId = quotation.QuotationId; // the foregin key to be used for the -> products JEnumerable <JToken> tokens = (JEnumerable <JToken>)products.Children <JToken>(); foreach (JToken token in tokens) { JToken productJson = token.Children().First(); ProductInQuotation productInstance = productJson.ToObject <ProductInQuotation>(); productInstance.QuotationId = quotationId; db.ProductsInQuotations.Add(productInstance); } db.SaveChanges(); return(StatusCode(HttpStatusCode.Created)); }
/// <summary> /// Requesting a Quotation. /// Save Quotation information in the local database. /// Send the object to the IS to route to the Enterprise /// TODO : Authorization /// </summary> /// <param name="jsonBody"></param> /// <returns></returns> public IHttpActionResult RequestQuotation(JObject jsonBody) { using (var dbTransaction = db.Database.BeginTransaction()){ try { // Deserializing the json and gettting Quotation object JObject products = (JObject)jsonBody["ProductsInQuotation"]; // this variable must be present in the javascript jsonBody.Remove("ProductsInQuotation"); Quotation quotation = jsonBody.ToObject <Quotation>(); quotation.Status = "Request"; db.Quotations.Add(quotation); db.SaveChanges(); int quotationId = quotation.QuotationId; // saving this in a seperate variable to make the code look simple //Deserializing the object and getting Produts in Quotation JEnumerable <JToken> tokens = (JEnumerable <JToken>)products.Children <JToken>(); foreach (JToken token in tokens) { JToken productJson = token.Children().First(); ProductInQuotation productInstance = productJson.ToObject <ProductInQuotation>(); productInstance.QuotationId = quotationId; db.ProductsInQuotations.Add(productInstance); } db.SaveChanges(); // lets send this to the IS Integrator integrator = new Integrator(); Setting setting = db.Settings.Find(1); //reconstructing the jsonBody to send to the IS jsonBody.Add("ProductsInQuotation", products); // adding routing information. Some of these might be usefull at the Enterprise system too. jsonBody.Add("ServiceId", setting.SystemIdNumber); jsonBody.Add("SellingEnterpriseId", jsonBody["SellingEnterpriseId"]); jsonBody.Add("BuyingEnterpriseId", jsonBody["BuyingEnterpriseId"]); HttpWebResponse response = integrator.sendJsonObject(jsonBody, "/api/Enterprises/RequestQuotation"); if (response != null && response.StatusCode != HttpStatusCode.Conflict) { dbTransaction.Commit(); return(StatusCode(response.StatusCode)); } else { dbTransaction.Rollback(); return(StatusCode(HttpStatusCode.Conflict)); } }catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); dbTransaction.Rollback(); return(StatusCode(HttpStatusCode.Conflict)); } } }