Files
DbFirst/DbFirst.Application/Catalogs/Commands/CreateCatalogHandler.cs
OlgunR 870b10779e Refactor Catalogs to use MediatR and CQRS pattern
Replaced direct service usage in CatalogsController with MediatR-based commands and queries for all CRUD operations. Added command/query and handler classes for Catalog operations. Updated dependency injection to register MediatR and removed ICatalogService. Improved code maintainability and testability by adopting CQRS architecture.
2026-01-19 09:00:06 +01:00

37 lines
1.1 KiB
C#

using AutoMapper;
using DbFirst.Domain.Entities;
using DbFirst.Domain.Repositories;
using MediatR;
namespace DbFirst.Application.Catalogs.Commands;
public class CreateCatalogHandler : IRequestHandler<CreateCatalogCommand, CatalogReadDto?>
{
private readonly ICatalogRepository _repository;
private readonly IMapper _mapper;
public CreateCatalogHandler(ICatalogRepository repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
public async Task<CatalogReadDto?> Handle(CreateCatalogCommand request, CancellationToken cancellationToken)
{
var existing = await _repository.GetByTitleAsync(request.Dto.CatTitle, cancellationToken);
if (existing != null)
{
return null;
}
var entity = _mapper.Map<VwmyCatalog>(request.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);
}
}