Add consumption logic and endpoints
This commit is contained in:
30
src/WebApi/Consumptions/DeleteConsumptions.cs
Normal file
30
src/WebApi/Consumptions/DeleteConsumptions.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace Vegasco.WebApi.Consumptions;
|
||||
|
||||
public static class DeleteConsumption
|
||||
{
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapDelete("consumptions/{id:guid}", Endpoint)
|
||||
.WithTags("Consumptions");
|
||||
}
|
||||
|
||||
private static async Task<IResult> Endpoint(
|
||||
ApplicationDbContext dbContext,
|
||||
Guid id,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Consumption? consumption = await dbContext.Consumptions.FindAsync([new ConsumptionId(id)], cancellationToken);
|
||||
if (consumption is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
dbContext.Consumptions.Remove(consumption);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return TypedResults.NoContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user