Ejemplo n.º 1
0
        // POST api/camera
        public HttpResponseMessage Post(Camera newCamera)
        {
            if(ModelState.IsValid)
            {
                try
                {
                    InMemoryCameraRepo.SingletonInstance.AddCamera(newCamera);

                    var response = Request.CreateResponse(HttpStatusCode.Created, newCamera);

                    //Add location header entry to newly created resource, so comply with ReST constraints
                    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "Camera", id = newCamera.Id }));

                    return response;
                }
                catch (Exception ex)
                {
                    ThrowHttpResponseException(HttpStatusCode.BadRequest, ex); //400
                }
            }

            //Modelstate is invalid
            var modelValidationErrorResponse = Request.CreateResponse(HttpStatusCode.BadRequest); //400
            modelValidationErrorResponse.Content = new StringContent(ModelState.GetModelErrors());
            return modelValidationErrorResponse;
        }
Ejemplo n.º 2
0
        // PUT api/camera/5
        public HttpResponseMessage Put(Camera camera)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    InMemoryCameraRepo.SingletonInstance.UpdateCamera(camera);
                    var response = Request.CreateResponse(HttpStatusCode.OK, camera); //Return 200
                    return response;
                }
                catch (Exception ex)
                {
                    ThrowHttpResponseException(HttpStatusCode.NotFound, ex);
                }
            }

            //Modelstate is invalid
            var modelValidationErrorResponse = Request.CreateResponse(HttpStatusCode.BadRequest); //400
            modelValidationErrorResponse.Content = new StringContent(ModelState.GetModelErrors());
            return modelValidationErrorResponse;
        }