public async Task <Unit> Handle(RemoveDistributorAreasCommand request, CancellationToken cancellationToken) { var distributorFromRepo = await _distributorRepository.FindByIdAsync(request.DistributorId); if (distributorFromRepo == null) { throw new DistributorNotFoundException(request.DistributorId); } foreach (var areaId in request.AreasIds) { var area = await _distributorRepository.FindAreaById(areaId); if (area == null) { throw new AreaNotFoundException(areaId); } distributorFromRepo.RemoveArea(area); } _distributorRepository.Update(distributorFromRepo); await _distributorRepository.UnitOfWork.SaveEntitiesSeveralTransactionsAsync(cancellationToken); return(Unit.Value); }
public async Task <string> Handle(CreateDistributorCommand request, CancellationToken cancellationToken) { var distributor = new Distributor(request.Name); foreach (var areaId in request.AreasIds) { var area = await _distributorRepository.FindAreaById(areaId); if (area == null) { throw new AreaNotFoundException(areaId); } distributor.AddArea(area); } _distributorRepository.Add(distributor); await _distributorRepository.UnitOfWork.SaveEntitiesAsync(); return(distributor.Id.ToString()); }