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:
@@ -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();
|
||||
@@ -61,6 +50,17 @@ public class CatalogsController : ControllerBase
|
||||
return Ok(updated);
|
||||
}
|
||||
|
||||
[HttpPut("upsert")]
|
||||
public async Task<ActionResult<CatalogReadDto>> Upsert(CatalogWriteDto dto, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await _service.UpsertAsync(dto, cancellationToken);
|
||||
if (result == null)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<IActionResult> Delete(int id, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -71,15 +71,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,17 +54,27 @@ 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);
|
||||
}
|
||||
|
||||
public async Task<CatalogReadDto?> UpsertAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = _mapper.Map<VwmyCatalog>(dto);
|
||||
if (entity.Guid == 0)
|
||||
{
|
||||
entity.AddedWho = "system";
|
||||
entity.AddedWhen = DateTime.UtcNow;
|
||||
}
|
||||
entity.ChangedWho = "system";
|
||||
entity.ChangedWhen = DateTime.UtcNow;
|
||||
|
||||
var upserted = await _repository.UpsertAsync(entity, cancellationToken);
|
||||
return upserted == null ? null : _mapper.Map<CatalogReadDto>(upserted);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
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,7 @@ 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<CatalogReadDto?> UpsertAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default);
|
||||
Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default);
|
||||
Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -6,9 +6,8 @@ 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<VwmyCatalog?> UpsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default);
|
||||
Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default);
|
||||
Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user