Ejemplo n.º 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));
            }
        }