2025-06-12 18:22:37 +02:00
|
|
|
|
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,
|
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2025-06-12 17:43:22 +02:00
|
|
|
|
Car? car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
if (car is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return TypedResults.NotFound();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dbContext.Cars.Remove(car);
|
|
|
|
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
return TypedResults.NoContent();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|