diff --git a/DbFirst.API/Controllers/CatalogsController.cs b/DbFirst.API/Controllers/CatalogsController.cs index db4b1c5..5824671 100644 --- a/DbFirst.API/Controllers/CatalogsController.cs +++ b/DbFirst.API/Controllers/CatalogsController.cs @@ -40,20 +40,9 @@ public class CatalogsController : ControllerBase } [HttpPut("{id:int}")] - public async Task Update(int id, CatalogWriteDto dto, CancellationToken cancellationToken) + public async Task> 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> 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> 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 Delete(int id, CancellationToken cancellationToken) { @@ -71,15 +71,4 @@ public class CatalogsController : ControllerBase } return NoContent(); } - - [HttpDelete("sp/{id:int}")] - public async Task DeleteWithStoredProcedure(int id, CancellationToken cancellationToken) - { - var deleted = await _service.DeleteWithStoredProcedureAsync(id, cancellationToken); - if (!deleted) - { - return NotFound(); - } - return NoContent(); - } } diff --git a/DbFirst.Application/Catalogs/CatalogService.cs b/DbFirst.Application/Catalogs/CatalogService.cs index ad28e69..ff4f7eb 100644 --- a/DbFirst.Application/Catalogs/CatalogService.cs +++ b/DbFirst.Application/Catalogs/CatalogService.cs @@ -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(created); } - public async Task UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default) - { - var existing = await _repository.GetByIdAsync(id, cancellationToken); - if (existing == null) - { - return false; - } - - var entity = _mapper.Map(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 UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default) + public async Task 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(updated); } + public async Task UpsertAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default) + { + var entity = _mapper.Map(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(upserted); + } + public async Task DeleteAsync(int id, CancellationToken cancellationToken = default) { return await _repository.DeleteAsync(id, cancellationToken); } - - public async Task DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default) - { - return await _repository.DeleteWithStoredProcedureAsync(id, cancellationToken); - } } diff --git a/DbFirst.Application/Catalogs/ICatalogService.cs b/DbFirst.Application/Catalogs/ICatalogService.cs index 2635133..99e16b2 100644 --- a/DbFirst.Application/Catalogs/ICatalogService.cs +++ b/DbFirst.Application/Catalogs/ICatalogService.cs @@ -5,8 +5,7 @@ public interface ICatalogService Task> GetAllAsync(CancellationToken cancellationToken = default); Task GetByIdAsync(int id, CancellationToken cancellationToken = default); Task CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default); - Task UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default); - Task UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default); + Task UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default); + Task UpsertAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default); Task DeleteAsync(int id, CancellationToken cancellationToken = default); - Task DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default); } diff --git a/DbFirst.Domain/Repositories/ICatalogRepository.cs b/DbFirst.Domain/Repositories/ICatalogRepository.cs index 922d7c0..e098979 100644 --- a/DbFirst.Domain/Repositories/ICatalogRepository.cs +++ b/DbFirst.Domain/Repositories/ICatalogRepository.cs @@ -6,9 +6,8 @@ public interface ICatalogRepository { Task> GetAllAsync(CancellationToken cancellationToken = default); Task GetByIdAsync(int id, CancellationToken cancellationToken = default); - Task AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default); - Task UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default); - Task UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default); + Task InsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default); + Task UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default); + Task UpsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default); Task DeleteAsync(int id, CancellationToken cancellationToken = default); - Task DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default); } diff --git a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs index c304db4..d84d727 100644 --- a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs +++ b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs @@ -25,44 +25,33 @@ public class CatalogRepository : ICatalogRepository return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken); } - public async Task AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default) + public async Task 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 UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default) + public async Task 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 UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default) + public async Task 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 DeleteAsync(int id, CancellationToken cancellationToken = default) @@ -82,12 +71,7 @@ public class CatalogRepository : ICatalogRepository return true; } - public async Task DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default) - { - return await DeleteAsync(id, cancellationToken); - } - - private async Task UpsertWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken) + private async Task ExecuteUpsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken) { var guidParam = new SqlParameter("@GUID", SqlDbType.Int) {