From 7f61e011ed6022826f33639c17697ad5c834c9e8 Mon Sep 17 00:00:00 2001 From: ThompsonNye Date: Mon, 23 Jun 2025 16:49:37 +0200 Subject: [PATCH] Add car name duplicate validation --- .../modules/cars/edit-car/edit-car.component.ts | 8 ++++++++ src/Vegasco.Server.Api/Cars/CreateCar.cs | 14 ++++++++++++-- src/Vegasco.Server.Api/Cars/UpdateCar.cs | 14 ++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Vegasco-Web/src/app/modules/cars/edit-car/edit-car.component.ts b/src/Vegasco-Web/src/app/modules/cars/edit-car/edit-car.component.ts index 01b0f52..3185433 100644 --- a/src/Vegasco-Web/src/app/modules/cars/edit-car/edit-car.component.ts +++ b/src/Vegasco-Web/src/app/modules/cars/edit-car/edit-car.component.ts @@ -201,6 +201,14 @@ export class EditCarComponent implements OnInit { 'Die Anwendung scheint falsche Daten an den Server zu senden.', }); break; + case error.status === 409: + this.messageService.add({ + severity: 'warn', + summary: 'Konflikt', + detail: + 'Es existiert bereits ein Auto mit diesem Namen. Bitte wähle einen anderen Namen.', + }); + break; default: console.error(error); this.messageService.add({ diff --git a/src/Vegasco.Server.Api/Cars/CreateCar.cs b/src/Vegasco.Server.Api/Cars/CreateCar.cs index ab453ed..d87efe5 100644 --- a/src/Vegasco.Server.Api/Cars/CreateCar.cs +++ b/src/Vegasco.Server.Api/Cars/CreateCar.cs @@ -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(201) - .ProducesValidationProblem(); + .ProducesValidationProblem() + .Produces(409); } public class Validator : AbstractValidator @@ -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); diff --git a/src/Vegasco.Server.Api/Cars/UpdateCar.cs b/src/Vegasco.Server.Api/Cars/UpdateCar.cs index 9d13ed8..055f509 100644 --- a/src/Vegasco.Server.Api/Cars/UpdateCar.cs +++ b/src/Vegasco.Server.Api/Cars/UpdateCar.cs @@ -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() .ProducesValidationProblem() - .Produces(404); + .Produces(404) + .Produces(409); } public class Validator : AbstractValidator @@ -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);