49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using PyroFetes.DTO.HistoryOfApproval.Request;
|
|
using PyroFetes.DTO.HistoryOfApproval.Response;
|
|
|
|
namespace PyroFetes.Endpoints.HistoryOfApproval;
|
|
|
|
public class CreateHistoryOfApprovalEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateHistoryOfApprovalDto, GetHistoryOfApprovalDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/historyofapprovals");
|
|
AllowAnonymous();
|
|
}
|
|
|
|
|
|
public override async Task HandleAsync(CreateHistoryOfApprovalDto 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.HistoryOfApproval historyOfApproval = new()
|
|
{
|
|
DeliveryDate = req.DeliveryDate,
|
|
ExpirationDate = req.ExpirationDate,
|
|
StaffId = databaseStaff.Id,
|
|
};
|
|
pyroFetesDbContext.Add(historyOfApproval);
|
|
await pyroFetesDbContext.SaveChangesAsync(ct);
|
|
|
|
GetHistoryOfApprovalDto response = new()
|
|
{
|
|
Id = historyOfApproval.Id,
|
|
DeliveryDate = historyOfApproval.DeliveryDate,
|
|
ExpirationDate = historyOfApproval.ExpirationDate,
|
|
StaffId = historyOfApproval.StaffId,
|
|
};
|
|
|
|
await Send.OkAsync(response, ct);
|
|
|
|
}
|
|
} |