public IHttpActionResult AddPurchaseOrder(JObject jsonBody)
        {
            JObject materials = (JObject)jsonBody["MaterialsInPurchaseOrder"]; // this variable must be present in the javascript

            jsonBody.Remove("MaterialsInPurchaseOrder");

            PurchaseOrder purchaseOrder = jsonBody.ToObject <PurchaseOrder>(); // the job card object

            db.PurchaseOrder.Add(purchaseOrder);

            db.SaveChanges();                                    // save the shit

            int purchaseOrderId = purchaseOrder.PurchaseOrderId; // the foregin key to be used for the -> proudcts

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

            foreach (JToken token in tokens)
            {
                JToken materialsJson = token.Children().First();
                MaterialInPurchaseOrder materialInstance = materialsJson.ToObject <MaterialInPurchaseOrder>();
                materialInstance.PurchaseOrderId = purchaseOrderId;
                db.MaterialInPurchaseOrders.Add(materialInstance);
            }

            db.SaveChanges();
            return(StatusCode(HttpStatusCode.Created));
        }
Exemplo n.º 2
0
        public IHttpActionResult PutMaterialInPurchaseOrder(int id, MaterialInPurchaseOrder materialInPurchaseOrder)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != materialInPurchaseOrder.MaterialInPurchaseOrderId)
            {
                return(BadRequest());
            }

            db.Entry(materialInPurchaseOrder).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MaterialInPurchaseOrderExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 3
0
        public IHttpActionResult GetMaterialInPurchaseOrder(int id)
        {
            MaterialInPurchaseOrder materialInPurchaseOrder = db.MaterialInPurchaseOrders.Find(id);

            if (materialInPurchaseOrder == null)
            {
                return(NotFound());
            }

            return(Ok(materialInPurchaseOrder));
        }
Exemplo n.º 4
0
        public IHttpActionResult PostMaterialInPurchaseOrder(MaterialInPurchaseOrder materialInPurchaseOrder)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.MaterialInPurchaseOrders.Add(materialInPurchaseOrder);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = materialInPurchaseOrder.MaterialInPurchaseOrderId }, materialInPurchaseOrder));
        }
Exemplo n.º 5
0
        public IHttpActionResult DeleteMaterialInPurchaseOrder(int id)
        {
            MaterialInPurchaseOrder materialInPurchaseOrder = db.MaterialInPurchaseOrders.Find(id);

            if (materialInPurchaseOrder == null)
            {
                return(NotFound());
            }

            db.MaterialInPurchaseOrders.Remove(materialInPurchaseOrder);
            db.SaveChanges();

            return(Ok(materialInPurchaseOrder));
        }