Finalisation endpoints

This commit is contained in:
2025-11-13 15:11:34 +01:00
parent 5c12a45ae6
commit 3a09bfc8ad
20 changed files with 922 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Truck.Request;
namespace PyroFetes.Endpoints.Truck;
public class DeleteTruckEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdTruckDto>
{
public override void Configure()
{
Delete("/api/trucks/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(IdTruckDto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var truck = await pyroFetesDbContext.Trucks
.Include(t => t.ShowTrucks)
.FirstOrDefaultAsync(t => t.Id == req.Id.Value, ct);
if (truck is null)
{
await Send.NotFoundAsync(ct);
return;
}
// Supprimer les relations ShowTruck associées
if (truck.ShowTrucks != null && truck.ShowTrucks.Any())
{
pyroFetesDbContext.ShowTrucks.RemoveRange(truck.ShowTrucks);
}
// Supprimer le truck
pyroFetesDbContext.Trucks.Remove(truck);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.OkAsync(ct);
}
}