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.
115 lines
4.3 KiB
C#
115 lines
4.3 KiB
C#
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;
|
|
|
|
public class CatalogRepository : ICatalogRepository
|
|
{
|
|
private readonly ApplicationDbContext _db;
|
|
private readonly IMapper _mapper;
|
|
|
|
public CatalogRepository(ApplicationDbContext db, IMapper mapper)
|
|
{
|
|
_db = db;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public async Task<List<Catalog>> GetAllAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
var entities = await _db.TbmyCatalogs.AsNoTracking().ToListAsync(cancellationToken);
|
|
return _mapper.Map<List<Catalog>>(entities);
|
|
}
|
|
|
|
public async Task<Catalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
|
{
|
|
var entity = await _db.TbmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
|
return entity == null ? null : _mapper.Map<Catalog>(entity);
|
|
}
|
|
|
|
public async Task<Catalog> AddAsync(Catalog catalog, CancellationToken cancellationToken = default)
|
|
{
|
|
var entity = _mapper.Map<TbmyCatalog>(catalog);
|
|
_db.TbmyCatalogs.Add(entity);
|
|
await _db.SaveChangesAsync(cancellationToken);
|
|
return _mapper.Map<Catalog>(entity);
|
|
}
|
|
|
|
public async Task<bool> UpdateAsync(int id, Catalog catalog, CancellationToken cancellationToken = default)
|
|
{
|
|
var entity = await _db.TbmyCatalogs.FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
|
if (entity == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_mapper.Map(catalog, entity);
|
|
entity.Guid = id;
|
|
await _db.SaveChangesAsync(cancellationToken);
|
|
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);
|
|
if (entity == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_db.TbmyCatalogs.Remove(entity);
|
|
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;
|
|
}
|
|
}
|