Files
Projet4/PyroFetes/Endpoints/ExperienceLevel/CreateExperienceLevelEndpoint.cs
T

47 lines
1.4 KiB
C#

using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.ExperienceLevel.Response;
using PyroFetes.DTO.ExperienceLevel.Request;
namespace PyroFetes.Endpoints.ExperienceLevel;
public class CreateExperienceLevelEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateExperienceLevelDto, GetExperienceLevelDto>
{
public override void Configure()
{
Post("/experiencelevels");
AllowAnonymous();
}
public override async Task HandleAsync(CreateExperienceLevelDto req, CancellationToken ct)
{
Models.Staff? databaseStaff = await pyroFetesDbContext.Staffs.SingleOrDefaultAsync(x => x.Id == req.StaffId, cancellationToken: ct);
if (databaseStaff == null)
{
await Send.NotFoundAsync(ct);
Console.WriteLine("Customer Type not found");
return;
}
Models.ExperienceLevel experienceLevel = new()
{
Label = req.Label,
StaffId = databaseStaff.Id,
};
pyroFetesDbContext.Add(experienceLevel);
await pyroFetesDbContext.SaveChangesAsync(ct);
GetExperienceLevelDto response = new()
{
Id = experienceLevel.Id,
Label = experienceLevel.Label,
StaffId = experienceLevel.StaffId
};
await Send.OkAsync(response, ct);
}
}