示例#1
0
        public RegisterOdsInstanceModelValidator(AdminAppDbContext database
                                                 , ICloudOdsAdminAppSettingsApiModeProvider apiModeProvider
                                                 , IDatabaseValidationService databaseValidationService
                                                 , IDatabaseConnectionProvider databaseConnectionProvider
                                                 , bool validationMessageWithDetails = false)
        {
            _database = database;
            _databaseValidationService  = databaseValidationService;
            _databaseConnectionProvider = databaseConnectionProvider;
            var requiredNumericSuffixMessage = "'ODS Instance District / EdOrg Id' must not be empty.";
            var inRangeMessage        = "'ODS Instance District / EdOrg Id' must be a valid positive integer.";
            var maxValue              = int.MaxValue;
            var minValue              = 1;
            var beUniqueValidationMsg = "Education Organization / District Id";

            _mode = apiModeProvider.GetApiMode();

            if (_mode == ApiMode.YearSpecific)
            {
                requiredNumericSuffixMessage = "'ODS Instance School Year' must not be empty.";
                maxValue              = 2099;
                inRangeMessage        = "'ODS Instance School Year' must be between 1900 and 2099.";
                beUniqueValidationMsg = "school year";
                minValue              = 1900;
            }

            RuleFor(m => m.NumericSuffix).NotEmpty().WithMessage(requiredNumericSuffixMessage);

            RuleFor(m => m.NumericSuffix)
            .Must(BeWithinApplicationNameMaxLength)
            .When(x => x.NumericSuffix != null);

            RuleFor(m => m.NumericSuffix)
            .Must(x => x <= maxValue && x >= minValue)
            .When(x => x.NumericSuffix != null)
            .WithMessage(inRangeMessage);

            RuleFor(m => m.NumericSuffix)
            .Must(BeAUniqueInstanceName)
            .When(x => x.NumericSuffix != null)
            .WithMessage(
                x => $"An instance for this {beUniqueValidationMsg}{(validationMessageWithDetails ? $"({x.NumericSuffix})":"")} already exists.");

            RuleFor(m => m.NumericSuffix)
            .Must(BeValidOdsInstanceDatabase)
            .When(x => x.NumericSuffix != null)
            .WithMessage(
                x => $"Could not connect to an ODS instance database for this {beUniqueValidationMsg}{(validationMessageWithDetails ? $"({x.NumericSuffix})":"")}.");

            RuleFor(m => m.Description).NotEmpty();

            RuleFor(m => m.Description)
            .Must(BeAUniqueInstanceDescription)
            .When(x => x.Description != null)
            .WithMessage(
                x => $"An instance with this description{(validationMessageWithDetails ? $"({beUniqueValidationMsg}: {x.NumericSuffix}, Description: {x.Description})":"")} already exists.");
        }
        public BulkRegisterOdsInstancesModelValidator(AdminAppDbContext database
                                                      , ICloudOdsAdminAppSettingsApiModeProvider apiModeProvider
                                                      , IDatabaseValidationService databaseValidationService
                                                      , IDatabaseConnectionProvider databaseConnectionProvider
                                                      , IBulkRegisterOdsInstancesFiltrationService dataFilterService)
        {
            var mode = apiModeProvider.GetApiMode();

            RuleFor(m => m.OdsInstancesFile)
            .NotEmpty();

            RuleFor(m => m.OdsInstancesFile.FileName).NotNull().Must(x => x.ToLower().EndsWith(".csv"))
            .WithMessage("Please select a file with .csv format.");

            When(m => m.OdsInstancesFile != null, () =>
            {
                RuleFor(m => m).SafeCustom(HaveValidHeaders);
            });

            When(m => m.OdsInstancesFile != null && !ValidHeadersRuleFailed, () =>
            {
                RuleFor(m => m).SafeCustom(HaveUniqueRecords);
            });

            When(
                m => m.OdsInstancesFile != null && !UniquenessRuleFailed && !ValidHeadersRuleFailed, () =>
            {
                RuleFor(x => x)
                .SafeCustom(
                    (model, context) =>
                {
                    var validator = new RegisterOdsInstanceModelValidator(
                        database, apiModeProvider, databaseValidationService,
                        databaseConnectionProvider, true);

                    var newOdsInstancesToRegister = dataFilterService.FilteredRecords(model.DataRecords(), mode).ToList();
                    model.FilteredDataRecords     = newOdsInstancesToRegister;

                    foreach (var record in newOdsInstancesToRegister)
                    {
                        var data    = (RegisterOdsInstanceModel)record;
                        var results = validator.Validate(data);
                        if (!results.IsValid)
                        {
                            foreach (var failure in results.Errors)
                            {
                                _logger.Error($"Property: {failure.PropertyName} failed validation. Error: {failure.ErrorMessage}");
                            }
                        }
                        context.AddFailures(results);
                    }
                });
            });
        }
示例#3
0
        public BulkRegisterOdsInstancesModelValidator(AdminAppDbContext database
                                                      , ICloudOdsAdminAppSettingsApiModeProvider apiModeProvider
                                                      , IDatabaseValidationService databaseValidationService
                                                      , IDatabaseConnectionProvider databaseConnectionProvider)
        {
            RuleFor(m => m.OdsInstancesFile)
            .NotEmpty();

            RuleFor(m => m.OdsInstancesFile)
            .Must(HaveUniqueRecords)
            .When(m => m.OdsInstancesFile != null);

            When(
                m => m.OdsInstancesFile != null && !UniquenessRuleFailed, () =>
            {
                RuleFor(x => x.OdsInstancesFile)
                .SafeCustom(
                    (model, context) =>
                {
                    var validator = new RegisterOdsInstanceModelValidator(
                        database, apiModeProvider, databaseValidationService,
                        databaseConnectionProvider, true);

                    foreach (var record in model.DataRecords())
                    {
                        var results = validator.Validate(record);
                        if (!results.IsValid)
                        {
                            foreach (var failure in results.Errors)
                            {
                                _logger.Error($"Property: {failure.PropertyName} failed validation. Error: {failure.ErrorMessage}");
                            }
                        }
                        context.AddFailures(results);
                    }
                });
            });
        }