Rename WebApi project to Vegasco.Server.Api
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
And update all references including comments etc.
This commit is contained in:
58
src/Vegasco.Server.Api/Cars/UpdateCar.cs
Normal file
58
src/Vegasco.Server.Api/Cars/UpdateCar.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Vegasco.Server.Api.Authentication;
|
||||
using Vegasco.Server.Api.Common;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Cars;
|
||||
|
||||
public static class UpdateCar
|
||||
{
|
||||
public record Request(string Name);
|
||||
public record Response(Guid Id, string Name);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapPut("cars/{id:guid}", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
public class Validator : AbstractValidator<Request>
|
||||
{
|
||||
public Validator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty()
|
||||
.MaximumLength(CarTableConfiguration.NameMaxLength);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<IResult> Endpoint(
|
||||
Guid id,
|
||||
Request request,
|
||||
IEnumerable<IValidator<Request>> validators,
|
||||
ApplicationDbContext dbContext,
|
||||
UserAccessor userAccessor,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
Car? car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken);
|
||||
|
||||
if (car is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
car.Name = request.Name;
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
Response response = new(car.Id.Value, car.Name);
|
||||
return TypedResults.Ok(response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user