Refactored IRepository<T> and ICatalogRepository to reside in the DbFirst.Application layer instead of Domain. Updated namespaces, using statements, and all references in services and handlers. Adjusted csproj dependencies to reflect the new structure. Updated comments to clarify Clean Architecture rationale and improved separation of concerns.
43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using AutoMapper;
|
|
using DbFirst.Application.Repositories;
|
|
using DbFirst.Domain.Entities;
|
|
using DbFirst.Domain;
|
|
using MediatR;
|
|
|
|
namespace DbFirst.Application.Catalogs.Commands;
|
|
|
|
public class UpdateCatalogHandler : IRequestHandler<UpdateCatalogCommand, CatalogReadDto?>
|
|
{
|
|
private readonly ICatalogRepository _repository;
|
|
private readonly IMapper _mapper;
|
|
|
|
public UpdateCatalogHandler(ICatalogRepository repository, IMapper mapper)
|
|
{
|
|
_repository = repository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public async Task<CatalogReadDto?> Handle(UpdateCatalogCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var existing = await _repository.GetByIdAsync(request.Id, cancellationToken);
|
|
if (existing == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var entity = _mapper.Map<VwmyCatalog>(request.Dto);
|
|
entity.Guid = request.Id;
|
|
entity.CatTitle = request.Dto.UpdateProcedure == CatalogUpdateProcedure.Update
|
|
? existing.CatTitle
|
|
: request.Dto.CatTitle;
|
|
entity.AddedWho = existing.AddedWho;
|
|
entity.AddedWhen = existing.AddedWhen;
|
|
entity.ChangedWho = "system";
|
|
entity.ChangedWhen = DateTime.UtcNow;
|
|
|
|
var procedure = request.Dto.UpdateProcedure;
|
|
var updated = await _repository.UpdateAsync(request.Id, entity, procedure, cancellationToken);
|
|
return updated == null ? null : _mapper.Map<CatalogReadDto>(updated);
|
|
}
|
|
}
|