forked from sanchezvem/PyroFetes
09/10
This commit is contained in:
45
PyroFetes/Endpoints/Movement/GetMovementEndpoint.cs
Normal file
45
PyroFetes/Endpoints/Movement/GetMovementEndpoint.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using API.DTO.Movement.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Movement;
|
||||
|
||||
public class GetMovementRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetMovementEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<GetMovementRequest, GetMovementDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/movements/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetMovementRequest req, CancellationToken ct)
|
||||
{
|
||||
|
||||
Models.Movement? movement = await pyrofetesdbcontext
|
||||
.Movements
|
||||
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (movement == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun mouvement avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
GetMovementDto responseDto = new()
|
||||
{
|
||||
Id = req.Id,
|
||||
Date = movement.Date,
|
||||
Start = movement.Start,
|
||||
Arrival = movement.Arrival,
|
||||
Quantity = movement.Quantity
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user