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:
@@ -55,14 +55,18 @@ public class CatalogRepository : ICatalogRepository
|
||||
|
||||
public async Task<Catalog?> 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<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)
|
||||
|
||||
Reference in New Issue
Block a user