Refactor catalog mapping and update logic
Removed ReverseMap from CatalogProfile for one-way mapping. Refactored UpdateAsync in CatalogRepository: eliminated existence check, set @GUID as input only, removed output handling, and simplified return logic.
This commit is contained in:
@@ -7,7 +7,7 @@ public class CatalogProfile : Profile
|
||||
{
|
||||
public CatalogProfile()
|
||||
{
|
||||
CreateMap<VwmyCatalog, CatalogReadDto>().ReverseMap();
|
||||
CreateMap<VwmyCatalog, CatalogReadDto>();
|
||||
CreateMap<CatalogWriteDto, VwmyCatalog>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,18 +34,12 @@ public class CatalogRepository : ICatalogRepository
|
||||
|
||||
public async Task<VwmyCatalog?> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var exists = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
|
||||
if (!exists)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
catalog.Guid = id;
|
||||
|
||||
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
|
||||
{
|
||||
Direction = ParameterDirection.InputOutput,
|
||||
Value = catalog.Guid == 0 ? DBNull.Value : catalog.Guid
|
||||
Direction = ParameterDirection.Input,
|
||||
Value = id
|
||||
};
|
||||
|
||||
var catTitleParam = new SqlParameter("@CAT_TITLE", catalog.CatTitle);
|
||||
@@ -53,17 +47,11 @@ public class CatalogRepository : ICatalogRepository
|
||||
var changedWhoParam = new SqlParameter("@CHANGED_WHO", (object?)catalog.ChangedWho ?? DBNull.Value);
|
||||
|
||||
await _db.Database.ExecuteSqlRawAsync(
|
||||
"EXEC dbo.PRTBMY_CATALOG_UPDATE @CAT_TITLE, @CAT_STRING, @CHANGED_WHO, @GUID OUTPUT",
|
||||
"EXEC dbo.PRTBMY_CATALOG_UPDATE @CAT_TITLE, @CAT_STRING, @CHANGED_WHO, @GUID",
|
||||
parameters: new[] { catTitleParam, catStringParam, changedWhoParam, guidParam },
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
if (guidParam.Value == DBNull.Value)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var guid = (int)guidParam.Value;
|
||||
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken);
|
||||
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)
|
||||
|
||||
Reference in New Issue
Block a user