Files
PF3-fork/PyroFetes/Endpoints/Truck/GetTruckEndpoint.cs
2025-11-13 15:11:34 +01:00

46 lines
1.2 KiB
C#

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