Files
DbFirst/DbFirst.Infrastructure/Repositories/CatalogRepository.cs
OlgunR 6caf3a4c07 Refactor to use VwmyCatalog as primary catalog entity
Replaced all usage of the Catalog entity with VwmyCatalog across application, domain, and infrastructure layers. Updated AutoMapper profiles, repository interfaces, and service logic to use VwmyCatalog. Removed the obsolete Catalog entity and related mapping profiles. Moved VwmyCatalog to the Domain.Entities namespace and cleaned up project structure. This change simplifies the domain model and eliminates redundant entity definitions.
2026-01-14 15:01:13 +01:00

116 lines
4.0 KiB
C#

using DbFirst.Domain.Repositories;
using DbFirst.Domain.Entities;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Data;
namespace DbFirst.Infrastructure.Repositories;
public class CatalogRepository : ICatalogRepository
{
private readonly ApplicationDbContext _db;
public CatalogRepository(ApplicationDbContext db)
{
_db = db;
}
public async Task<List<VwmyCatalog>> GetAllAsync(CancellationToken cancellationToken = default)
{
return await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken);
}
public async Task<VwmyCatalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
}
public async Task<VwmyCatalog> AddAsync(VwmyCatalog 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, VwmyCatalog 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<VwmyCatalog?> UpdateWithStoredProcedureAsync(VwmyCatalog 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<VwmyCatalog?> UpsertWithStoredProcedureAsync(VwmyCatalog 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;
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken);
}
}