Exemplo n.º 1
0
        public MakePaymentResult MakePayment(MakePaymentRequest request)
        {
            Account    debtorAccount = _accountAccess.GetAccount(request.DebtorAccountNumber);
            IValidator validator     = _validationFactory.GetValidator(request.PaymentScheme);
            bool       valid         = validator.ValidatePayment(debtorAccount, request.Amount);

            if (valid)
            {
                debtorAccount.Balance -= request.Amount;
                _accountAccess.UpdateAccount(debtorAccount);
            }

            return(new MakePaymentResult {
                Success = valid
            });
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public async Task HandleEvent(PropertyDefinitionUpdatedEvent eventData, CancellationToken ct)
        {
            var definition = _dynamicMetadataDefinition.GetById(eventData.DefinitionId);
            var allMovies  = _movieReadService.GetAll();
            var validator  = _validationFactory.GetValidator(definition);

            var propertyInDefinition = definition.Properties.Single(x => x.Id == eventData.UpdatedPropertyKey);

            foreach (var movie in allMovies)
            {
                var matchedMetadatas = movie.Metadata.Where(x => x.DefinitionId == eventData.DefinitionId).ToList();

                if (matchedMetadatas.Any())
                {
                    foreach (var matchedMetadata in matchedMetadatas)
                    {
                        var property = matchedMetadata.Properties.Single(x => x.Id == eventData.UpdatedPropertyKey);

                        bool alreadySetStatus = false;
                        if (property.HasDefaultValue)
                        {
                            var copy = _mapper.Map <DynamicMetadataProperty>(propertyInDefinition);
                            copy.HasDefaultValue = true;

                            matchedMetadata.Properties.Remove(property);
                            matchedMetadata.Properties.Add(copy);
                        }
                        else if (property.Type != propertyInDefinition.Type)
                        {
                            property.State   = PropertyState.UsesOldType;
                            alreadySetStatus = true;
                        }

                        var validationErrors = validator.Validate(matchedMetadata, movie);

                        if (validationErrors.Any() && !alreadySetStatus)
                        {
                            property.State = PropertyState.Invalidated;
                        }
                    }

                    _movieWriteService.UpdateItem(movie, ct);
                }
            }
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public async Task <MetadataDefinitionDto> HandleResult(RemoveMovieMetadataCommand command, CancellationToken token)
        {
            var definition = _dynamicMetadataDefinitonService.GetById(command.MetadataDefinitionId);

            if (definition == null)
            {
                throw new NotFoundException();
            }

            var existingProperty = definition.Properties.SingleOrDefault(x => x.Id == command.MetadataKey);

            if (existingProperty == null)
            {
                throw new NotFoundException();
            }

            var validator = _validationFactory.GetValidator(definition);

            if (validator.WillUseProperty(existingProperty.Id))
            {
                throw new BadRequestException("Property is used by validator")
                      {
                          ErrorCode = 11,
                      };
            }

            definition.Properties.Remove(existingProperty);
            _dynamicMetadataDefinitionWriteService.UpdateItem(definition, token);

            await _eventDispatcher.DispatchEvent(new PropertyRemovedFromMetadataDefinitionEvent
            {
                RemovedPropertyKey = command.MetadataKey,
                DefinitionId       = command.MetadataDefinitionId
            });

            return(_mapper.Map <MetadataDefinitionDto>(definition));
        }
        public async Task HandleEvent(RefreshValidationEvent eventData, CancellationToken ct)
        {
            var allMovies          = _movieReadService.GetAll();
            var metadataDefinition = _metadataReadService.GetById(eventData.DefinitionId);
            var validators         = _validationFactory.GetValidator(metadataDefinition);

            foreach (var movie in allMovies)
            {
                var updatedDefinitions = movie.Metadata.Where(x => x.DefinitionId == metadataDefinition.Id).ToList();

                if (!updatedDefinitions.Any())
                {
                    continue;
                }

                bool isValidMovie = true;
                foreach (var definition in updatedDefinitions)
                {
                    var propertyValidationErrors = validators.Validate(definition, movie);

                    if (propertyValidationErrors.Any())
                    {
                        definition.IsValid = false;
                        isValidMovie       = false;
                    }

                    if (!propertyValidationErrors.Any())
                    {
                        definition.IsValid = true;
                    }
                }

                movie.IsValid = isValidMovie;

                _movieWriteService.UpdateItem(movie, ct);
            }
        }
        /// <inheritdoc />
        public async Task <DynamicMetadataDto> HandleResult(UpdateMovieMetadataCommand command, CancellationToken token)
        {
            var movie = _movieReadService.GetById(command.MovieId);

            var metadata = movie.Metadata.FirstOrDefault(x => x.Id == command.MetdataId);

            var definition = _dynamicMetadataDefinitionReadService.GetById(metadata.DefinitionId);

            var validator = _validationFactory.GetValidator(definition);

            foreach (var metadataEntry in command.Metadata.DynamicProperties)
            {
                var propertyDefinition = definition.Properties.SingleOrDefault(x => x.Id == metadataEntry.Id);
                if (propertyDefinition != null)
                {
                    if (propertyDefinition.Type != metadataEntry.Type)
                    {
                        throw new AggregatedValidationException("Type mismatch")
                              {
                                  ErrorCode        = 11231,
                                  ValidationErrors = new List <Error>()
                              };
                    }

                    var existingMetadata = metadata.Properties.SingleOrDefault(x => x.Id == metadataEntry.Id);
                    if (existingMetadata != null)
                    {
                        try
                        {
                            var parsedValue = _inputDataParser.DeserializeItem(metadataEntry.Id, metadataEntry.Type, metadataEntry.Value);

                            parsedValue.HasDefaultValue = false;
                            metadata.Properties.Remove(existingMetadata);
                            metadata.Properties.Add(parsedValue);
                        }
                        catch (JsonException exception)
                        {
                            throw new AggregatedValidationException("Invalid type mismatch")
                                  {
                                      ErrorCode        = 11231,
                                      ValidationErrors = new List <Error>()
                                  };
                        }
                    }
                }
            }

            var validationResult = validator.Validate(metadata, movie);

            if (validationResult.Any())
            {
                throw new AggregatedValidationException("Validation exception")
                      {
                          ErrorCode        = 1,
                          ValidationErrors = validationResult
                      };
            }


            movie.Metadata.Add(metadata);

            _movieWriteService.UpdateItem(movie, token);

            var resultValue = _mapper.Map <DynamicMetadataDto>(metadata);

            return(resultValue);
        }