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

33 lines
733 B
C#
Raw Normal View History

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")
.Produces(204)
.Produces(404);
2024-08-17 16:38:40 +02:00
}
public static async Task<IResult> Endpoint(
Guid id,
ApplicationDbContext dbContext,
CancellationToken cancellationToken)
{
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();
}
}