Files
vegasco/src/Vegasco.Server.Api/Cars/DeleteCar.cs
ThompsonNye ab32be98a6
All checks were successful
continuous-integration/drone/push Build is passing
Use concrete types
2025-06-24 19:28:55 +02:00

41 lines
949 B
C#

using Microsoft.EntityFrameworkCore;
using Vegasco.Server.Api.Persistence;
namespace Vegasco.Server.Api.Cars;
public static class DeleteCar
{
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
{
return builder
.MapDelete("cars/{id:guid}", Endpoint)
.WithTags("Cars")
.WithDescription("Deletes a car by ID")
.Produces(204)
.Produces(404);
}
public static async Task<IResult> Endpoint(
Guid id,
ApplicationDbContext dbContext,
ILoggerFactory loggerFactory,
CancellationToken cancellationToken)
{
int rows = await dbContext.Cars
.Where(x => x.Id == new CarId(id))
.ExecuteDeleteAsync(cancellationToken);
if (rows == 0)
{
return TypedResults.NotFound();
}
if (rows > 1)
{
ILogger logger = loggerFactory.CreateLogger(nameof(DeleteCar));
logger.LogWarning("Deleted '{DeletedRowCount}' rows for id '{CarId}'", rows, id);
}
return TypedResults.NoContent();
}
}