Replaced the InsertAsync placeholder with a full implementation that inserts a new VwmyCatalog using the dbo.PRTBMY_CATALOG_INSERT stored procedure. The method sets up SQL parameters, handles the output GUID, and retrieves the inserted catalog from the view. Exceptions are thrown if the insert fails or the catalog cannot be loaded.
98 lines
3.6 KiB
C#
98 lines
3.6 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)
|
|
{
|
|
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
|
|
var catTitleParam = new SqlParameter("@CAT_TITLE", catalog.CatTitle);
|
|
var catStringParam = new SqlParameter("@CAT_STRING", catalog.CatString);
|
|
var addedWhoParam = new SqlParameter("@ADDED_WHO", (object?)catalog.AddedWho ?? DBNull.Value);
|
|
|
|
await _db.Database.ExecuteSqlRawAsync(
|
|
"EXEC dbo.PRTBMY_CATALOG_INSERT @CAT_TITLE, @CAT_STRING, @ADDED_WHO, @GUID OUTPUT",
|
|
parameters: new[] { catTitleParam, catStringParam, addedWhoParam, guidParam },
|
|
cancellationToken: cancellationToken);
|
|
|
|
if (guidParam.Value == DBNull.Value)
|
|
{
|
|
throw new InvalidOperationException("Failed to insert catalog via stored procedure.");
|
|
}
|
|
|
|
var guid = (int)guidParam.Value;
|
|
var created = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken);
|
|
if (created == null)
|
|
{
|
|
throw new InvalidOperationException("Inserted catalog could not be loaded from view.");
|
|
}
|
|
|
|
return created;
|
|
}
|
|
|
|
public async Task<VwmyCatalog?> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
|
{
|
|
catalog.Guid = id;
|
|
|
|
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
|
|
{
|
|
Direction = ParameterDirection.Input,
|
|
Value = id
|
|
};
|
|
|
|
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",
|
|
parameters: new[] { catTitleParam, catStringParam, changedWhoParam, guidParam },
|
|
cancellationToken: cancellationToken);
|
|
|
|
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, 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;
|
|
}
|
|
}
|