Class EntityCommandEndpointBase<TKey, TListModel, TReadModel, TCreateModel, TUpdateModel>
- Namespace
- Arbiter.CommandQuery.Endpoints
- Assembly
- Arbiter.CommandQuery.Endpoints.dll
Provides a base class for defining RESTful command endpoints for an entity, including create, update, upsert, patch, and delete operations.
public abstract class EntityCommandEndpointBase<TKey, TListModel, TReadModel, TCreateModel, TUpdateModel> : EntityQueryEndpointBase<TKey, TListModel, TReadModel>, IEndpointRoute
Type Parameters
TKey
The type of the entity key.
TListModel
The type of the list model returned by queries.
TReadModel
The type of the read model returned by single-entity queries and commands.
TCreateModel
The type of the model used to create a new entity.
TUpdateModel
The type of the model used to update or patch an entity.
- Inheritance
-
EntityQueryEndpointBase<TKey, TListModel, TReadModel>EntityCommandEndpointBase<TKey, TListModel, TReadModel, TCreateModel, TUpdateModel>
- Implements
- Inherited Members
Remarks
This class extends EntityQueryEndpointBase<TKey, TListModel, TReadModel> to provide endpoints for entity command operations. It is intended for use in applications to standardize CRUD API patterns.
Constructors
EntityCommandEndpointBase(ILoggerFactory, string, string?)
Initializes a new instance of the EntityCommandEndpointBase<TKey, TListModel, TReadModel, TCreateModel, TUpdateModel> class.
protected EntityCommandEndpointBase(ILoggerFactory loggerFactory, string entityName, string? routePrefix = null)
Parameters
loggerFactory
ILoggerFactoryThe logger factory to create an ILogger for this endpoint.
entityName
stringThe name of the entity for this endpoint.
routePrefix
stringThe route prefix for this endpoint. If not set,
entityName
is used.
Methods
CreateCommand(IMediator, TCreateModel, ClaimsPrincipal?, CancellationToken)
Creates a new entity using the provided create model and the mediator service.
protected virtual Task<Results<Ok<TReadModel>, ProblemHttpResult>> CreateCommand(IMediator mediator, TCreateModel createModel, ClaimsPrincipal? user = null, CancellationToken cancellationToken = default)
Parameters
mediator
IMediatorThe IMediator to send the request to.
createModel
TCreateModelThe model containing data for the new entity.
user
ClaimsPrincipalThe current security claims principal.
cancellationToken
CancellationTokenThe request cancellation token.
Returns
- Task<Results<Ok<TReadModel>, ProblemHttpResult>>
An awaitable task returning either Ok<TValue> with the created entity or ProblemHttpResult on error.
DeleteCommand(IMediator, TKey, ClaimsPrincipal?, CancellationToken)
Deletes an existing entity by its identifier using the mediator service.
protected virtual Task<Results<Ok<TReadModel>, ProblemHttpResult>> DeleteCommand(IMediator mediator, TKey id, ClaimsPrincipal? user = null, CancellationToken cancellationToken = default)
Parameters
mediator
IMediatorThe IMediator to send the request to.
id
TKeyThe identifier of the entity to delete.
user
ClaimsPrincipalThe current security claims principal.
cancellationToken
CancellationTokenThe request cancellation token.
Returns
- Task<Results<Ok<TReadModel>, ProblemHttpResult>>
An awaitable task returning either Ok<TValue> with the deleted entity or ProblemHttpResult on error.
GetUpdateQuery(IMediator, TKey, ClaimsPrincipal?, CancellationToken)
Retrieves an entity for update by its identifier using the mediator service.
protected virtual Task<Results<Ok<TUpdateModel>, ProblemHttpResult>> GetUpdateQuery(IMediator mediator, TKey id, ClaimsPrincipal? user = null, CancellationToken cancellationToken = default)
Parameters
mediator
IMediatorThe IMediator to send the request to.
id
TKeyThe identifier of the entity to retrieve.
user
ClaimsPrincipalThe current security claims principal.
cancellationToken
CancellationTokenThe request cancellation token.
Returns
- Task<Results<Ok<TUpdateModel>, ProblemHttpResult>>
An awaitable task returning either Ok<TValue> with the update model or ProblemHttpResult on error.
MapGroup(RouteGroupBuilder)
Maps the command endpoints for the entity, including create, update, upsert, patch, and delete operations.
protected override void MapGroup(RouteGroupBuilder group)
Parameters
group
RouteGroupBuilderThe RouteGroupBuilder used to define the endpoint group.
Remarks
This method adds endpoints for:
- GET {id}/update - Retrieve an entity for update
- POST - Create a new entity
- POST {id} - Upsert (create or update) an entity
- PUT {id} - Update an entity
- PATCH {id} - Patch an entity using a JSON patch document
- DELETE {id} - Delete an entity
PatchCommand(IMediator, TKey, JsonPatchDocument, ClaimsPrincipal?, CancellationToken)
Applies a JSON patch document to an existing entity using the mediator service.
protected virtual Task<Results<Ok<TReadModel>, ProblemHttpResult>> PatchCommand(IMediator mediator, TKey id, JsonPatchDocument jsonPatch, ClaimsPrincipal? user = null, CancellationToken cancellationToken = default)
Parameters
mediator
IMediatorThe IMediator to send the request to.
id
TKeyThe identifier of the entity to patch.
jsonPatch
JsonPatchDocumentThe JSON patch document describing the changes.
user
ClaimsPrincipalThe current security claims principal.
cancellationToken
CancellationTokenThe request cancellation token.
Returns
- Task<Results<Ok<TReadModel>, ProblemHttpResult>>
An awaitable task returning either Ok<TValue> with the patched entity or ProblemHttpResult on error.
UpdateCommand(IMediator, TKey, TUpdateModel, ClaimsPrincipal?, CancellationToken)
Updates an existing entity using the provided update model and the mediator service.
protected virtual Task<Results<Ok<TReadModel>, ProblemHttpResult>> UpdateCommand(IMediator mediator, TKey id, TUpdateModel updateModel, ClaimsPrincipal? user = null, CancellationToken cancellationToken = default)
Parameters
mediator
IMediatorThe IMediator to send the request to.
id
TKeyThe identifier of the entity to update.
updateModel
TUpdateModelThe model containing updated data for the entity.
user
ClaimsPrincipalThe current security claims principal.
cancellationToken
CancellationTokenThe request cancellation token.
Returns
- Task<Results<Ok<TReadModel>, ProblemHttpResult>>
An awaitable task returning either Ok<TValue> with the updated entity or ProblemHttpResult on error.
UpsertCommand(IMediator, TKey, TUpdateModel, ClaimsPrincipal?, CancellationToken)
Creates or updates an entity using the provided update model and the mediator service.
protected virtual Task<Results<Ok<TReadModel>, ProblemHttpResult>> UpsertCommand(IMediator mediator, TKey id, TUpdateModel updateModel, ClaimsPrincipal? user = null, CancellationToken cancellationToken = default)
Parameters
mediator
IMediatorThe IMediator to send the request to.
id
TKeyThe identifier of the entity to upsert.
updateModel
TUpdateModelThe model containing data for the entity.
user
ClaimsPrincipalThe current security claims principal.
cancellationToken
CancellationTokenThe request cancellation token.
Returns
- Task<Results<Ok<TReadModel>, ProblemHttpResult>>
An awaitable task returning either Ok<TValue> with the upserted entity or ProblemHttpResult on error.