コード例 #1
0
        //PUT
        //description: update service request based on id
        //route: api/servicerequest/{id}
        //    Response
        //200: updated service request
        //400: bad service request
        //404: not found

        // PUT: api/CreateServiceRequest/5
        public HttpResponseMessage Put(string id, [FromBody] string value)
        {
            //          {
            //              "id": "0e581177-33dc-4de8-8743-4f91a0327d50",
            //              "CurrentStatus": "Complete",
            //              "lastModifiedBy": "maintenance man"
            //          }
            //create new service request getting data from the body of post (raw json formatted - example above)
            string req_txt;

            using (StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream))
            {
                req_txt = reader.ReadToEnd();
            }
            //get defined fields by putting it into a service request object
            ServiceRequest srr = JsonConvert.DeserializeObject <ServiceRequest>(req_txt);

            try
            {
                var data = serviceRequests.UpdateRequest(id, (int)srr.currentStatus, srr.lastModifiedBy);
                //data = data.Remove(0);  //for testing bad request
                if (data.Length > 0)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, data));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }