2025-06-19 15:04:09 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-07-21 20:58:45 +02:00
|
|
|
|
using System.Diagnostics;
|
2025-06-19 15:04:09 +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
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-24 19:35:29 +02:00
|
|
|
|
private static async Task<IResult> Endpoint(
|
2024-08-17 16:38:40 +02:00
|
|
|
|
Guid id,
|
|
|
|
|
|
ApplicationDbContext dbContext,
|
2025-06-19 15:04:09 +02:00
|
|
|
|
ILoggerFactory loggerFactory,
|
2024-08-17 16:38:40 +02:00
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2025-07-21 20:58:45 +02:00
|
|
|
|
Activity? activity = Activity.Current;
|
|
|
|
|
|
activity?.SetTag("id", id);
|
|
|
|
|
|
|
2025-06-24 19:28:55 +02:00
|
|
|
|
int rows = await dbContext.Cars
|
2025-06-19 15:04:09 +02:00
|
|
|
|
.Where(x => x.Id == new CarId(id))
|
|
|
|
|
|
.ExecuteDeleteAsync(cancellationToken);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
2025-06-19 17:05:19 +02:00
|
|
|
|
if (rows == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return TypedResults.NotFound();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-19 15:04:09 +02:00
|
|
|
|
if (rows > 1)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
2025-07-21 21:41:16 +02:00
|
|
|
|
ILogger logger = loggerFactory.CreateLogger(typeof(DeleteCar));
|
2025-06-19 15:04:09 +02:00
|
|
|
|
logger.LogWarning("Deleted '{DeletedRowCount}' rows for id '{CarId}'", rows, id);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-19 17:05:19 +02:00
|
|
|
|
return TypedResults.NoContent();
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|