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.
This commit is contained in:
OlgunR
2026-01-12 16:39:21 +01:00
parent eabf60923d
commit 97eea94090
2 changed files with 13 additions and 8 deletions

View File

@@ -50,13 +50,14 @@ public class CatalogsController : ControllerBase
return NoContent(); return NoContent();
} }
[HttpPut("sp")] [HttpPut("sp/{id:int}")]
public async Task<ActionResult<CatalogDto>> UpdateWithStoredProcedure(CatalogDto dto, CancellationToken cancellationToken) public async Task<ActionResult<CatalogDto>> UpdateWithStoredProcedure(int id, CatalogDto dto, CancellationToken cancellationToken)
{ {
dto.Guid = id;
var updated = await _service.UpdateWithStoredProcedureAsync(dto, cancellationToken); var updated = await _service.UpdateWithStoredProcedureAsync(dto, cancellationToken);
if (updated == null) if (updated == null)
{ {
return BadRequest(); return NotFound();
} }
return Ok(updated); return Ok(updated);
} }

View File

@@ -55,14 +55,18 @@ public class CatalogRepository : ICatalogRepository
public async Task<Catalog?> UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default) public async Task<Catalog?> UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default)
{ {
// ensure the record exists by CAT_TITLE to avoid insert behavior of the SP if (catalog.Guid == 0)
var exists = await _db.TbmyCatalogs.AsNoTracking().AnyAsync(x => x.CatTitle == catalog.CatTitle, cancellationToken);
if (!exists)
{ {
return null; 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 catStringParam = new SqlParameter("@CAT_STRING", catalog.CatString);
var changedWhoParam = new SqlParameter("@CHANGED_WHO", (object?)catalog.ChangedWho ?? DBNull.Value); var changedWhoParam = new SqlParameter("@CHANGED_WHO", (object?)catalog.ChangedWho ?? DBNull.Value);
var guidOutParam = new SqlParameter("@GUID", SqlDbType.Int) { Direction = ParameterDirection.Output }; var guidOutParam = new SqlParameter("@GUID", SqlDbType.Int) { Direction = ParameterDirection.Output };
@@ -79,7 +83,7 @@ public class CatalogRepository : ICatalogRepository
var guid = (int)guidOutParam.Value; var guid = (int)guidOutParam.Value;
var entity = await _db.TbmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken); 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<Catalog>(entity); return entity == null ? new Catalog { Guid = guid, CatTitle = existing.CatTitle, CatString = catalog.CatString, ChangedWho = catalog.ChangedWho } : _mapper.Map<Catalog>(entity);
} }
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default) public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)