forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonApiController.cs
More file actions
85 lines (77 loc) · 3.99 KB
/
JsonApiController.cs
File metadata and controls
85 lines (77 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using JSONAPI.Documents;
namespace JSONAPI.Http
{
/// <summary>
/// This ApiController is capable of serving all requests using JSON API.
/// </summary>
public class JsonApiController : ApiController
{
private readonly IDocumentMaterializerLocator _documentMaterializerLocator;
/// <summary>
/// Creates a new JsonApiController
/// </summary>
/// <param name="documentMaterializerLocator">The service locator to get document materializers for a given resource type.</param>
public JsonApiController(IDocumentMaterializerLocator documentMaterializerLocator)
{
_documentMaterializerLocator = documentMaterializerLocator;
}
/// <summary>
/// Returns a document corresponding to a set of records of this type.
/// </summary>
public virtual async Task<IHttpActionResult> GetResourceCollection(string resourceType, CancellationToken cancellationToken)
{
var materializer = _documentMaterializerLocator.GetMaterializerByResourceTypeName(resourceType);
var document = await materializer.GetRecords(Request, cancellationToken);
return Ok(document);
}
/// <summary>
/// Returns a document corresponding to the single record matching the ID.
/// </summary>
public virtual async Task<IHttpActionResult> Get(string resourceType, string id, CancellationToken cancellationToken)
{
var materializer = _documentMaterializerLocator.GetMaterializerByResourceTypeName(resourceType);
var document = await materializer.GetRecordById(id, Request, cancellationToken);
return Ok(document);
}
/// <summary>
/// Returns a document corresponding to the resource(s) related to the resource identified by the ID,
/// and the relationship name.
/// </summary>
public virtual async Task<IHttpActionResult> 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);
}
/// <summary>
/// Creates a new record corresponding to the data in the request document.
/// </summary>
public virtual async Task<IHttpActionResult> Post(string resourceType, [FromBody]ISingleResourceDocument requestDocument, CancellationToken cancellationToken)
{
var materializer = _documentMaterializerLocator.GetMaterializerByResourceTypeName(resourceType);
var document = await materializer.CreateRecord(requestDocument, Request, cancellationToken);
return Ok(document);
}
/// <summary>
/// Updates the record with the given ID with data from the request payloaad.
/// </summary>
public virtual async Task<IHttpActionResult> 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);
}
/// <summary>
/// Deletes the record corresponding to the ID.
/// </summary>
public virtual async Task<IHttpActionResult> Delete(string resourceType, string id, CancellationToken cancellationToken)
{
var materializer = _documentMaterializerLocator.GetMaterializerByResourceTypeName(resourceType);
var document = await materializer.DeleteRecord(id, cancellationToken);
return Ok(document);
}
}
}