コード例 #1
0
        /// <summary>
        /// Handles a GET by either getting a resource by ID or querying against all resources
        /// </summary>
        /// <param name="request"></param>
        /// <param name="resourceHandler"></param>
        /// <param name="resourceDescriptor"></param>
        /// <returns></returns>
        private async Task HandleGet(IRequest request, IResourceHandler resourceHandler, ResourceDescriptor resourceDescriptor)
        {
            if (resourceDescriptor.Id != null)
            {
                // get the single resource
                var resource = await resourceHandler.Get(resourceDescriptor);

                // if found, render the resource and return in the body
                // otherwise, indicate not found
                if (resource != null)
                {
                    request.Response.WithStatus(HttpStatusCode.OK).WithJsonBody(ResourceSerializer.Serialize(resource));
                }
                else
                {
                    request.Response.WithStatus(HttpStatusCode.NotFound);
                }
            }
            else
            {
                Logger.Debug("Executing query for resources of type {0}...", resourceDescriptor.Type);

                // no id, so this is a query
                var resourceCollection = await resourceHandler.Query(resourceDescriptor);

                Logger.Debug("Completed query for resources of type {0}.", resourceDescriptor.Type);

                // create JSON array from results and return as body
                request.Response.WithStatus(HttpStatusCode.OK)
                .WithJsonBody(ResourceSerializer.Serialize(resourceCollection));
            }
        }
コード例 #2
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));
        }
コード例 #3
0
        /// <summary>
        /// Handles DELETE by removing a resource
        /// </summary>
        /// <param name="request"></param>
        /// <param name="resourceHandler"></param>
        /// <param name="resourceDescriptor"></param>
        /// <returns></returns>
        private async Task HandleDelete(IRequest request, IResourceHandler resourceHandler, ResourceDescriptor resourceDescriptor)
        {
            try
            {
                // get the resource to ensure it exists
                var existing = await resourceHandler.Get(resourceDescriptor);

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

                // delete the resource using the handler
                await resourceHandler.Delete(resourceDescriptor);

                // return OK to indicate the resource was successfully deleted
                request.Response.WithStatus(HttpStatusCode.OK);
            }
            catch (Exception e)
            {
                request.Response.WithStatus(HttpStatusCode.InternalServerError).WithPlainTextBody(e.ToString());
            }
        }
コード例 #4
0
 public string _T(string key)
 {
     return(_rh.Get(key));
 }