コード例 #1
0
        public override async Task ValidateForAdding(TransactionDto dto)
        {
            ValidateNotDefaultValue(dto.Recipient.Id, nameof(dto.Recipient));
            ValidateNotDefaultValue(dto.Category.Id, nameof(dto.Category));
            ValidateNotDefaultValue(dto.Currency.Id, nameof(dto.Currency));
            ValidateNotDefaultValue(dto.OccuredOn, nameof(dto.OccuredOn));
            ValidateNotDefaultValue(dto.Amount, nameof(dto.Amount));
            ValidateValueNotNullOrWhitespace(dto.Title, nameof(dto.Title));

            if (dto.Amount < 0)
            {
                throw new BusinessException($"Amount should be greater than zero.");
            }

            var categoryExists = await _categoryRepository.Exists(dto.Category.Id);

            if (!categoryExists)
            {
                throw new BusinessException($"Category with ID {dto.Category.Id} does not exists.");
            }

            var recipientExists = await _recipientRepository.Exists(dto.Recipient.Id);

            if (!recipientExists)
            {
                throw new BusinessException($"Recipient with ID {dto.Recipient.Id} does not exists.");
            }

            var currencyExists = await _currencyRepository.Exists(dto.Currency.Id);

            if (!currencyExists)
            {
                throw new BusinessException($"Currency with ID {dto.Currency.Id} does not exists.");
            }
        }
コード例 #2
0
        public void Handle(AddRecipientsSelections command)
        {
            List <Recipient> recipients = new List <Recipient>();
            List <Selection> selections = new List <Selection>();

            _validator.ToList().ForEach(x => x.Validate(command));
            foreach (var recipient in command.RecipientEmails)
            {
                if (!_recipientRepository.Exists(recipient))
                {
                    var newRecipient = new AddRecipient(recipient);
                    _commandHandlerDispatcher.Execute(newRecipient);
                    _unitOfWork.CommitChanges();
                }
                recipients.Add(_recipientRepository.Get(recipient));
            }

            foreach (var selection in command.SheetSelections)
            {
                var sheetName     = RegexUtils.GetSheetNameFromSelectionString(selection);
                var selectionName = RegexUtils.GetSelectionNameFromSelectionString(selection);
                if (!_selectionRepository.Exists(sheetName, selectionName))
                {
                    var newSelection = new AddSelection(sheetName, selectionName);
                    _commandHandlerDispatcher.Execute(newSelection);
                    _unitOfWork.CommitChanges();
                }
                selections.Add(_selectionRepository.Get(sheetName, selectionName));
            }

            foreach (var recipientEntity in recipients)
            {
                foreach (var selectionEntity in selections)
                {
                    if (!_sharingRepository.Exists(recipientEntity.ID, selectionEntity.ID))
                    {
                        var newSharing = new AddSharing(recipientEntity.ID, selectionEntity.ID);
                        _commandHandlerDispatcher.Execute(newSharing);
                        _unitOfWork.CommitChanges();
                    }
                }
            }
        }