2024-08-17 16:38:40 +02:00
|
|
|
|
using FluentValidation;
|
|
|
|
|
|
using FluentValidation.Results;
|
2025-06-12 18:22:37 +02:00
|
|
|
|
using Vegasco.Server.Api.Authentication;
|
|
|
|
|
|
using Vegasco.Server.Api.Common;
|
|
|
|
|
|
using Vegasco.Server.Api.Persistence;
|
|
|
|
|
|
using Vegasco.Server.Api.Users;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
2025-06-12 18:22:37 +02:00
|
|
|
|
namespace Vegasco.Server.Api.Cars;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
public static class CreateCar
|
|
|
|
|
|
{
|
|
|
|
|
|
public record Request(string Name);
|
|
|
|
|
|
public record Response(Guid Id, string Name);
|
|
|
|
|
|
|
|
|
|
|
|
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
return builder
|
2024-08-17 16:38:40 +02:00
|
|
|
|
.MapPost("cars", Endpoint)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
.WithTags("Cars");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class Validator : AbstractValidator<Request>
|
|
|
|
|
|
{
|
|
|
|
|
|
public Validator()
|
|
|
|
|
|
{
|
|
|
|
|
|
RuleFor(x => x.Name)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
.NotEmpty()
|
|
|
|
|
|
.MaximumLength(CarTableConfiguration.NameMaxLength);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
public static async Task<IResult> Endpoint(
|
|
|
|
|
|
Request request,
|
|
|
|
|
|
IEnumerable<IValidator<Request>> validators,
|
|
|
|
|
|
ApplicationDbContext dbContext,
|
|
|
|
|
|
UserAccessor userAccessor,
|
|
|
|
|
|
CancellationToken cancellationToken)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
2024-08-17 16:38:40 +02:00
|
|
|
|
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken: cancellationToken);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
if (failedValidations.Count > 0)
|
|
|
|
|
|
{
|
2024-08-17 16:38:40 +02:00
|
|
|
|
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 17:43:22 +02:00
|
|
|
|
string userId = userAccessor.GetUserId();
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
2025-06-12 17:43:22 +02:00
|
|
|
|
User? user = await dbContext.Users.FindAsync([userId], cancellationToken: cancellationToken);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
if (user is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
user = new User
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = userId
|
|
|
|
|
|
};
|
|
|
|
|
|
await dbContext.Users.AddAsync(user, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Car car = new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = request.Name,
|
|
|
|
|
|
UserId = userId
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await dbContext.Cars.AddAsync(car, cancellationToken);
|
|
|
|
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
2024-08-17 18:00:23 +02:00
|
|
|
|
Response response = new(car.Id.Value, car.Name);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
return TypedResults.Created($"/v1/cars/{car.Id}", response);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|