Endopoint presque fini
This commit is contained in:
43
PF3/Endpoints/Staff/UpdateStaffEndpoint.cs
Normal file
43
PF3/Endpoints/Staff/UpdateStaffEndpoint.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PF3.DTO.Staff.Request;
|
||||
using PF3.DTO.Staff.Response;
|
||||
|
||||
namespace PF3.Endpoints.Staff;
|
||||
|
||||
public class UpdateStaffEndpoint(PF3DbContext pf3DbContext) : Endpoint<UpdateStaffDto, ReadStaffDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/staff/{Id}");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateStaffDto req, CancellationToken ct)
|
||||
{
|
||||
var staff = await pf3DbContext.Staffs.FirstOrDefaultAsync(s => s.Id == req.Id, ct);
|
||||
if (staff is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
staff.FirstName = req.FirstName;
|
||||
staff.LastName = req.LastName;
|
||||
staff.Profession = req.Profession;
|
||||
staff.Email = req.Email;
|
||||
|
||||
await pf3DbContext.SaveChangesAsync(ct);
|
||||
|
||||
var result = new ReadStaffDto
|
||||
{
|
||||
Id = staff.Id,
|
||||
FirstName = staff.FirstName,
|
||||
LastName = staff.LastName,
|
||||
Profession = staff.Profession,
|
||||
Email = staff.Email
|
||||
};
|
||||
|
||||
await Send.OkAsync(result, ct);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user