From 97eea9409003b86dbe2df170533e523d43ca749e Mon Sep 17 00:00:00 2001 From: OlgunR Date: Mon, 12 Jan 2026 16:39:21 +0100 Subject: [PATCH] Update SP-based catalog update to use id route param Refactor UpdateWithStoredProcedure to accept id from the route and set dto.Guid accordingly. In the repository, use Guid for existence checks instead of CAT_TITLE, and ensure CatTitle is not changed via the SP. Improve error handling to return NotFound when appropriate. --- DbFirst.API/Controllers/CatalogsController.cs | 7 ++++--- .../Repositories/CatalogRepository.cs | 14 +++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/DbFirst.API/Controllers/CatalogsController.cs b/DbFirst.API/Controllers/CatalogsController.cs index d94cec4..82c6307 100644 --- a/DbFirst.API/Controllers/CatalogsController.cs +++ b/DbFirst.API/Controllers/CatalogsController.cs @@ -50,13 +50,14 @@ public class CatalogsController : ControllerBase return NoContent(); } - [HttpPut("sp")] - public async Task> UpdateWithStoredProcedure(CatalogDto dto, CancellationToken cancellationToken) + [HttpPut("sp/{id:int}")] + public async Task> UpdateWithStoredProcedure(int id, CatalogDto dto, CancellationToken cancellationToken) { + dto.Guid = id; var updated = await _service.UpdateWithStoredProcedureAsync(dto, cancellationToken); if (updated == null) { - return BadRequest(); + return NotFound(); } return Ok(updated); } diff --git a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs index 7b66f0b..2e69fde 100644 --- a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs +++ b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs @@ -55,14 +55,18 @@ public class CatalogRepository : ICatalogRepository public async Task UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default) { - // ensure the record exists by CAT_TITLE to avoid insert behavior of the SP - var exists = await _db.TbmyCatalogs.AsNoTracking().AnyAsync(x => x.CatTitle == catalog.CatTitle, cancellationToken); - if (!exists) + 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", catalog.CatTitle); + 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 }; @@ -79,7 +83,7 @@ public class CatalogRepository : ICatalogRepository 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 = catalog.CatTitle, CatString = catalog.CatString, ChangedWho = catalog.ChangedWho } : _mapper.Map(entity); + return entity == null ? new Catalog { Guid = guid, CatTitle = existing.CatTitle, CatString = catalog.CatString, ChangedWho = catalog.ChangedWho } : _mapper.Map(entity); } public async Task DeleteAsync(int id, CancellationToken cancellationToken = default)