feat(infra): add delete endpoint for transactions with corresponding integration tests
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
namespace FiscalOS.API.Transactions.DeleteById;
|
||||
|
||||
internal static class Endpoint
|
||||
{
|
||||
private const string Route = "/{id}";
|
||||
|
||||
public static RouteHandlerBuilder MapDeleteEndpoint(this RouteGroupBuilder groupBuilder)
|
||||
{
|
||||
return groupBuilder.MapDelete(Route, HandleAsync);
|
||||
}
|
||||
|
||||
private static async Task<IResult> HandleAsync(
|
||||
HttpContext httpContext,
|
||||
[FromRoute] Guid id,
|
||||
[FromServices] AppDbContext appDbContext,
|
||||
CancellationToken ct
|
||||
)
|
||||
{
|
||||
var userId = httpContext.GetUserId();
|
||||
|
||||
var existingTransaction = await appDbContext.Transactions
|
||||
.SingleOrDefaultAsync(t => t.UserId == userId && t.Id == id, ct);
|
||||
|
||||
if (existingTransaction is null)
|
||||
{
|
||||
return Results.NotFound();
|
||||
}
|
||||
|
||||
appDbContext.Transactions.Remove(existingTransaction);
|
||||
await appDbContext.SaveChangesAsync(ct);
|
||||
|
||||
return Results.NoContent();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using FiscalOS.API.Transactions.DeleteById;
|
||||
using FiscalOS.API.Transactions.FireWebhook;
|
||||
using FiscalOS.API.Transactions.Get;
|
||||
using FiscalOS.API.Transactions.Webhook;
|
||||
@@ -14,6 +15,7 @@ internal static class TransactionsExtensions
|
||||
.RequireAuthorization();
|
||||
|
||||
transactionsGroup.MapGetEndpoint();
|
||||
transactionsGroup.MapDeleteEndpoint();
|
||||
transactionsGroup.MapWebhookEndpoint().AllowAnonymous();
|
||||
|
||||
if (app.Environment.IsProduction() is false)
|
||||
|
||||
Reference in New Issue
Block a user