Removed Upsert endpoint and related service/repository methods. Deleted ExecuteUpsertAsync helper. Insert now throws NotImplementedException as a placeholder. Refactored Delete method in repository; logic unchanged.
86 lines
3.0 KiB
C#
86 lines
3.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> InsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
|
{
|
|
// Platzhalter: Insert-Prozedur folgt.
|
|
// TODO: Replace with dedicated insert stored procedure invocation.
|
|
throw new NotImplementedException("Insert stored procedure not implemented yet.");
|
|
}
|
|
|
|
public async Task<VwmyCatalog?> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
|
{
|
|
var exists = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
|
|
if (!exists)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
catalog.Guid = id;
|
|
|
|
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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|