diff --git a/DbFirst.API/Controllers/CatalogsController.cs b/DbFirst.API/Controllers/CatalogsController.cs index 1aaf1e0..f220617 100644 --- a/DbFirst.API/Controllers/CatalogsController.cs +++ b/DbFirst.API/Controllers/CatalogsController.cs @@ -36,6 +36,10 @@ public class CatalogsController : ControllerBase public async Task> Create(CatalogWriteDto dto, CancellationToken cancellationToken) { var created = await _service.CreateAsync(dto, cancellationToken); + if (created == null) + { + return Conflict(); + } return CreatedAtAction(nameof(GetById), new { id = created.Guid }, created); } diff --git a/DbFirst.Application/Catalogs/CatalogService.cs b/DbFirst.Application/Catalogs/CatalogService.cs index c2263da..755162d 100644 --- a/DbFirst.Application/Catalogs/CatalogService.cs +++ b/DbFirst.Application/Catalogs/CatalogService.cs @@ -27,8 +27,14 @@ public class CatalogService : ICatalogService return item == null ? null : _mapper.Map(item); } - public async Task CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default) + public async Task CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default) { + var existing = await _repository.GetByTitleAsync(dto.CatTitle, cancellationToken); + if (existing != null) + { + return null; + } + var entity = _mapper.Map(dto); entity.AddedWho = "system"; entity.AddedWhen = DateTime.UtcNow; @@ -49,6 +55,7 @@ public class CatalogService : ICatalogService var entity = _mapper.Map(dto); entity.Guid = id; + entity.CatTitle = existing.CatTitle; entity.AddedWho = existing.AddedWho; entity.AddedWhen = existing.AddedWhen; entity.ChangedWho = "system"; diff --git a/DbFirst.Application/Catalogs/ICatalogService.cs b/DbFirst.Application/Catalogs/ICatalogService.cs index f7bc50f..ba9dac6 100644 --- a/DbFirst.Application/Catalogs/ICatalogService.cs +++ b/DbFirst.Application/Catalogs/ICatalogService.cs @@ -4,7 +4,7 @@ public interface ICatalogService { Task> GetAllAsync(CancellationToken cancellationToken = default); Task GetByIdAsync(int id, CancellationToken cancellationToken = default); - Task CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default); + Task CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default); Task UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default); Task DeleteAsync(int id, CancellationToken cancellationToken = default); } diff --git a/DbFirst.Domain/Repositories/ICatalogRepository.cs b/DbFirst.Domain/Repositories/ICatalogRepository.cs index 7821577..6082ef7 100644 --- a/DbFirst.Domain/Repositories/ICatalogRepository.cs +++ b/DbFirst.Domain/Repositories/ICatalogRepository.cs @@ -6,6 +6,7 @@ public interface ICatalogRepository { Task> GetAllAsync(CancellationToken cancellationToken = default); Task GetByIdAsync(int id, CancellationToken cancellationToken = default); + Task GetByTitleAsync(string title, CancellationToken cancellationToken = default); Task InsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default); Task UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default); Task DeleteAsync(int id, CancellationToken cancellationToken = default); diff --git a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs index 37e8d0a..beffe78 100644 --- a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs +++ b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs @@ -25,6 +25,11 @@ public class CatalogRepository : ICatalogRepository return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken); } + public async Task GetByTitleAsync(string title, CancellationToken cancellationToken = default) + { + return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.CatTitle == title, cancellationToken); + } + public async Task InsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default) { var guidParam = new SqlParameter("@GUID", SqlDbType.Int)