This commit is contained in:
commit
0e79a8eb5e
1 changed files with 1282 additions and 0 deletions
41
src/Core/Jobs/CreateJobEndpoint.cs
Normal file
41
src/Core/Jobs/CreateJobEndpoint.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue