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 CreateCar
|
||||
.WithTags("Cars")
|
||||
.WithDescription("Creates a new car")
|
||||
.Produces<Response>(201)
|
||||
.ProducesValidationProblem();
|
||||
.ProducesValidationProblem()
|
||||
.Produces(409);
|
||||
}
|
||||
|
||||
public class Validator : AbstractValidator<Request>
|
||||
@@ -59,10 +61,18 @@ public static class CreateCar
|
||||
|
||||
Car car = new()
|
||||
{
|
||||
Name = request.Name,
|
||||
Name = request.Name.Trim(),
|
||||
UserId = userId
|
||||
};
|
||||
|
||||
var isDuplicate = await dbContext.Cars
|
||||
.AnyAsync(x => x.Name.ToUpper() == request.Name.ToUpper(), cancellationToken);
|
||||
|
||||
if (isDuplicate)
|
||||
{
|
||||
return TypedResults.Conflict();
|
||||
}
|
||||
|
||||
await dbContext.Cars.AddAsync(car, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user