Files
vegasco/src/Vegasco.Server.Api/Consumptions/DeleteConsumptions.cs

38 lines
1006 B
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using Vegasco.Server.Api.Persistence;
2024-08-23 18:02:18 +02:00
namespace Vegasco.Server.Api.Consumptions;
2024-08-23 18:02:18 +02:00
public static class DeleteConsumption
{
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
{
return builder
.MapDelete("consumptions/{id:guid}", Endpoint)
2025-06-16 20:28:37 +02:00
.WithTags("Consumptions")
.WithDescription("Deletes a consumption entry by ID")
2025-06-16 20:28:37 +02:00
.Produces(204)
.Produces(404);
2024-08-23 18:02:18 +02:00
}
private static async Task<IResult> Endpoint(
Guid id,
ApplicationDbContext dbContext,
ILoggerFactory loggerFactory,
2024-08-23 18:02:18 +02:00
CancellationToken cancellationToken)
{
var rows = await dbContext.Consumptions
.Where(x => x.Id == new ConsumptionId(id))
.ExecuteDeleteAsync(cancellationToken);
if (rows > 1)
2024-08-23 18:02:18 +02:00
{
var logger = loggerFactory.CreateLogger(nameof(DeleteConsumption));
logger.LogWarning("Deleted '{DeletedRowCount}' rows for id '{ConsumptionId}'", rows, id);
2024-08-23 18:02:18 +02:00
}
return rows > 0
? TypedResults.NoContent()
: TypedResults.NotFound();
2024-08-23 18:02:18 +02:00
}
}