2025-06-19 15:04:09 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Vegasco.Server.Api.Persistence;
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
2025-06-12 18:22:37 +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")
|
2025-06-16 20:34:09 +02:00
|
|
|
|
.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,
|
2025-06-19 15:04:09 +02:00
|
|
|
|
ApplicationDbContext dbContext,
|
|
|
|
|
|
ILoggerFactory loggerFactory,
|
2024-08-23 18:02:18 +02:00
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2025-06-19 15:04:09 +02:00
|
|
|
|
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
|
|
|
|
{
|
2025-06-19 15:04:09 +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
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-19 15:04:09 +02:00
|
|
|
|
return rows > 0
|
|
|
|
|
|
? TypedResults.NoContent()
|
|
|
|
|
|
: TypedResults.NotFound();
|
2024-08-23 18:02:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|