Enforce unique catalog titles on creation
Added a uniqueness check for catalog titles in the creation flow. The service now prevents creating catalogs with duplicate titles by checking for existing entries before insertion. If a duplicate is detected, the API returns a 409 Conflict response. Updated interfaces and repository to support title-based lookups.
This commit is contained in:
@@ -36,6 +36,10 @@ public class CatalogsController : ControllerBase
|
|||||||
public async Task<ActionResult<CatalogReadDto>> Create(CatalogWriteDto dto, CancellationToken cancellationToken)
|
public async Task<ActionResult<CatalogReadDto>> Create(CatalogWriteDto dto, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var created = await _service.CreateAsync(dto, cancellationToken);
|
var created = await _service.CreateAsync(dto, cancellationToken);
|
||||||
|
if (created == null)
|
||||||
|
{
|
||||||
|
return Conflict();
|
||||||
|
}
|
||||||
return CreatedAtAction(nameof(GetById), new { id = created.Guid }, created);
|
return CreatedAtAction(nameof(GetById), new { id = created.Guid }, created);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,8 +27,14 @@ public class CatalogService : ICatalogService
|
|||||||
return item == null ? null : _mapper.Map<CatalogReadDto>(item);
|
return item == null ? null : _mapper.Map<CatalogReadDto>(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<CatalogReadDto> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default)
|
public async Task<CatalogReadDto?> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
var existing = await _repository.GetByTitleAsync(dto.CatTitle, cancellationToken);
|
||||||
|
if (existing != null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
var entity = _mapper.Map<VwmyCatalog>(dto);
|
var entity = _mapper.Map<VwmyCatalog>(dto);
|
||||||
entity.AddedWho = "system";
|
entity.AddedWho = "system";
|
||||||
entity.AddedWhen = DateTime.UtcNow;
|
entity.AddedWhen = DateTime.UtcNow;
|
||||||
@@ -49,6 +55,7 @@ public class CatalogService : ICatalogService
|
|||||||
|
|
||||||
var entity = _mapper.Map<VwmyCatalog>(dto);
|
var entity = _mapper.Map<VwmyCatalog>(dto);
|
||||||
entity.Guid = id;
|
entity.Guid = id;
|
||||||
|
entity.CatTitle = existing.CatTitle;
|
||||||
entity.AddedWho = existing.AddedWho;
|
entity.AddedWho = existing.AddedWho;
|
||||||
entity.AddedWhen = existing.AddedWhen;
|
entity.AddedWhen = existing.AddedWhen;
|
||||||
entity.ChangedWho = "system";
|
entity.ChangedWho = "system";
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ public interface ICatalogService
|
|||||||
{
|
{
|
||||||
Task<List<CatalogReadDto>> GetAllAsync(CancellationToken cancellationToken = default);
|
Task<List<CatalogReadDto>> GetAllAsync(CancellationToken cancellationToken = default);
|
||||||
Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
||||||
Task<CatalogReadDto> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default);
|
Task<CatalogReadDto?> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default);
|
||||||
Task<CatalogReadDto?> UpdateAsync(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> DeleteAsync(int id, CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ public interface ICatalogRepository
|
|||||||
{
|
{
|
||||||
Task<List<VwmyCatalog>> GetAllAsync(CancellationToken cancellationToken = default);
|
Task<List<VwmyCatalog>> GetAllAsync(CancellationToken cancellationToken = default);
|
||||||
Task<VwmyCatalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
Task<VwmyCatalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
||||||
|
Task<VwmyCatalog?> GetByTitleAsync(string title, CancellationToken cancellationToken = default);
|
||||||
Task<VwmyCatalog> InsertAsync(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?> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default);
|
||||||
Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default);
|
Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default);
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ public class CatalogRepository : ICatalogRepository
|
|||||||
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<VwmyCatalog?> GetByTitleAsync(string title, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.CatTitle == title, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<VwmyCatalog> InsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
public async Task<VwmyCatalog> InsertAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
|
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
|
||||||
|
|||||||
Reference in New Issue
Block a user