Пример #1
0
        /// <summary>
        /// Handles a PUT by updating a resource
        /// </summary>
        /// <param name="request"></param>
        /// <param name="resourceHandler"></param>
        /// <param name="resourceDescriptor"></param>
        /// <param name="resource"></param>
        /// <returns></returns>
        private async Task HandlePut(IRequest request, IResourceHandler resourceHandler, ResourceDescriptor resourceDescriptor, Resource resource)
        {
            // get the object first to ensure it exists
            var existing = await resourceHandler.Get(resourceDescriptor);

            if (existing == null)
            {
                request.Response.WithStatus(HttpStatusCode.NotFound);
                return;
            }

            // if a different ID was provided on the resource, or it wasn't set at all, set it now
            if (resource.Id != existing.Id)
            {
                resource.Id = existing.Id;
            }

            // update resource using handler
            var result = await resourceHandler.Update(resourceDescriptor, resource);

            // return the updated object rendered as JSON
            request.Response.WithStatus(HttpStatusCode.OK).WithJsonBody(ResourceSerializer.Serialize(result));
        }
Пример #2
0
        public Stream Update(string type, string id, string format, Stream content)
        {
            IResourceHandler handler = _registry.GetHandler(type);

            if (handler == null)
            {
                return(ErrorMessage(HttpStatusCode.NotImplemented, String.Format("Create resource version of type {0} not implemented.", type)));
            }

            try
            {
                var    reader = new StreamReader(content);
                string data   = reader.ReadToEnd();

                string contentType = GetContentType(WebOperationContext.Current.IncomingRequest.ContentType, format);

                WebOperationContext.Current.OutgoingResponse.StatusCode = handler.Update(id, contentType, data);
                return(new MemoryStream(Encoding.UTF8.GetBytes(String.Empty)));
            }
            catch (Exception e)
            {
                return(ErrorMessage(HttpStatusCode.InternalServerError, e.Message));
            }
        }