53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using FastEndpoints;
|
|
using PyroFetes.DTO.Truck.Request;
|
|
using PyroFetes.DTO.Truck.Response;
|
|
|
|
namespace PyroFetes.Endpoints.Truck;
|
|
|
|
public class CreateTruckEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateTruckDto, ReadTruckDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/trucks");
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(CreateTruckDto req, CancellationToken ct)
|
|
{
|
|
var truck = new PyroFetes.Models.Truck
|
|
{
|
|
Type = req.Type ?? string.Empty,
|
|
MaxExplosiveCapacity = req.MaxExplosiveCapacity,
|
|
Sizes = req.Sizes,
|
|
Status = req.Status
|
|
};
|
|
|
|
pyroFetesDbContext.Trucks.Add(truck);
|
|
await pyroFetesDbContext.SaveChangesAsync(ct);
|
|
|
|
// Ajouter la relation ShowTruck si ShowId est fourni
|
|
if (req.ShowId.HasValue && req.ShowId.Value != 0)
|
|
{
|
|
var showTruck = new PyroFetes.Models.ShowTruck
|
|
{
|
|
TruckId = truck.Id,
|
|
ShowId = req.ShowId.Value
|
|
};
|
|
pyroFetesDbContext.ShowTrucks.Add(showTruck);
|
|
await pyroFetesDbContext.SaveChangesAsync(ct);
|
|
}
|
|
|
|
var result = new ReadTruckDto
|
|
{
|
|
Id = truck.Id,
|
|
Type = truck.Type,
|
|
MaxExplosiveCapacity = truck.MaxExplosiveCapacity,
|
|
Sizes = truck.Sizes,
|
|
Statut = truck.Status,
|
|
ShowId = req.ShowId
|
|
};
|
|
|
|
await Send.OkAsync(result, ct);
|
|
}
|
|
}
|