Files
DbFirst/DbFirst.Infrastructure/Repositories/CatalogRepository.cs
OlgunR ce40abe20f Add VwmyCatalog view entity and update repository usage
Introduce VwmyCatalog as a keyless entity mapped to the VWMY_CATALOG database view. Update ApplicationDbContext to include the new DbSet and configure its mapping. Modify CatalogRepository to query the view instead of the table for GetAllAsync and GetByIdAsync. Add AutoMapper configuration for VwmyCatalog to Catalog. Add the VwmyCatalog entity class.
2026-01-12 17:00:37 +01:00

119 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 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)
{
if (catalog.Guid == 0)
{
return null;
}
var existing = await _db.TbmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == catalog.Guid, cancellationToken);
if (existing == null)
{
return null;
}
var catTitleParam = new SqlParameter("@CAT_TITLE", existing.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 = existing.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;
}
}