forked from sanchezvem/PyroFetes
43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
using PyroFetes.DTO.Material.Request;
|
|
using PyroFetes.DTO.Material.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.AspNetCore.Server.Kestrel;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace PyroFetes.Endpoints.Material;
|
|
|
|
public class UpdateMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint<UpdateMaterialDto, GetMaterialDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Put("/api/material/{@id}", x => new { x.Id });
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(UpdateMaterialDto req, CancellationToken ct)
|
|
{
|
|
Models.Material? materialToEdit = await appDbContext
|
|
.Materials
|
|
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
|
|
|
|
if (materialToEdit == null)
|
|
{
|
|
Console.WriteLine($"Aucun matériel avec l'id {req.Id} trouvé.");
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
|
|
}
|
|
materialToEdit.Label = req.Label;
|
|
materialToEdit.WarehouseId = req.WarehouseId;
|
|
await appDbContext.SaveChangesAsync(ct);
|
|
|
|
GetMaterialDto responseDto = new()
|
|
{
|
|
Id = req.Id,
|
|
Label = req.Label,
|
|
WarehouseId = req.WarehouseId,
|
|
};
|
|
await Send.OkAsync(responseDto, ct);
|
|
}
|
|
|
|
} |