Compare commits
2 Commits
6caf3a4c07
...
facc376f74
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
facc376f74 | ||
|
|
a849a88fa3 |
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user