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);
|
||||
}
|
||||
}
|
||||
45
PyroFetes/Endpoints/Truck/DeleteTruckEndpoint.cs
Normal file
45
PyroFetes/Endpoints/Truck/DeleteTruckEndpoint.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
32
PyroFetes/Endpoints/Truck/GetAllTrucksEndpoint.cs
Normal file
32
PyroFetes/Endpoints/Truck/GetAllTrucksEndpoint.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Truck.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Truck;
|
||||
|
||||
public class GetAllTrucksEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest<List<ReadTruckDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/trucks");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
var trucks = await pyroFetesDbContext.Trucks
|
||||
.Include(t => t.ShowTrucks)
|
||||
.Select(t => new ReadTruckDto
|
||||
{
|
||||
Id = t.Id,
|
||||
Type = t.Type,
|
||||
MaxExplosiveCapacity = t.MaxExplosiveCapacity,
|
||||
Sizes = t.Sizes,
|
||||
Statut = t.Status,
|
||||
ShowId = t.ShowTrucks!.FirstOrDefault() != null ? t.ShowTrucks.FirstOrDefault()!.ShowId : null
|
||||
})
|
||||
.ToListAsync(ct);
|
||||
|
||||
await Send.OkAsync(trucks, ct);
|
||||
}
|
||||
}
|
||||
45
PyroFetes/Endpoints/Truck/GetTruckEndpoint.cs
Normal file
45
PyroFetes/Endpoints/Truck/GetTruckEndpoint.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Truck.Request;
|
||||
using PyroFetes.DTO.Truck.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Truck;
|
||||
|
||||
public class GetTruckEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdTruckDto, ReadTruckDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/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
|
||||
.Where(t => t.Id == req.Id.Value)
|
||||
.Select(t => new ReadTruckDto
|
||||
{
|
||||
Id = t.Id,
|
||||
Type = t.Type,
|
||||
MaxExplosiveCapacity = t.MaxExplosiveCapacity,
|
||||
Sizes = t.Sizes,
|
||||
Statut = t.Status,
|
||||
ShowId = t.ShowTrucks!.FirstOrDefault() != null ? t.ShowTrucks.FirstOrDefault()!.ShowId : null
|
||||
})
|
||||
.FirstOrDefaultAsync(ct);
|
||||
|
||||
if (truck is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await Send.OkAsync(truck, ct);
|
||||
}
|
||||
}
|
||||
82
PyroFetes/Endpoints/Truck/UpdateTruckEndpoint.cs
Normal file
82
PyroFetes/Endpoints/Truck/UpdateTruckEndpoint.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Truck.Request;
|
||||
using PyroFetes.DTO.Truck.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Truck;
|
||||
|
||||
public class UpdateTruckEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<UpdateTruckDto, ReadTruckDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/trucks/{Id}");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateTruckDto 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;
|
||||
}
|
||||
|
||||
// Mise à jour des propriétés du truck
|
||||
truck.Type = req.Type ?? truck.Type;
|
||||
if (double.TryParse(req.MaxExplosiveCapacity, out var capacity))
|
||||
{
|
||||
truck.MaxExplosiveCapacity = capacity;
|
||||
}
|
||||
truck.Sizes = req.Sizes ?? truck.Sizes;
|
||||
truck.Status = req.Statut ?? truck.Status;
|
||||
|
||||
// Gérer la relation ShowTruck
|
||||
if (req.ShowId.HasValue)
|
||||
{
|
||||
// Supprimer l'ancienne relation
|
||||
if (truck.ShowTrucks != null && truck.ShowTrucks.Any())
|
||||
{
|
||||
var existingShowTrucks = truck.ShowTrucks.ToList();
|
||||
foreach (var st in existingShowTrucks)
|
||||
{
|
||||
pyroFetesDbContext.ShowTrucks.Remove(st);
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter la nouvelle relation si ShowId n'est pas 0
|
||||
if (req.ShowId.Value != 0)
|
||||
{
|
||||
var newShowTruck = new PyroFetes.Models.ShowTruck
|
||||
{
|
||||
TruckId = truck.Id,
|
||||
ShowId = req.ShowId.Value
|
||||
};
|
||||
pyroFetesDbContext.ShowTrucks.Add(newShowTruck);
|
||||
}
|
||||
}
|
||||
|
||||
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