Ejemplo n.º 1
0
        public async Task <CommandResult> Handle(DeleteCostCenterCommand message)
        {
            //Validate using domain models
            CostCenter costCenterAux = new CostCenter();

            costCenterAux = _mapper.Map <DeleteCostCenterCommand, CostCenter>(message);

            //validate code uniqueness
            var existingCostCenter = await _costCenterRepository.GetCostCenter(message.CostCenterId);

            if (existingCostCenter == null)
            {
                costCenterAux.AddError("Cost Center Id not found.");
            }

            //if is not valid
            if (costCenterAux.HasErrors)
            {
                return(costCenterAux.ToResult());
            }

            _costCenterRepository.Delete(existingCostCenter);

            await _unitOfWork.CommitAsync();

            //Publish bussines Event
            await _bus.PublishAsync(new CostCenterDeleted()
            {
                CostCenterId = existingCostCenter.CostCenterId
            });

            //Return result
            return(existingCostCenter.ToResult());
        }