Files
AP-WEB-PF3/PF3/Endpoints/Truck/UpdateTruckEndpoint.cs
2025-10-16 17:00:36 +02:00

48 lines
1.2 KiB
C#

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);
}
}