Removed direct usage of TbmyCatalog entity and DbSet. All create, update, and delete operations now use stored procedures. Entity lookups and mappings are performed via the VwmyCatalog view. Updated AutoMapper profile and DbContext configuration accordingly. Catalog creation now sets ChangedWho/ChangedWhen to "system" and current UTC time.
123 lines
4.3 KiB
C#
123 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 viewRows = await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken);
|
|
return _mapper.Map<List<Catalog>>(viewRows);
|
|
}
|
|
|
|
public async Task<Catalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
|
{
|
|
var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
|
return viewRow == null ? null : _mapper.Map<Catalog>(viewRow);
|
|
}
|
|
|
|
public async Task<Catalog> AddAsync(Catalog catalog, CancellationToken cancellationToken = default)
|
|
{
|
|
var created = await UpsertWithStoredProcedureAsync(catalog, cancellationToken);
|
|
if (created == null)
|
|
{
|
|
throw new InvalidOperationException("Failed to create catalog via stored procedure.");
|
|
}
|
|
|
|
return created;
|
|
}
|
|
|
|
public async Task<bool> UpdateAsync(int id, Catalog catalog, CancellationToken cancellationToken = default)
|
|
{
|
|
var existing = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
|
|
if (!existing)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
catalog.Guid = id;
|
|
var updated = await UpsertWithStoredProcedureAsync(catalog, cancellationToken);
|
|
return updated != null;
|
|
}
|
|
|
|
public async Task<Catalog?> UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default)
|
|
{
|
|
if (catalog.Guid == 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var existing = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == catalog.Guid, cancellationToken);
|
|
if (!existing)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return await UpsertWithStoredProcedureAsync(catalog, cancellationToken);
|
|
}
|
|
|
|
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)
|
|
{
|
|
var exists = await _db.VwmyCatalogs.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;
|
|
}
|
|
|
|
public async Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default)
|
|
{
|
|
return await DeleteAsync(id, cancellationToken);
|
|
}
|
|
|
|
private async Task<Catalog?> UpsertWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken)
|
|
{
|
|
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
|
|
{
|
|
Direction = ParameterDirection.InputOutput,
|
|
Value = catalog.Guid == 0 ? DBNull.Value : catalog.Guid
|
|
};
|
|
|
|
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);
|
|
|
|
await _db.Database.ExecuteSqlRawAsync(
|
|
"EXEC dbo.PRTBMY_CATALOG_UPDATE @CAT_TITLE, @CAT_STRING, @CHANGED_WHO, @GUID OUTPUT",
|
|
parameters: new[] { catTitleParam, catStringParam, changedWhoParam, guidParam },
|
|
cancellationToken: cancellationToken);
|
|
|
|
if (guidParam.Value == DBNull.Value)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var guid = (int)guidParam.Value;
|
|
var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken);
|
|
return viewRow == null ? null : _mapper.Map<Catalog>(viewRow);
|
|
}
|
|
}
|