Files
Projet4/PyroFetes/Endpoints/Customer/CreateCustomerEndpoint.cs
2025-12-04 17:45:06 +01:00

47 lines
1.3 KiB
C#

using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Customer.Request;
using PyroFetes.DTO.Customer.Response;
namespace PyroFetes.Endpoints.Customer;
public class CreateCustomerEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateCustomerDto, GetCustomerDto>
{
public override void Configure()
{
Post("/customers");
AllowAnonymous();
}
public override async Task HandleAsync(CreateCustomerDto req, CancellationToken ct)
{
Models.CustomerType? databaseCustomerType = await pyroFetesDbContext.CustomerTypes.SingleOrDefaultAsync(x => x.Id == req.CustomerTypeId, cancellationToken: ct);
if (databaseCustomerType == null)
{
await Send.NotFoundAsync(ct);
Console.WriteLine("Customer Type not found");
return;
}
Models.Customer customer = new()
{
Note = req.Note,
CustomerTypeId = databaseCustomerType.Id,
};
pyroFetesDbContext.Add(customer);
await pyroFetesDbContext.SaveChangesAsync(ct);
GetCustomerDto response = new GetCustomerDto()
{
Id = customer.Id,
Note = customer.Note,
CustomerTypeId = customer.CustomerTypeId
};
await Send.OkAsync(response, ct);
}
}