From ab1f73f987ebfb1514a8bc35f1bb5ce7f76e23f2 Mon Sep 17 00:00:00 2001 From: OlgunR Date: Fri, 16 Jan 2026 11:42:38 +0100 Subject: [PATCH] Implement InsertAsync with stored procedure in CatalogRepository 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. --- .../Repositories/CatalogRepository.cs | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs index 0227349..f37ed80 100644 --- a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs +++ b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs @@ -27,9 +27,33 @@ public class CatalogRepository : ICatalogRepository public async Task 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."); + 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 UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)