Removed direct usage of TbmyCatalog entity and DbSet. All create, update, and delete operations now use stored procedures. Entity lookups and mappings are performed via the VwmyCatalog view. Updated AutoMapper profile and DbContext configuration accordingly. Catalog creation now sets ChangedWho/ChangedWhen to "system" and current UTC time.
88 lines
3.2 KiB
C#
88 lines
3.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<CatalogReadDto>> GetAllAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
var domainItems = await _repository.GetAllAsync(cancellationToken);
|
|
return _mapper.Map<List<CatalogReadDto>>(domainItems);
|
|
}
|
|
|
|
public async Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
|
{
|
|
var domainItem = await _repository.GetByIdAsync(id, cancellationToken);
|
|
return domainItem == null ? null : _mapper.Map<CatalogReadDto>(domainItem);
|
|
}
|
|
|
|
public async Task<CatalogReadDto> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default)
|
|
{
|
|
var domainItem = _mapper.Map<Catalog>(dto);
|
|
domainItem.AddedWho = "system";
|
|
domainItem.AddedWhen = DateTime.UtcNow;
|
|
domainItem.ChangedWho = "system";
|
|
domainItem.ChangedWhen = DateTime.UtcNow;
|
|
|
|
var created = await _repository.AddAsync(domainItem, 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 domainItem = _mapper.Map<Catalog>(dto);
|
|
domainItem.Guid = id;
|
|
domainItem.AddedWho = existing.AddedWho;
|
|
domainItem.AddedWhen = existing.AddedWhen;
|
|
domainItem.ChangedWho = "system";
|
|
domainItem.ChangedWhen = DateTime.UtcNow;
|
|
return await _repository.UpdateAsync(id, domainItem, cancellationToken);
|
|
}
|
|
|
|
public async Task<CatalogReadDto?> UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default)
|
|
{
|
|
var existing = await _repository.GetByIdAsync(id, cancellationToken);
|
|
if (existing == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var domainItem = _mapper.Map<Catalog>(dto);
|
|
domainItem.Guid = id;
|
|
domainItem.AddedWho = existing.AddedWho;
|
|
domainItem.AddedWhen = existing.AddedWhen;
|
|
domainItem.ChangedWho = "system";
|
|
domainItem.ChangedWhen = DateTime.UtcNow;
|
|
|
|
var updated = await _repository.UpdateWithStoredProcedureAsync(domainItem, cancellationToken);
|
|
return updated == null ? null : _mapper.Map<CatalogReadDto>(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);
|
|
}
|
|
}
|