47 lines
1.3 KiB
C#
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);
|
|
|
|
}
|
|
} |