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()
|
public CatalogProfile()
|
||||||
{
|
{
|
||||||
CreateMap<VwmyCatalog, CatalogReadDto>().ReverseMap();
|
CreateMap<VwmyCatalog, CatalogReadDto>();
|
||||||
CreateMap<CatalogWriteDto, VwmyCatalog>();
|
CreateMap<CatalogWriteDto, VwmyCatalog>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,18 +34,12 @@ public class CatalogRepository : ICatalogRepository
|
|||||||
|
|
||||||
public async Task<VwmyCatalog?> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
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;
|
catalog.Guid = id;
|
||||||
|
|
||||||
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
|
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
|
||||||
{
|
{
|
||||||
Direction = ParameterDirection.InputOutput,
|
Direction = ParameterDirection.Input,
|
||||||
Value = catalog.Guid == 0 ? DBNull.Value : catalog.Guid
|
Value = id
|
||||||
};
|
};
|
||||||
|
|
||||||
var catTitleParam = new SqlParameter("@CAT_TITLE", catalog.CatTitle);
|
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);
|
var changedWhoParam = new SqlParameter("@CHANGED_WHO", (object?)catalog.ChangedWho ?? DBNull.Value);
|
||||||
|
|
||||||
await _db.Database.ExecuteSqlRawAsync(
|
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 },
|
parameters: new[] { catTitleParam, catStringParam, changedWhoParam, guidParam },
|
||||||
cancellationToken: cancellationToken);
|
cancellationToken: cancellationToken);
|
||||||
|
|
||||||
if (guidParam.Value == DBNull.Value)
|
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var guid = (int)guidParam.Value;
|
|
||||||
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)
|
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)
|
||||||
|
|||||||
Reference in New Issue
Block a user