Class CrudApiController<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse, TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto, TCrudControllerLogMessageActions>
- Namespace
- Whipstaff.AspNetCore
- Assembly
- Whipstaff.AspNetCore.dll
A generic controller supporting CRUD operations. Pre-defines CQRS activities along with Authorization and logging.
public abstract class CrudApiController<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse, TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto, TCrudControllerLogMessageActions> : QueryOnlyApiController<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse, TCrudControllerLogMessageActions> where TListQuery : IAuditableRequest<TListRequestDto, TListQueryResponse> where TListRequestDto : class, new() where TListQueryResponse : class where TViewQuery : IAuditableRequest<long, TViewQueryResponse?> where TViewQueryResponse : class where TAddCommand : IAuditableRequest<TAddRequestDto, TAddResponseDto?> where TDeleteCommand : IAuditableRequest<long, TDeleteResponseDto?> where TUpdateCommand : IAuditableRequest<TUpdateRequestDto, TUpdateResponseDto?> where TUpdateResponseDto : class? where TCrudControllerLogMessageActions : ICrudControllerLogMessageActions
Type Parameters
TListQuery
The type for the List Query.
TListRequestDto
The type for the Request DTO for the List Operation.
TListQueryResponse
The type for the Response DTO for the List Operation.
TViewQuery
The type for the View Query.
TViewQueryResponse
The type for the Response DTO for the View Operation.
TAddCommand
The type for the Add Command.
TAddRequestDto
The type for the Request DTO for the Add Operation.
TAddResponseDto
The type for the Response DTO for the Add Operation.
TDeleteCommand
The type for the Delete Command.
TDeleteResponseDto
The type for the Request DTO for the Delete Operation.
TUpdateCommand
The type for the Update Command.
TUpdateRequestDto
The type for the Request DTO for the Update Operation.
TUpdateResponseDto
The type for the Response DTO for the Update Operation.
TCrudControllerLogMessageActions
The type for the log message actions mapping class.
- Inheritance
-
QueryOnlyApiController<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse, TCrudControllerLogMessageActions>CrudApiController<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse, TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto, TCrudControllerLogMessageActions>
- Inherited Members
- Extension Methods
Constructors
CrudApiController(IAuthorizationService, ILogger<CrudApiController<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse, TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto, TCrudControllerLogMessageActions>>, IMediator, IAuditableCommandFactory<TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto>, IAuditableQueryFactory<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse>, TCrudControllerLogMessageActions)
Initializes a new instance of the CrudApiController<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse, TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto, TCrudControllerLogMessageActions> class.
protected CrudApiController(IAuthorizationService authorizationService, ILogger<CrudApiController<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse, TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto, TCrudControllerLogMessageActions>> logger, IMediator mediator, IAuditableCommandFactory<TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto> commandFactory, IAuditableQueryFactory<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse> queryFactory, TCrudControllerLogMessageActions logMessageActions)
Parameters
authorizationService
IAuthorizationServiceThe authorization service for validating access.
logger
ILogger<CrudApiController<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse, TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto, TCrudControllerLogMessageActions>>The logger object.
mediator
IMediatorThe mediatr object to publish CQRS messages to.
commandFactory
IAuditableCommandFactory<TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto>The factory for generating Command messages.
queryFactory
IAuditableQueryFactory<TListQuery, TListRequestDto, TListQueryResponse, TViewQuery, TViewQueryResponse>The factory for generating Query messages.
logMessageActions
TCrudControllerLogMessageActionsLog Message Actions for the logging events in the controller.
Properties
CommandFactory
Gets the Command Factory used for creating Commands to pass through the mediator.
protected IAuditableCommandFactory<TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto> CommandFactory { get; }
Property Value
- IAuditableCommandFactory<TAddCommand, TAddRequestDto, TAddResponseDto, TDeleteCommand, TDeleteResponseDto, TUpdateCommand, TUpdateRequestDto, TUpdateResponseDto>
Methods
DeleteAsync(long, CancellationToken)
Operation to Delete an entity.
[HttpDelete("{id:long}")]
public Task<ActionResult<TDeleteResponseDto>> DeleteAsync(long id, CancellationToken cancellationToken)
Parameters
id
longUnique ID of the entity to be deleted.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- Task<ActionResult<TDeleteResponseDto>>
A Task<TResult> representing the result of the asynchronous operation.
GetAddActionResultAsync(TAddResponseDto)
Gets the Action Result for the Add operation.
protected abstract Task<ActionResult<TAddResponseDto>> GetAddActionResultAsync(TAddResponseDto result)
Parameters
result
TAddResponseDtoThe Response DTO from the CQRS operation.
Returns
- Task<ActionResult<TAddResponseDto>>
A Task<TResult> representing the result of the asynchronous operation.
GetAddCommandAsync(TAddRequestDto, ClaimsPrincipal, CancellationToken)
Gets the CQRS Command for the Add operation.
protected abstract Task<TAddCommand> GetAddCommandAsync(TAddRequestDto addRequestDto, ClaimsPrincipal claimsPrincipal, CancellationToken cancellationToken)
Parameters
addRequestDto
TAddRequestDtoThe Add Request DTO.
claimsPrincipal
ClaimsPrincipalThe claims principal of the user requesting the add.
cancellationToken
CancellationTokenThe cancellation token for the request.
Returns
- Task<TAddCommand>
Command for performing an Add.
GetAddPolicyAsync()
Gets the authorization policy for the Add operation.
protected abstract Task<string> GetAddPolicyAsync()
Returns
- Task<string>
A Task<TResult> representing the result of the asynchronous operation.
GetDeleteActionResultAsync(TDeleteResponseDto)
Gets the Action Result for the Delete operation.
protected abstract Task<ActionResult<TDeleteResponseDto>> GetDeleteActionResultAsync(TDeleteResponseDto result)
Parameters
result
TDeleteResponseDtoThe Response DTO from the CQRS operation.
Returns
- Task<ActionResult<TDeleteResponseDto>>
A Task<TResult> representing the result of the asynchronous operation.
GetDeleteCommandAsync(long, ClaimsPrincipal, CancellationToken)
Gets the CQRS Delete Command.
protected abstract Task<TDeleteCommand> GetDeleteCommandAsync(long id, ClaimsPrincipal claimsPrincipal, CancellationToken cancellationToken)
Parameters
id
longUnique Id for the entity to delete.
claimsPrincipal
ClaimsPrincipalClaims Principal of the user requesting deletion.
cancellationToken
CancellationTokenCancellation token for the request.
Returns
- Task<TDeleteCommand>
Command for performing a delete.
GetDeletePolicyAsync()
Gets the authorization policy for the Delete operation.
protected abstract Task<string> GetDeletePolicyAsync()
Returns
- Task<string>
A Task<TResult> representing the result of the asynchronous operation.
GetUpdateActionResultAsync(TUpdateResponseDto?)
Gets the Action Result for the Update operation.
protected abstract Task<ActionResult<TUpdateResponseDto>> GetUpdateActionResultAsync(TUpdateResponseDto? result)
Parameters
result
TUpdateResponseDtoThe Response DTO from the CQRS operation.
Returns
- Task<ActionResult<TUpdateResponseDto>>
A Task<TResult> representing the result of the asynchronous operation.
GetUpdateCommandAsync(TUpdateRequestDto, ClaimsPrincipal, CancellationToken)
Gets the CQRS update command for the request.
protected abstract Task<TUpdateCommand> GetUpdateCommandAsync(TUpdateRequestDto updateRequestDto, ClaimsPrincipal claimsPrincipal, CancellationToken cancellationToken)
Parameters
updateRequestDto
TUpdateRequestDtoThe Update request DTO.
claimsPrincipal
ClaimsPrincipalThe claims principal of the user requesting the update.
cancellationToken
CancellationTokenThe cancellation token for the request.
Returns
- Task<TUpdateCommand>
CQRS Update Command.
GetUpdatePolicyAsync()
Gets the authorization policy for the Update operation.
protected abstract Task<string> GetUpdatePolicyAsync()
Returns
- Task<string>
A Task<TResult> representing the result of the asynchronous operation.
PostAsync(TAddRequestDto, CancellationToken)
Operation to create an Entity.
[HttpPost]
public Task<ActionResult<TAddResponseDto>> PostAsync(TAddRequestDto addRequestDto, CancellationToken cancellationToken)
Parameters
addRequestDto
TAddRequestDtoThe Request DTO for the Add Operation.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- Task<ActionResult<TAddResponseDto>>
A Task<TResult> representing the result of the asynchronous operation.
PutAsync(long, TUpdateRequestDto, CancellationToken)
Operation to update an entity.
[HttpPut("{id:long}")]
public Task<ActionResult<TUpdateResponseDto>> PutAsync(long id, TUpdateRequestDto updateRequestDto, CancellationToken cancellationToken)
Parameters
id
longUnique Id of the entity.
updateRequestDto
TUpdateRequestDtoThe Request DTO of the Update operation.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- Task<ActionResult<TUpdateResponseDto>>
A Task<TResult> representing the result of the asynchronous operation.