2024-08-17 16:38:40 +02:00
|
|
|
|
using FluentValidation;
|
|
|
|
|
|
using FluentValidation.Results;
|
2025-06-23 16:49:37 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
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)
|
2025-06-16 20:28:37 +02:00
|
|
|
|
.WithTags("Cars")
|
2025-06-16 20:34:09 +02:00
|
|
|
|
.WithDescription("Creates a new car")
|
2025-06-16 20:28:37 +02:00
|
|
|
|
.Produces<Response>(201)
|
2025-06-23 16:49:37 +02:00
|
|
|
|
.ProducesValidationProblem()
|
|
|
|
|
|
.Produces(409);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-24 19:35:29 +02:00
|
|
|
|
private static async Task<IResult> Endpoint(
|
2024-08-17 16:38:40 +02:00
|
|
|
|
Request request,
|
|
|
|
|
|
IEnumerable<IValidator<Request>> validators,
|
|
|
|
|
|
ApplicationDbContext dbContext,
|
|
|
|
|
|
UserAccessor userAccessor,
|
2025-07-21 20:58:45 +02:00
|
|
|
|
ILoggerFactory loggerFactory,
|
2024-08-17 16:38:40 +02:00
|
|
|
|
CancellationToken cancellationToken)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
2025-07-21 21:41:16 +02:00
|
|
|
|
ILogger logger = loggerFactory.CreateLogger(typeof(CreateCar));
|
2025-07-21 20:58:45 +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)
|
|
|
|
|
|
{
|
2025-07-21 20:58:45 +02:00
|
|
|
|
logger.LogDebug(
|
|
|
|
|
|
"Validation failed for request {@Request} with errors {@Errors}",
|
|
|
|
|
|
request,
|
|
|
|
|
|
failedValidations
|
|
|
|
|
|
.Where(x => !x.IsValid)
|
|
|
|
|
|
.SelectMany(x => x.Errors)
|
|
|
|
|
|
.Select(x => x.ErrorMessage));
|
|
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-21 20:58:45 +02:00
|
|
|
|
bool isDuplicate = await dbContext.Cars
|
|
|
|
|
|
.AnyAsync(x => x.Name.ToUpper() == request.Name.ToUpper(), cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
if (isDuplicate)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogDebug("Car with name '{CarName}' (case insensitive) already exists", request.Name);
|
|
|
|
|
|
return TypedResults.Conflict();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-07-21 20:58:45 +02:00
|
|
|
|
logger.LogDebug("User with ID '{UserId}' not found, creating new user", userId);
|
|
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
user = new User
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = userId
|
|
|
|
|
|
};
|
|
|
|
|
|
await dbContext.Users.AddAsync(user, cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Car car = new()
|
|
|
|
|
|
{
|
2025-06-23 16:49:37 +02:00
|
|
|
|
Name = request.Name.Trim(),
|
2024-08-17 16:38:40 +02:00
|
|
|
|
UserId = userId
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await dbContext.Cars.AddAsync(car, cancellationToken);
|
|
|
|
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
2025-07-21 20:58:45 +02:00
|
|
|
|
logger.LogTrace("Created new car: {@Car}", car);
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|