Added support for updating and deleting Catalog records via SQL Server stored procedures. Introduced PUT /catalogs/sp and DELETE /catalogs/sp/{id} endpoints in CatalogsController. Extended service and repository interfaces and implementations to handle stored procedure operations. Added Microsoft.Data.SqlClient package for direct SQL execution. Repository checks for record existence before SP calls to prevent unintended behavior.
60 lines
2.2 KiB
C#
60 lines
2.2 KiB
C#
using AutoMapper;
|
|
using DbFirst.Domain.DomainEntities;
|
|
using DbFirst.Domain.Repositories;
|
|
|
|
namespace DbFirst.Application.Catalogs;
|
|
|
|
public class CatalogService : ICatalogService
|
|
{
|
|
private readonly ICatalogRepository _repository;
|
|
private readonly IMapper _mapper;
|
|
|
|
public CatalogService(ICatalogRepository repository, IMapper mapper)
|
|
{
|
|
_repository = repository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public async Task<List<CatalogDto>> GetAllAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
var domainItems = await _repository.GetAllAsync(cancellationToken);
|
|
return _mapper.Map<List<CatalogDto>>(domainItems);
|
|
}
|
|
|
|
public async Task<CatalogDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
|
{
|
|
var domainItem = await _repository.GetByIdAsync(id, cancellationToken);
|
|
return domainItem == null ? null : _mapper.Map<CatalogDto>(domainItem);
|
|
}
|
|
|
|
public async Task<CatalogDto> CreateAsync(CatalogDto dto, CancellationToken cancellationToken = default)
|
|
{
|
|
var domainItem = _mapper.Map<Catalog>(dto);
|
|
var created = await _repository.AddAsync(domainItem, cancellationToken);
|
|
return _mapper.Map<CatalogDto>(created);
|
|
}
|
|
|
|
public async Task<bool> UpdateAsync(int id, CatalogDto dto, CancellationToken cancellationToken = default)
|
|
{
|
|
var domainItem = _mapper.Map<Catalog>(dto);
|
|
return await _repository.UpdateAsync(id, domainItem, cancellationToken);
|
|
}
|
|
|
|
public async Task<CatalogDto?> UpdateWithStoredProcedureAsync(CatalogDto dto, CancellationToken cancellationToken = default)
|
|
{
|
|
var domainItem = _mapper.Map<Catalog>(dto);
|
|
var updated = await _repository.UpdateWithStoredProcedureAsync(domainItem, cancellationToken);
|
|
return updated == null ? null : _mapper.Map<CatalogDto>(updated);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|