46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
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);
|
|
}
|
|
}
|