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); } private static async Task 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(); } }