Refactor Catalog update/upsert endpoints and service logic

- Update endpoint now returns CatalogReadDto or NotFound
- Replace UpdateWithStoredProcedure with Upsert endpoint (PUT /upsert)
- Rename AddAsync to InsertAsync; UpdateAsync returns DTO or null
- Introduce UpsertAsync for combined insert/update logic
- Remove stored procedure-specific update/delete methods
- Use DTOs consistently in controller/service responses
- Repository methods now return entities instead of booleans
This commit is contained in:
OlgunR
2026-01-14 15:29:09 +01:00
parent 6caf3a4c07
commit a849a88fa3
5 changed files with 47 additions and 83 deletions

View File

@@ -25,44 +25,33 @@ public class CatalogRepository : ICatalogRepository
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
}
public async Task<VwmyCatalog> AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
public async Task<VwmyCatalog> InsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
{
var created = await UpsertWithStoredProcedureAsync(catalog, cancellationToken);
catalog.Guid = 0;
var created = await ExecuteUpsertAsync(catalog, cancellationToken);
if (created == null)
{
throw new InvalidOperationException("Failed to create catalog via stored procedure.");
throw new InvalidOperationException("Failed to insert catalog via stored procedure.");
}
return created;
}
public async Task<bool> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)
public async Task<VwmyCatalog?> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)
{
var existing = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
if (!existing)
var exists = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
if (!exists)
{
return false;
return null;
}
catalog.Guid = id;
var updated = await UpsertWithStoredProcedureAsync(catalog, cancellationToken);
return updated != null;
return await ExecuteUpsertAsync(catalog, cancellationToken);
}
public async Task<VwmyCatalog?> UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
public async Task<VwmyCatalog?> UpsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
{
if (catalog.Guid == 0)
{
return null;
}
var existing = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == catalog.Guid, cancellationToken);
if (!existing)
{
return null;
}
return await UpsertWithStoredProcedureAsync(catalog, cancellationToken);
return await ExecuteUpsertAsync(catalog, cancellationToken);
}
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)
@@ -82,12 +71,7 @@ public class CatalogRepository : ICatalogRepository
return true;
}
public async Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default)
{
return await DeleteAsync(id, cancellationToken);
}
private async Task<VwmyCatalog?> UpsertWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken)
private async Task<VwmyCatalog?> ExecuteUpsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken)
{
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
{