Beispiel #1
0
        public HttpResponseMessage AddOrder(JObject jsonBody)
        {
            JObject products = (JObject)jsonBody["ProductsRetailOrder"]; // this variable must be present in the javascript

            jsonBody.Remove("ProductsRetailOrder");

            OnlineOrder retailOrder = jsonBody.ToObject <OnlineOrder>(); // the job card object

            //retailOrder.CustomerId = User.Identity.GetUserId();

            db.OnlineOrders.Add(retailOrder);

            db.SaveChanges();

            int retailOrderId = retailOrder.OnlineOrderId; // the foregin key to be used for the -> proudcts

            JEnumerable <JToken> tokens = (JEnumerable <JToken>)products.Children <JToken>();

            foreach (JToken token in tokens)
            {
                JToken productJson = token.Children().First();
                ProductInOnlineOrder productInstance = productJson.ToObject <ProductInOnlineOrder>();
                productInstance.OnlineOrderId = retailOrderId;
                db.ProductsInOnlineOrders.Add(productInstance);
            }

            db.SaveChanges();
            return(this.Request.CreateResponse(HttpStatusCode.Created, retailOrderId));
        }
Beispiel #2
0
        public HttpResponseMessage AddOrder(JObject jsonBody)
        {
            JObject products = (JObject)jsonBody["ProductsInRetailOrder"]; // contains product in the order
            JObject account  = (JObject)jsonBody["Account"];               // contains information about  the account who is placin the order

            jsonBody.Remove("ProductsRetailOrder");                        // removing these information, will add the later back to send to IS
            jsonBody.Remove("Account");                                    // same as above


            OnlineOrder order = jsonBody.ToObject <OnlineOrder>(); // the online order

            HttpWebResponse response = null;                       // to store the response from the IS

            // startin the distributed transaction, will be commited only the distributed system transaction is success
            using (var dbTransaction = db.Database.BeginTransaction())
            {
                try
                {
                    // write order
                    db.OnlineOrders.Add(order);

                    db.SaveChanges();

                    // get order id to FK
                    int OrderId = order.OnlineOrderId;     // the foregin key to be used for the -> product in..

                    JEnumerable <JToken> tokens = (JEnumerable <JToken>)products.Children <JToken>();

                    foreach (JToken token in tokens)
                    {
                        JToken productJson = token.Children().First();     // I think this can be changed if changed earlier part of code
                        ProductInOnlineOrder productInstance = productJson.ToObject <ProductInOnlineOrder>();
                        productInstance.OnlineOrderId = OrderId;
                        db.ProductsInOnlineOrders.Add(productInstance);

                        // update the stock
                        Product dbProduct   = db.Products.Find(productInstance.ProductId);
                        float   newQuantity = (float)dbProduct.StocksQuantity;
                        newQuantity = newQuantity - productInstance.Quantity;
                        if (newQuantity < 0)
                        {
                            dbTransaction.Rollback();
                            return(this.Request.CreateResponse(HttpStatusCode.Conflict, "Stock not sufficient."));
                        }
                        else
                        {
                            dbProduct.StocksQuantity  = newQuantity;
                            db.Entry(dbProduct).State = EntityState.Modified;
                        }
                    }

                    Integrator integrator = new Integrator();

                    // add routing information to the data and send it to the IS
                    Setting setting = db.Settings.Find(1);
                    jsonBody.Add("ServiceId", setting.SystemIdNumber);
                    //jsonBody.Add("EnterpriseId", jsonBody["EnterpriseId"].ToString());
                    //jsonBody.Add("AccountId", jsonBody["AccountId"].ToString());

                    // add payload information as promised earlier
                    jsonBody.Add("Account", account);
                    jsonBody.Add("ServiceOrderId", OrderId);
                    //jsonBody.Add("ProductsInRetailOrder", account);

                    response = integrator.sendJsonObject(jsonBody, "/api/Enterprises/AddRetailOrder");

                    db.SaveChanges();
                    // commit if distributed transaction is success
                    if (response != null && response.StatusCode != HttpStatusCode.Conflict)
                    {
                        dbTransaction.Commit();
                    }
                    else
                    {
                        dbTransaction.Rollback();
                    }
                }
                catch (Exception ex)
                {
                    dbTransaction.Rollback();
                    System.Diagnostics.Trace.WriteLine(ex);
                    return(this.Request.CreateResponse(HttpStatusCode.Conflict, response));
                }
            }

            return(this.Request.CreateResponse(HttpStatusCode.Created, response));
        }