Пример #1
0
        public ActionResult Change([FromBody] PurchaseRequest purchaseRequest)
        {
            if (purchaseRequest == null)
            {
                return(Json(new Msg {
                    Result = "Failure", Message = "Purchase request  is null"
                }, JsonRequestBehavior.AllowGet));
            }
            User user = db.Users.Find(purchaseRequest.UserId);

            if (user == null)
            {
                return(Json(new Msg {
                    Result = "Failure", Message = "User Id FK is invalid"
                }, JsonRequestBehavior.AllowGet));
            }

            PurchaseRequest tempPurchaseRequest = db.PurchaseRequests.Find(purchaseRequest.Id);

            if (tempPurchaseRequest == null)
            {
                return(Json(new Msg {
                    Result = "Failure", Message = "Purchase request Id not found"
                }, JsonRequestBehavior.AllowGet));
            }
            tempPurchaseRequest.Clone(purchaseRequest);
            db.SaveChanges();
            return(Json(new Msg {
                Result = "Success", Message = "Change Successful."
            }, JsonRequestBehavior.AllowGet));
        }
Пример #2
0
        public ActionResult Change([FromBody] PurchaseRequest purchaseRequest)
        {
            if (purchaseRequest == null)
            {
                return(Json(new Msg {
                    Result = "Failure", Message = "Purchase request  is null"
                }, JsonRequestBehavior.AllowGet));
            }
            //**Foreign key issue:
            User user = db.Users.Find(purchaseRequest.UserId); //returns a user for the ID or null if not found

            if (user == null)                                  //this is true if the id is not found
            {
                return(Json(new Msg {
                    Result = "Failure", Message = "User Id FK is invalid"
                }, JsonRequestBehavior.AllowGet));
            }

            //if we get here, just update the user
            PurchaseRequest tempPurchaseRequest = db.PurchaseRequests.Find(purchaseRequest.ID);

            if (tempPurchaseRequest == null)
            {
                return(Json(new Msg {
                    Result = "Failure", Message = "Purchase request ID not found"
                }, JsonRequestBehavior.AllowGet));
            }
            tempPurchaseRequest.Clone(purchaseRequest);
            db.SaveChanges(); //you have to make sure all the changes did in fact occur
            return(Json(new Msg {
                Result = "Success", Message = "Change Successful."
            }, JsonRequestBehavior.AllowGet));
        }