See More

using System.Threading; using System.Threading.Tasks; using System.Web.Http; using JSONAPI.Documents; namespace JSONAPI.Http { ///

/// This ApiController is capable of serving all requests using JSON API. /// public class JsonApiController : ApiController { private readonly IDocumentMaterializerLocator _documentMaterializerLocator; /// /// Creates a new JsonApiController /// /// The service locator to get document materializers for a given resource type. public JsonApiController(IDocumentMaterializerLocator documentMaterializerLocator) { _documentMaterializerLocator = documentMaterializerLocator; } /// /// Returns a document corresponding to a set of records of this type. /// public virtual async Task GetResourceCollection(string resourceType, CancellationToken cancellationToken) { var materializer = _documentMaterializerLocator.GetMaterializerByResourceTypeName(resourceType); var document = await materializer.GetRecords(Request, cancellationToken); return Ok(document); } /// /// Returns a document corresponding to the single record matching the ID. /// public virtual async Task Get(string resourceType, string id, CancellationToken cancellationToken) { var materializer = _documentMaterializerLocator.GetMaterializerByResourceTypeName(resourceType); var document = await materializer.GetRecordById(id, Request, cancellationToken); return Ok(document); } /// /// Returns a document corresponding to the resource(s) related to the resource identified by the ID, /// and the relationship name. /// public virtual async Task GetRelatedResource(string resourceType, string id, string relationshipName, CancellationToken cancellationToken) { var materializer = _documentMaterializerLocator.GetRelatedResourceMaterializer(resourceType, relationshipName); var document = await materializer.GetRelatedResourceDocument(id, Request, cancellationToken); return Ok(document); } /// /// Creates a new record corresponding to the data in the request document. /// public virtual async Task Post(string resourceType, [FromBody]ISingleResourceDocument requestDocument, CancellationToken cancellationToken) { var materializer = _documentMaterializerLocator.GetMaterializerByResourceTypeName(resourceType); var document = await materializer.CreateRecord(requestDocument, Request, cancellationToken); return Ok(document); } /// /// Updates the record with the given ID with data from the request payload. /// public virtual async Task Patch(string resourceType, string id, [FromBody]ISingleResourceDocument requestDocument, CancellationToken cancellationToken) { var materializer = _documentMaterializerLocator.GetMaterializerByResourceTypeName(resourceType); var document = await materializer.UpdateRecord(id, requestDocument, Request, cancellationToken); return Ok(document); } /// /// Deletes the record corresponding to the ID. /// public virtual async Task Delete(string resourceType, string id, CancellationToken cancellationToken) { var materializer = _documentMaterializerLocator.GetMaterializerByResourceTypeName(resourceType); var document = await materializer.DeleteRecord(id, Request, cancellationToken); return Ok(document); } } }