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;
|
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 UpdateCar
|
|
|
|
|
|
{
|
|
|
|
|
|
public record Request(string Name);
|
2025-07-21 20:58:45 +02:00
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
public record Response(Guid Id, string Name);
|
|
|
|
|
|
|
|
|
|
|
|
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
return builder
|
|
|
|
|
|
.MapPut("cars/{id:guid}", Endpoint)
|
2025-06-16 20:28:37 +02:00
|
|
|
|
.WithTags("Cars")
|
2025-06-16 20:34:09 +02:00
|
|
|
|
.WithDescription("Updates a car by ID")
|
2025-06-16 20:28:37 +02:00
|
|
|
|
.Produces<Response>()
|
|
|
|
|
|
.ProducesValidationProblem()
|
2025-06-23 16:49:37 +02:00
|
|
|
|
.Produces(404)
|
|
|
|
|
|
.Produces(409);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class Validator : AbstractValidator<Request>
|
|
|
|
|
|
{
|
|
|
|
|
|
public Validator()
|
|
|
|
|
|
{
|
|
|
|
|
|
RuleFor(x => x.Name)
|
|
|
|
|
|
.NotEmpty()
|
|
|
|
|
|
.MaximumLength(CarTableConfiguration.NameMaxLength);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-24 19:35:29 +02:00
|
|
|
|
private static async Task<IResult> Endpoint(
|
2024-08-17 16:38:40 +02:00
|
|
|
|
Guid id,
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-07-21 20:58:45 +02:00
|
|
|
|
var logger = loggerFactory.CreateLogger(nameof(UpdateCar));
|
|
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
|
|
|
|
|
|
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 18:00:23 +02:00
|
|
|
|
Car? car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
if (car is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return TypedResults.NotFound();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-24 19:28:55 +02:00
|
|
|
|
bool isDuplicate = await dbContext.Cars
|
2025-06-23 16:49:37 +02:00
|
|
|
|
.AnyAsync(x => x.Name.ToUpper() == request.Name.ToUpper(), cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
if (isDuplicate)
|
|
|
|
|
|
{
|
2025-07-21 20:58:45 +02:00
|
|
|
|
logger.LogDebug("Car with name '{CarName}' (case insensitive) already exists", request.Name);
|
2025-06-23 16:49:37 +02:00
|
|
|
|
return TypedResults.Conflict();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
car.Name = request.Name.Trim();
|
2024-08-17 16:38:40 +02:00
|
|
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
2025-07-21 20:58:45 +02:00
|
|
|
|
logger.LogTrace("Updated 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.Ok(response);
|
|
|
|
|
|
}
|
2025-07-21 20:58:45 +02:00
|
|
|
|
}
|