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 catTitleParam = new SqlParameter("@CAT_TITLE", catalog.CatTitle); + 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 }; @@ -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)