Finalisation endpoints
This commit is contained in:
52
PyroFetes/Endpoints/Truck/CreateTruckEndpoint.cs
Normal file
52
PyroFetes/Endpoints/Truck/CreateTruckEndpoint.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user