Compare commits

...

2 Commits

Author SHA1 Message Date
OlgunR
facc376f74 Remove Upsert functionality from Catalog API
Removed Upsert endpoint and related service/repository methods. Deleted ExecuteUpsertAsync helper. Insert now throws NotImplementedException as a placeholder. Refactored Delete method in repository; logic unchanged.
2026-01-15 16:57:33 +01:00
OlgunR
a849a88fa3 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
2026-01-14 15:29:09 +01:00
5 changed files with 31 additions and 109 deletions

View File

@@ -40,20 +40,9 @@ public class CatalogsController : ControllerBase
}
[HttpPut("{id:int}")]
public async Task<IActionResult> Update(int id, CatalogWriteDto dto, CancellationToken cancellationToken)
public async Task<ActionResult<CatalogReadDto>> Update(int id, CatalogWriteDto dto, CancellationToken cancellationToken)
{
var updated = await _service.UpdateAsync(id, dto, cancellationToken);
if (!updated)
{
return NotFound();
}
return NoContent();
}
[HttpPut("sp/{id:int}")]
public async Task<ActionResult<CatalogReadDto>> UpdateWithStoredProcedure(int id, CatalogWriteDto dto, CancellationToken cancellationToken)
{
var updated = await _service.UpdateWithStoredProcedureAsync(id, dto, cancellationToken);
if (updated == null)
{
return NotFound();
@@ -71,15 +60,4 @@ public class CatalogsController : ControllerBase
}
return NoContent();
}
[HttpDelete("sp/{id:int}")]
public async Task<IActionResult> DeleteWithStoredProcedure(int id, CancellationToken cancellationToken)
{
var deleted = await _service.DeleteWithStoredProcedureAsync(id, cancellationToken);
if (!deleted)
{
return NotFound();
}
return NoContent();
}
}

View File

@@ -35,28 +35,11 @@ public class CatalogService : ICatalogService
entity.ChangedWho = "system";
entity.ChangedWhen = DateTime.UtcNow;
var created = await _repository.AddAsync(entity, cancellationToken);
var created = await _repository.InsertAsync(entity, cancellationToken);
return _mapper.Map<CatalogReadDto>(created);
}
public async Task<bool> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default)
{
var existing = await _repository.GetByIdAsync(id, cancellationToken);
if (existing == null)
{
return false;
}
var entity = _mapper.Map<VwmyCatalog>(dto);
entity.Guid = id;
entity.AddedWho = existing.AddedWho;
entity.AddedWhen = existing.AddedWhen;
entity.ChangedWho = "system";
entity.ChangedWhen = DateTime.UtcNow;
return await _repository.UpdateAsync(id, entity, cancellationToken);
}
public async Task<CatalogReadDto?> UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default)
public async Task<CatalogReadDto?> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default)
{
var existing = await _repository.GetByIdAsync(id, cancellationToken);
if (existing == null)
@@ -71,7 +54,7 @@ public class CatalogService : ICatalogService
entity.ChangedWho = "system";
entity.ChangedWhen = DateTime.UtcNow;
var updated = await _repository.UpdateWithStoredProcedureAsync(entity, cancellationToken);
var updated = await _repository.UpdateAsync(id, entity, cancellationToken);
return updated == null ? null : _mapper.Map<CatalogReadDto>(updated);
}
@@ -79,9 +62,4 @@ public class CatalogService : ICatalogService
{
return await _repository.DeleteAsync(id, cancellationToken);
}
public async Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default)
{
return await _repository.DeleteWithStoredProcedureAsync(id, cancellationToken);
}
}

View File

@@ -5,8 +5,6 @@ public interface ICatalogService
Task<List<CatalogReadDto>> GetAllAsync(CancellationToken cancellationToken = default);
Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
Task<CatalogReadDto> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default);
Task<bool> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default);
Task<CatalogReadDto?> UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default);
Task<CatalogReadDto?> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default);
Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default);
Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default);
}

View File

@@ -6,9 +6,7 @@ public interface ICatalogRepository
{
Task<List<VwmyCatalog>> GetAllAsync(CancellationToken cancellationToken = default);
Task<VwmyCatalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
Task<VwmyCatalog> AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default);
Task<bool> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default);
Task<VwmyCatalog?> UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default);
Task<VwmyCatalog> InsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default);
Task<VwmyCatalog?> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default);
Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default);
Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default);
}

View File

@@ -25,70 +25,23 @@ 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);
if (created == null)
{
throw new InvalidOperationException("Failed to create catalog via stored procedure.");
}
return created;
// Platzhalter: Insert-Prozedur folgt.
// TODO: Replace with dedicated insert stored procedure invocation.
throw new NotImplementedException("Insert stored procedure not implemented yet.");
}
public async Task<bool> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)
{
var existing = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
if (!existing)
{
return false;
}
catalog.Guid = id;
var updated = await UpsertWithStoredProcedureAsync(catalog, cancellationToken);
return updated != null;
}
public async Task<VwmyCatalog?> UpdateWithStoredProcedureAsync(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);
}
public async Task<bool> DeleteAsync(int id, 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 false;
return null;
}
var guidParam = new SqlParameter("@GUID", id);
await _db.Database.ExecuteSqlRawAsync(
"EXEC dbo.PRTBMY_CATALOG_DELETE @GUID",
parameters: new[] { guidParam },
cancellationToken: cancellationToken);
catalog.Guid = id;
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)
{
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
{
Direction = ParameterDirection.InputOutput,
@@ -112,4 +65,21 @@ public class CatalogRepository : ICatalogRepository
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)
{
var exists = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
if (!exists)
{
return false;
}
var guidParam = new SqlParameter("@GUID", id);
await _db.Database.ExecuteSqlRawAsync(
"EXEC dbo.PRTBMY_CATALOG_DELETE @GUID",
parameters: new[] { guidParam },
cancellationToken: cancellationToken);
return true;
}
}