Handle unique constraint violation on ProductCompanyTableCouple create
This commit is contained in:
parent
e7b737a3dd
commit
87a2250852
1 changed files with 15 additions and 1 deletions
|
@ -4,6 +4,7 @@ using DrinkRateAPI.Contexts;
|
||||||
using DrinkRateAPI.DbEntities;
|
using DrinkRateAPI.DbEntities;
|
||||||
using DrinkRateAPI.Exceptions;
|
using DrinkRateAPI.Exceptions;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace DrinkRateAPI.Services;
|
namespace DrinkRateAPI.Services;
|
||||||
|
|
||||||
|
@ -24,7 +25,20 @@ public class ProductCompanyTableCoupleService(ApplicationDbContext context)
|
||||||
};
|
};
|
||||||
|
|
||||||
_context.CompanyTable.Add(companyTable);
|
_context.CompanyTable.Add(companyTable);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
await _context.SaveChangesAsync();
|
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(
|
return await GetProductCompanyTableCouplePostResponse(
|
||||||
productCompanyTableCouplePost.ProductTableName,
|
productCompanyTableCouplePost.ProductTableName,
|
||||||
|
|
Loading…
Reference in a new issue