diff --git a/DrinkRateAPI/Services/ProductCompanyTableCoupleService.cs b/DrinkRateAPI/Services/ProductCompanyTableCoupleService.cs index a5b4e90..9eac858 100644 --- a/DrinkRateAPI/Services/ProductCompanyTableCoupleService.cs +++ b/DrinkRateAPI/Services/ProductCompanyTableCoupleService.cs @@ -4,6 +4,7 @@ using DrinkRateAPI.Contexts; using DrinkRateAPI.DbEntities; using DrinkRateAPI.Exceptions; using Microsoft.EntityFrameworkCore; +using Npgsql; namespace DrinkRateAPI.Services; @@ -24,7 +25,20 @@ public class ProductCompanyTableCoupleService(ApplicationDbContext context) }; _context.CompanyTable.Add(companyTable); - await _context.SaveChangesAsync(); + + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateException ex) when + (ex.InnerException is PostgresException + { + SqlState: PostgresErrorCodes.UniqueViolation + } + ) + { + throw new BadRequestException("Product table or company table with the same name already exists."); + } return await GetProductCompanyTableCouplePostResponse( productCompanyTableCouplePost.ProductTableName,