Add SP-based update/delete for Catalogs, new endpoints
Added support for updating and deleting Catalog records via SQL Server stored procedures. Introduced PUT /catalogs/sp and DELETE /catalogs/sp/{id} endpoints in CatalogsController. Extended service and repository interfaces and implementations to handle stored procedure operations. Added Microsoft.Data.SqlClient package for direct SQL execution. Repository checks for record existence before SP calls to prevent unintended behavior.
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -2,7 +2,9 @@ using AutoMapper;
|
||||
using DbFirst.Domain.DomainEntities;
|
||||
using DbFirst.Domain.Repositories;
|
||||
using DbFirst.Infrastructure.ScaffoldEntities;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Data;
|
||||
|
||||
namespace DbFirst.Infrastructure.Repositories;
|
||||
|
||||
@@ -51,6 +53,35 @@ public class CatalogRepository : ICatalogRepository
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<Catalog?> UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// ensure the record exists by CAT_TITLE to avoid insert behavior of the SP
|
||||
var exists = await _db.TbmyCatalogs.AsNoTracking().AnyAsync(x => x.CatTitle == catalog.CatTitle, cancellationToken);
|
||||
if (!exists)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var catTitleParam = new SqlParameter("@CAT_TITLE", catalog.CatTitle);
|
||||
var catStringParam = new SqlParameter("@CAT_STRING", catalog.CatString);
|
||||
var changedWhoParam = new SqlParameter("@CHANGED_WHO", (object?)catalog.ChangedWho ?? DBNull.Value);
|
||||
var guidOutParam = new SqlParameter("@GUID", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
||||
|
||||
await _db.Database.ExecuteSqlRawAsync(
|
||||
"EXEC dbo.PRTBMY_CATALOG_UPDATE @CAT_TITLE, @CAT_STRING, @CHANGED_WHO, @GUID OUTPUT",
|
||||
parameters: new[] { catTitleParam, catStringParam, changedWhoParam, guidOutParam },
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
if (guidOutParam.Value == DBNull.Value)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var guid = (int)guidOutParam.Value;
|
||||
var entity = await _db.TbmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken);
|
||||
return entity == null ? new Catalog { Guid = guid, CatTitle = catalog.CatTitle, CatString = catalog.CatString, ChangedWho = catalog.ChangedWho } : _mapper.Map<Catalog>(entity);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await _db.TbmyCatalogs.FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
||||
@@ -63,4 +94,21 @@ public class CatalogRepository : ICatalogRepository
|
||||
await _db.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var exists = await _db.TbmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
|
||||
if (!exists)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var guidParam = new SqlParameter("@GUID", id);
|
||||
await _db.Database.ExecuteSqlRawAsync(
|
||||
"EXEC dbo.PRTBMY_CATALOG_DELETE @GUID",
|
||||
parameters: new[] { guidParam },
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user