|
| 1 | +# Controllers |
| 2 | + |
| 3 | +You need to create controllers that inherit from `JsonApiController<TEntity>` |
| 4 | + |
| 5 | +```c# |
| 6 | +public class ArticlesController : JsonApiController<Article> |
| 7 | +{ |
| 8 | + public ArticlesController( |
| 9 | + IJsonApiContext jsonApiContext, |
| 10 | + IResourceService<Article> resourceService, |
| 11 | + ILoggerFactory loggerFactory) |
| 12 | + : base(jsonApiContext, resourceService, loggerFactory) |
| 13 | + { } |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +## Non-Integer Type Keys |
| 18 | + |
| 19 | +If your model is using a type other than int for the primary key, you must explicitly declare it in the controller and service generic type definitions. |
| 20 | + |
| 21 | +```c# |
| 22 | +public class ArticlesController : JsonApiController<Article, Guid> |
| 23 | +//---------------------- ^^^^ |
| 24 | +{ |
| 25 | + public ArticlesController( |
| 26 | + IJsonApiContext jsonApiContext, |
| 27 | + IResourceService<Article, Guid> resourceService, |
| 28 | + //--------------------- ^^^^ |
| 29 | +
|
| 30 | + ILoggerFactory loggerFactory) |
| 31 | + : base(jsonApiContext, resourceService, loggerFactory) |
| 32 | + { } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +## Resource Access Control |
| 37 | + |
| 38 | +It is often desirable to limit what methods are exposed on your controller. The first way, you can do this is to simply inherit from `BaseJsonApiController` and explicitly declare what methods are available. |
| 39 | + |
| 40 | +In this example, if a client attempts to do anything other than GET a resource, an HTTP 404 Not Found response will be returned since no other methods are exposed. |
| 41 | + |
| 42 | +This approach is ok, but introduces some boilerplate that can easily be avoided. |
| 43 | + |
| 44 | +```c# |
| 45 | +public class ArticlesController : BaseJsonApiController<Article> |
| 46 | +{ |
| 47 | + public ArticlesController( |
| 48 | + IJsonApiContext jsonApiContext, |
| 49 | + IResourceService<Article> resourceService) |
| 50 | + : base(jsonApiContext, resourceService) |
| 51 | + { } |
| 52 | + |
| 53 | + [HttpGet] |
| 54 | + public override async Task<IActionResult> GetAsync() |
| 55 | + => await base.GetAsync(); |
| 56 | + |
| 57 | + [HttpGet("{id}")] |
| 58 | + public override async Task<IActionResult> GetAsync(TId id) |
| 59 | + => await base.GetAsync(id); |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Using ActionFilterAttributes |
| 64 | + |
| 65 | +The next option is to use the ActionFilterAttributes that ship with the library. The available attributes are: |
| 66 | + |
| 67 | +- `NoHttpPost`: disallow POST requests |
| 68 | +- `NoHttpPatch`: disallow PATCH requests |
| 69 | +- `NoHttpDelete`: disallow DELETE requests |
| 70 | +- `HttpReadOnly`: all of the above |
| 71 | + |
| 72 | +Not only does this reduce boilerplate, but it also provides a more meaningful HTTP response code. |
| 73 | +An attempt to use one blacklisted methods will result in a HTTP 405 Method Not Allowed response. |
| 74 | + |
| 75 | +```c# |
| 76 | +[HttpReadOnly] |
| 77 | +public class ArticlesController : BaseJsonApiController<Article> |
| 78 | +{ |
| 79 | + public ArticlesController( |
| 80 | + IJsonApiContext jsonApiContext, |
| 81 | + IResourceService<Article> resourceService) |
| 82 | + : base(jsonApiContext, resourceService) |
| 83 | + { } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Implicit Access By Service Injection |
| 88 | + |
| 89 | +Finally, you can control the allowed methods by supplying only the available service implementations. In some cases, resources may be an aggregation of entities or a view on top of the underlying entities. In these cases, there may not be a writable IResourceService implementation. In these cases, simply inject the implementation that is available. |
| 90 | + |
| 91 | +As with the ActionFilterAttributes, if a service implementation is not available to service a request, HTTP 405 Method Not Allowed will be returned. |
| 92 | + |
| 93 | +For more information about resource injection, see the next section titled Resource Services. |
| 94 | + |
| 95 | +```c# |
| 96 | +public class ReportsController : BaseJsonApiController<Report> |
| 97 | +{ |
| 98 | + public ReportsController( |
| 99 | + IJsonApiContext jsonApiContext, |
| 100 | + IGetAllService<Report> getAll) |
| 101 | + : base(jsonApiContext, getAll: getAll) |
| 102 | + { } |
| 103 | + |
| 104 | + [HttpGet] |
| 105 | + public override async Task<IActionResult> GetAsync() |
| 106 | + => await base.GetAsync(); |
| 107 | +} |
| 108 | +``` |
0 commit comments