Add car name duplicate validation
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Vegasco.Server.Api.Authentication;
|
||||
using Vegasco.Server.Api.Common;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
@@ -19,7 +20,8 @@ public static class UpdateCar
|
||||
.WithDescription("Updates a car by ID")
|
||||
.Produces<Response>()
|
||||
.ProducesValidationProblem()
|
||||
.Produces(404);
|
||||
.Produces(404)
|
||||
.Produces(409);
|
||||
}
|
||||
|
||||
public class Validator : AbstractValidator<Request>
|
||||
@@ -53,7 +55,15 @@ public static class UpdateCar
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
car.Name = request.Name;
|
||||
var isDuplicate = await dbContext.Cars
|
||||
.AnyAsync(x => x.Name.ToUpper() == request.Name.ToUpper(), cancellationToken);
|
||||
|
||||
if (isDuplicate)
|
||||
{
|
||||
return TypedResults.Conflict();
|
||||
}
|
||||
|
||||
car.Name = request.Name.Trim();
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
Response response = new(car.Id.Value, car.Name);
|
||||
|
||||
Reference in New Issue
Block a user