Skip to content

Commit

Permalink
add GetUpdate and Upsert endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed May 16, 2024
1 parent 8a5ce52 commit af69557
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/MediatR.CommandQuery.Endpoints/EntityCommandEndpointBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Security.Claims;

using MediatR.CommandQuery.Commands;
using MediatR.CommandQuery.Queries;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
Expand All @@ -22,6 +23,16 @@ protected override void MapGroup(RouteGroupBuilder group)
{
base.MapGroup(group);

group
.MapGet("{id}/update", GetUpdateQuery)
.Produces<TUpdateModel>()
.ProducesValidationProblem()
.ProducesProblem(StatusCodes.Status500InternalServerError)
.WithTags(EntityName)
.WithName($"Get{EntityName}Update")
.WithSummary("Get an entity for update by id")
.WithDescription("Get an entity for update by id");

group
.MapPost("", CreateCommand)
.Produces<TReadModel>()
Expand Down Expand Up @@ -73,6 +84,14 @@ protected override void MapGroup(RouteGroupBuilder group)
.WithDescription("Delete entity");
}

protected virtual async Task<TUpdateModel> GetUpdateQuery(
[FromRoute] TKey id,
ClaimsPrincipal? user = default,
CancellationToken cancellationToken = default)
{
var command = new EntityIdentifierQuery<TKey, TUpdateModel>(user, id);
return await Mediator.Send(command, cancellationToken);
}

protected virtual async Task<TReadModel> CreateCommand(
[FromBody] TCreateModel createModel,
Expand Down
28 changes: 28 additions & 0 deletions src/MediatR.CommandQuery.Mvc/EntityCommandControllerBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net.Mime;

using MediatR.CommandQuery.Commands;
using MediatR.CommandQuery.Queries;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -17,6 +18,15 @@ protected EntityCommandControllerBase(IMediator mediator) : base(mediator)
{
}

[HttpGet("{id}/update")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))]
public virtual async Task<ActionResult<TUpdateModel>> GetUpdate(
[FromRoute] TKey id,
CancellationToken cancellationToken = default)
{
return await GetUpdateQuery(id, cancellationToken);
}

[HttpPost("")]
[Consumes(MediaTypeNames.Application.Json)]
Expand All @@ -30,6 +40,19 @@ public virtual async Task<ActionResult<TReadModel>> Create(
return await CreateCommand(createModel, cancellationToken);
}

[HttpPost("{id}")]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ValidationProblemDetails))]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))]
public virtual async Task<ActionResult<TReadModel>> Upsert(
[FromRoute] TKey id,
[FromBody] TUpdateModel updateModel,
CancellationToken cancellationToken = default)
{
return await UpsertCommand(id, updateModel, cancellationToken);
}

[HttpPut("{id}")]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK)]
Expand Down Expand Up @@ -66,6 +89,11 @@ public virtual async Task<ActionResult<TReadModel>> Delete(
return await DeleteCommand(id, cancellationToken);
}

protected virtual async Task<TUpdateModel> GetUpdateQuery(TKey id, CancellationToken cancellationToken = default)
{
var command = new EntityIdentifierQuery<TKey, TUpdateModel>(User, id);
return await Mediator.Send(command, cancellationToken);
}

protected virtual async Task<TReadModel> CreateCommand(TCreateModel createModel, CancellationToken cancellationToken = default)
{
Expand Down

0 comments on commit af69557

Please sign in to comment.