Files
vegasco/src/Vegasco.Server.Api/Cars/UpdateCar.cs

90 lines
2.3 KiB
C#
Raw Normal View History

2024-08-17 16:38:40 +02:00
using FluentValidation;
using FluentValidation.Results;
2025-06-23 16:49:37 +02:00
using Microsoft.EntityFrameworkCore;
using Vegasco.Server.Api.Authentication;
using Vegasco.Server.Api.Common;
using Vegasco.Server.Api.Persistence;
2024-08-17 16:38:40 +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")
.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);
}
}
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 21:41:16 +02:00
ILogger logger = loggerFactory.CreateLogger(typeof(UpdateCar));
2025-07-21 20:58:45 +02:00
2024-08-17 16:38:40 +02:00
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
if (failedValidations.Count > 0)
{
string[] errors = failedValidations
.Where(x => !x.IsValid)
.SelectMany(x => x.Errors)
.Select(x => x.ErrorMessage)
.ToArray();
2025-07-21 20:58:45 +02:00
logger.LogDebug(
"Validation failed for request {@Request} with errors {@Errors}",
request,
errors);
2025-07-21 20:58:45 +02:00
2024-08-17 16:38:40 +02:00
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
}
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);
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
}