Removed Upsert endpoint and related service/repository methods. Deleted ExecuteUpsertAsync helper. Insert now throws NotImplementedException as a placeholder. Refactored Delete method in repository; logic unchanged.
66 lines
2.2 KiB
C#
66 lines
2.2 KiB
C#
using AutoMapper;
|
|
using DbFirst.Domain.Repositories;
|
|
using DbFirst.Domain.Entities;
|
|
|
|
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 items = await _repository.GetAllAsync(cancellationToken);
|
|
return _mapper.Map<List<CatalogReadDto>>(items);
|
|
}
|
|
|
|
public async Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
|
{
|
|
var item = await _repository.GetByIdAsync(id, cancellationToken);
|
|
return item == null ? null : _mapper.Map<CatalogReadDto>(item);
|
|
}
|
|
|
|
public async Task<CatalogReadDto> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default)
|
|
{
|
|
var entity = _mapper.Map<VwmyCatalog>(dto);
|
|
entity.AddedWho = "system";
|
|
entity.AddedWhen = DateTime.UtcNow;
|
|
entity.ChangedWho = "system";
|
|
entity.ChangedWhen = DateTime.UtcNow;
|
|
|
|
var created = await _repository.InsertAsync(entity, cancellationToken);
|
|
return _mapper.Map<CatalogReadDto>(created);
|
|
}
|
|
|
|
public async Task<CatalogReadDto?> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default)
|
|
{
|
|
var existing = await _repository.GetByIdAsync(id, cancellationToken);
|
|
if (existing == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var entity = _mapper.Map<VwmyCatalog>(dto);
|
|
entity.Guid = id;
|
|
entity.AddedWho = existing.AddedWho;
|
|
entity.AddedWhen = existing.AddedWhen;
|
|
entity.ChangedWho = "system";
|
|
entity.ChangedWhen = DateTime.UtcNow;
|
|
|
|
var updated = await _repository.UpdateAsync(id, entity, 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);
|
|
}
|
|
}
|