Files
vegasco/src/Vegasco.Server.Api/Cars/DeleteCar.cs

38 lines
927 B
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using Vegasco.Server.Api.Persistence;
2024-08-17 16:38:40 +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")
.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,
ILoggerFactory loggerFactory,
2024-08-17 16:38:40 +02:00
CancellationToken cancellationToken)
{
var rows = await dbContext.Cars
.Where(x => x.Id == new CarId(id))
.ExecuteDeleteAsync(cancellationToken);
2024-08-17 16:38:40 +02:00
if (rows > 1)
2024-08-17 16:38:40 +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
}
return rows > 0
? TypedResults.NoContent()
: TypedResults.NotFound();
2024-08-17 16:38:40 +02:00
}
}