Class BaseJsonApiController<TResource, TId>
- Namespace
- JsonApiDotNetCore.Controllers
- Assembly
- JsonApiDotNetCore.dll
Implements the foundational ASP.NET controller layer in the JsonApiDotNetCore architecture that delegates to a Resource Service.
public abstract class BaseJsonApiController<TResource, TId> : CoreJsonApiController where TResource : class, IIdentifiable<TId>
Type Parameters
TResource
The resource type.
TId
The resource identifier type.
- Inheritance
-
BaseJsonApiController<TResource, TId>
- Derived
- Inherited Members
Constructors
BaseJsonApiController(IJsonApiOptions, IResourceGraph, ILoggerFactory, IGetAllService<TResource, TId>?, IGetByIdService<TResource, TId>?, IGetSecondaryService<TResource, TId>?, IGetRelationshipService<TResource, TId>?, ICreateService<TResource, TId>?, IAddToRelationshipService<TResource, TId>?, IUpdateService<TResource, TId>?, ISetRelationshipService<TResource, TId>?, IDeleteService<TResource, TId>?, IRemoveFromRelationshipService<TResource, TId>?)
Creates an instance from separate services for the various individual read and write methods.
protected BaseJsonApiController(IJsonApiOptions options, IResourceGraph resourceGraph, ILoggerFactory loggerFactory, IGetAllService<TResource, TId>? getAll = null, IGetByIdService<TResource, TId>? getById = null, IGetSecondaryService<TResource, TId>? getSecondary = null, IGetRelationshipService<TResource, TId>? getRelationship = null, ICreateService<TResource, TId>? create = null, IAddToRelationshipService<TResource, TId>? addToRelationship = null, IUpdateService<TResource, TId>? update = null, ISetRelationshipService<TResource, TId>? setRelationship = null, IDeleteService<TResource, TId>? delete = null, IRemoveFromRelationshipService<TResource, TId>? removeFromRelationship = null)
Parameters
options
IJsonApiOptionsresourceGraph
IResourceGraphloggerFactory
ILoggerFactorygetAll
IGetAllService<TResource, TId>getById
IGetByIdService<TResource, TId>getSecondary
IGetSecondaryService<TResource, TId>getRelationship
IGetRelationshipService<TResource, TId>create
ICreateService<TResource, TId>addToRelationship
IAddToRelationshipService<TResource, TId>update
IUpdateService<TResource, TId>setRelationship
ISetRelationshipService<TResource, TId>delete
IDeleteService<TResource, TId>removeFromRelationship
IRemoveFromRelationshipService<TResource, TId>
BaseJsonApiController(IJsonApiOptions, IResourceGraph, ILoggerFactory, IResourceQueryService<TResource, TId>?, IResourceCommandService<TResource, TId>?)
Creates an instance from separate services for reading and writing.
protected BaseJsonApiController(IJsonApiOptions options, IResourceGraph resourceGraph, ILoggerFactory loggerFactory, IResourceQueryService<TResource, TId>? queryService = null, IResourceCommandService<TResource, TId>? commandService = null)
Parameters
options
IJsonApiOptionsresourceGraph
IResourceGraphloggerFactory
ILoggerFactoryqueryService
IResourceQueryService<TResource, TId>commandService
IResourceCommandService<TResource, TId>
BaseJsonApiController(IJsonApiOptions, IResourceGraph, ILoggerFactory, IResourceService<TResource, TId>)
Creates an instance from a read/write service.
protected BaseJsonApiController(IJsonApiOptions options, IResourceGraph resourceGraph, ILoggerFactory loggerFactory, IResourceService<TResource, TId> resourceService)
Parameters
options
IJsonApiOptionsresourceGraph
IResourceGraphloggerFactory
ILoggerFactoryresourceService
IResourceService<TResource, TId>
Methods
DeleteAsync(TId, CancellationToken)
Deletes an existing resource. Example:
DELETE /articles/1 HTTP/1.1
public virtual Task<IActionResult> DeleteAsync(TId id, CancellationToken cancellationToken)
Parameters
id
TIdcancellationToken
CancellationToken
Returns
DeleteRelationshipAsync(TId, string, ISet<IIdentifiable>, CancellationToken)
Removes resources from a to-many relationship. Example:
DELETE /articles/1/relationships/revisions HTTP/1.1
public virtual Task<IActionResult> DeleteRelationshipAsync(TId id, string relationshipName, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)
Parameters
id
TIdIdentifies the left side of the relationship.
relationshipName
stringThe relationship to remove resources from.
rightResourceIds
ISet<IIdentifiable>The set of resources to remove from the relationship.
cancellationToken
CancellationTokenPropagates notification that request handling should be canceled.
Returns
GetAsync(CancellationToken)
Gets a collection of primary resources. Example:
GET /articles HTTP/1.1
public virtual Task<IActionResult> GetAsync(CancellationToken cancellationToken)
Parameters
cancellationToken
CancellationToken
Returns
GetAsync(TId, CancellationToken)
Gets a single primary resource by ID. Example:
GET /articles/1 HTTP/1.1
public virtual Task<IActionResult> GetAsync(TId id, CancellationToken cancellationToken)
Parameters
id
TIdcancellationToken
CancellationToken
Returns
GetRelationshipAsync(TId, string, CancellationToken)
Gets a relationship value, which can be a null
, a single object or a collection. Example:
GET /articles/1/relationships/author HTTP/1.1
Example:
GET /articles/1/relationships/revisions HTTP/1.1
public virtual Task<IActionResult> GetRelationshipAsync(TId id, string relationshipName, CancellationToken cancellationToken)
Parameters
id
TIdrelationshipName
stringcancellationToken
CancellationToken
Returns
GetSecondaryAsync(TId, string, CancellationToken)
Gets a secondary resource or collection of secondary resources. Example:
GET /articles/1/author HTTP/1.1
Example:
GET /articles/1/revisions HTTP/1.1
public virtual Task<IActionResult> GetSecondaryAsync(TId id, string relationshipName, CancellationToken cancellationToken)
Parameters
id
TIdrelationshipName
stringcancellationToken
CancellationToken
Returns
PatchAsync(TId, TResource, CancellationToken)
Updates the attributes and/or relationships of an existing resource. Only the values of sent attributes are replaced. And only the values of sent relationships are replaced. Example:
PATCH /articles/1 HTTP/1.1
public virtual Task<IActionResult> PatchAsync(TId id, TResource resource, CancellationToken cancellationToken)
Parameters
id
TIdresource
TResourcecancellationToken
CancellationToken
Returns
PatchRelationshipAsync(TId, string, object?, CancellationToken)
Performs a complete replacement of a relationship on an existing resource. Example:
PATCH /articles/1/relationships/author HTTP/1.1
Example:
PATCH /articles/1/relationships/revisions HTTP/1.1
public virtual Task<IActionResult> PatchRelationshipAsync(TId id, string relationshipName, object? rightValue, CancellationToken cancellationToken)
Parameters
id
TIdIdentifies the left side of the relationship.
relationshipName
stringThe relationship for which to perform a complete replacement.
rightValue
objectThe resource or set of resources to assign to the relationship.
cancellationToken
CancellationTokenPropagates notification that request handling should be canceled.
Returns
PostAsync(TResource, CancellationToken)
Creates a new resource with attributes, relationships or both. Example:
POST /articles HTTP/1.1
public virtual Task<IActionResult> PostAsync(TResource resource, CancellationToken cancellationToken)
Parameters
resource
TResourcecancellationToken
CancellationToken
Returns
PostRelationshipAsync(TId, string, ISet<IIdentifiable>, CancellationToken)
Adds resources to a to-many relationship. Example:
POST /articles/1/revisions HTTP/1.1
public virtual Task<IActionResult> PostRelationshipAsync(TId id, string relationshipName, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)
Parameters
id
TIdIdentifies the left side of the relationship.
relationshipName
stringThe relationship to add resources to.
rightResourceIds
ISet<IIdentifiable>The set of resources to add to the relationship.
cancellationToken
CancellationTokenPropagates notification that request handling should be canceled.