41 lines
1 KiB
C#
41 lines
1 KiB
C#
using TestWeb.Infra;
|
|
using TestWeb.Utils;
|
|
|
|
namespace TestWeb.Core.Jobs;
|
|
|
|
public static class CreateJobEndpoint
|
|
{
|
|
public record Request(
|
|
string ClientName,
|
|
string Pickup,
|
|
string Dropoff
|
|
);
|
|
|
|
public record Response(int Id);
|
|
|
|
public class RequestValidator : AbstractValidator<Request>
|
|
{
|
|
public RequestValidator()
|
|
{
|
|
RuleFor(x => x.ClientName).NotEmpty().MaximumLength(250);
|
|
RuleFor(x => x.Pickup).NotEmpty().MaximumLength(120);
|
|
RuleFor(x => x.Dropoff).NotEmpty().MaximumLength(120);
|
|
}
|
|
}
|
|
|
|
public static async Task<Ok<Response>> Handle(Request request, AppDbContext db, CancellationToken ct)
|
|
{
|
|
var row = new Job
|
|
{
|
|
ClientName = request.ClientName,
|
|
Pickup = request.Pickup,
|
|
Dropoff = request.Dropoff,
|
|
};
|
|
|
|
await db.Jobs.AddAsync(row, ct);
|
|
await db.SaveChangesAsync(ct);
|
|
|
|
var response = new Response(row.Id);
|
|
return TypedResults.Ok(response);
|
|
}
|
|
}
|