Endopoint presque fini
This commit is contained in:
48
PF3/Endpoints/Truck/UpdateTruckEndpoint.cs
Normal file
48
PF3/Endpoints/Truck/UpdateTruckEndpoint.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using PF3.DTO.Truck.Request;
|
||||
using PF3.DTO.Truck.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PF3;
|
||||
|
||||
namespace PF3.Endpoints.Truck;
|
||||
|
||||
public class UpdateTruckEndpoint(PF3DbContext pf3DbContext) : Endpoint<UpdateTruckDto, ReadTruckDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/trucks/{id}");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateTruckDto req, CancellationToken ct)
|
||||
{
|
||||
var id = Route<int>("id");
|
||||
|
||||
var truck = await pf3DbContext.Trucks.FirstOrDefaultAsync(t => t.Id == id, ct);
|
||||
|
||||
if (truck is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
truck.Type = req.Type;
|
||||
truck.MaxExplosiveCapacity = req.MaxExplosiveCapacity;
|
||||
truck.Sizes = req.Sizes;
|
||||
truck.Statut = req.Statut;
|
||||
truck.ShowId = req.ShowId;
|
||||
|
||||
await pf3DbContext.SaveChangesAsync(ct);
|
||||
|
||||
var result = new ReadTruckDto
|
||||
{
|
||||
Id = truck.Id,
|
||||
Type = truck.Type,
|
||||
MaxExplosiveCapacity = truck.MaxExplosiveCapacity,
|
||||
Sizes = truck.Sizes,
|
||||
Statut = truck.Statut,
|
||||
ShowId = truck.ShowId
|
||||
};
|
||||
|
||||
await Send.OkAsync(result, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user