2025-06-19 15:04:09 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Vegasco.Server.Api.Persistence;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
2025-06-12 18:22:37 +02:00
|
|
|
|
namespace Vegasco.Server.Api.Cars;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
public static class DeleteCar
|
|
|
|
|
|
{
|
|
|
|
|
|
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
return builder
|
|
|
|
|
|
.MapDelete("cars/{id:guid}", Endpoint)
|
2025-06-16 20:28:37 +02:00
|
|
|
|
.WithTags("Cars")
|
2025-06-16 20:34:09 +02:00
|
|
|
|
.WithDescription("Deletes a car by ID")
|
2025-06-16 20:28:37 +02:00
|
|
|
|
.Produces(204)
|
|
|
|
|
|
.Produces(404);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task<IResult> Endpoint(
|
|
|
|
|
|
Guid id,
|
|
|
|
|
|
ApplicationDbContext dbContext,
|
2025-06-19 15:04:09 +02:00
|
|
|
|
ILoggerFactory loggerFactory,
|
2024-08-17 16:38:40 +02:00
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2025-06-19 15:04:09 +02:00
|
|
|
|
var rows = await dbContext.Cars
|
|
|
|
|
|
.Where(x => x.Id == new CarId(id))
|
|
|
|
|
|
.ExecuteDeleteAsync(cancellationToken);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
2025-06-19 17:05:19 +02:00
|
|
|
|
if (rows == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return TypedResults.NotFound();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-19 15:04:09 +02:00
|
|
|
|
if (rows > 1)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
2025-06-19 15:04:09 +02:00
|
|
|
|
var logger = loggerFactory.CreateLogger(nameof(DeleteCar));
|
|
|
|
|
|
logger.LogWarning("Deleted '{DeletedRowCount}' rows for id '{CarId}'", rows, id);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-19 17:05:19 +02:00
|
|
|
|
return TypedResults.NoContent();
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|