Пример #1
0
        private IEnumerable <ScholarshipSchool> GetScholarshipSchoolsFromFile(string contentRootPath, ScholarshipContext context, ILogger <ScholarshipContextSeed> logger)
        {
            string csvFileScholarshipSchools = Path.Combine(contentRootPath, "Setup", "ScholarshipSchools.csv");

            if (!File.Exists(csvFileScholarshipSchools))
            {
                return(GetPreconfiguredScholarshipSchools());
            }

            string[] csvheaders;
            try
            {
                string[] requiredHeaders = { "scholarshipschool", "name", "phonenumber", "emailaddress" };
                csvheaders = GetHeaders(csvFileScholarshipSchools, requiredHeaders);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message);
                return(GetPreconfiguredScholarshipSchools());
            }

            var scholarshipLocationIdLookup = context.ScholarshipLocations.ToDictionary(ct => ct.Location, ct => ct.Id);

            return(File.ReadAllLines(csvFileScholarshipSchools)
                   .Skip(1)      // skip header row
                   .Select(row => Regex.Split(row, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"))
                   .SelectTry(column => CreateScholarshipSchool(column, csvheaders, scholarshipLocationIdLookup))
                   .OnCaughtException(ex => { logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message); return null; })
                   .Where(x => x != null));
        }
Пример #2
0
        private IEnumerable <ScholarshipFeeStructure> GetScholarshipFeeStructuresFromFile(string contentRootPath, ScholarshipContext context, ILogger <ScholarshipContextSeed> logger)
        {
            string csvFileScholarshipFeeStructures = Path.Combine(contentRootPath, "Setup", "ScholarshipFeeStructures.csv");

            if (!File.Exists(csvFileScholarshipFeeStructures))
            {
                return(GetPreconfiguredScholarshipFeeStructures());
            }

            string[] csvheaders;
            try
            {
                string[] requiredHeaders = { "name", "fee", "course", "currency", "duration", "educationlevel", "school" };
                csvheaders = GetHeaders(csvFileScholarshipFeeStructures, requiredHeaders);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message);
                return(GetPreconfiguredScholarshipFeeStructures());
            }

            var scholarshipCurrencyIdLookup       = context.ScholarshipCurrencies.ToDictionary(ct => ct.Code, ct => ct.Id);
            var scholarshipDurationIdLookup       = context.ScholarshipDurations.ToDictionary(ct => ct.Duration, ct => ct.Id);
            var scholarshipEducationLevelIdLookup = context.ScholarshipEducationLevels.ToDictionary(ct => ct.EducationLevel, ct => ct.Id);
            var scholarshipCourseIdLookup         = context.ScholarshipCourses.ToDictionary(ct => ct.Name, ct => ct.Id);
            var scholarshipSchoolIdLookup         = context.ScholarshipSchools.ToDictionary(ct => ct.Name, ct => ct.Id);

            return(File.ReadAllLines(csvFileScholarshipFeeStructures)
                   .Skip(1)      // skip header row
                   .Select(row => Regex.Split(row, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"))
                   .SelectTry(column => CreateScholarshipFeeStructure(column, csvheaders, scholarshipCurrencyIdLookup, scholarshipDurationIdLookup, scholarshipEducationLevelIdLookup, scholarshipCourseIdLookup, scholarshipSchoolIdLookup))
                   .OnCaughtException(ex => { logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message); return null; })
                   .Where(x => x != null));
        }
Пример #3
0
        private IEnumerable <ScholarshipItem> GetScholarshipItemsFromFile(string contentRootPath, ScholarshipContext context, ILogger <ScholarshipContextSeed> logger)
        {
            string csvFileScholarshipItems = Path.Combine(contentRootPath, "Setup", "ScholarshipItems.csv");

            if (!File.Exists(csvFileScholarshipItems))
            {
                return(GetPreconfiguredItems());
            }

            string[] csvheaders;
            try
            {
                string[] requiredHeaders = { "currencysymbol", "duration", "educationlevel", "interest", "location", "description", "name", "amount", "picturefilename" };
                string[] optionalheaders = { "availableslots", "reslotthreshold", "maxslotthreshold", "onreapply" };
                csvheaders = GetHeaders(csvFileScholarshipItems, requiredHeaders, optionalheaders);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message);
                return(GetPreconfiguredItems());
            }

            var scholarshipCurrencyIdLookup       = context.ScholarshipCurrencies.ToDictionary(ct => ct.Currency, ct => ct.Id);
            var scholarshipDurationIdLookup       = context.ScholarshipDurations.ToDictionary(ct => ct.Duration, ct => ct.Id);
            var scholarshipEducationlevelIdLookup = context.ScholarshipEducationLevels.ToDictionary(ct => ct.EducationLevel, ct => ct.Id);
            var scholarshipInterestIdLookup       = context.ScholarshipInterests.ToDictionary(ct => ct.Interest, ct => ct.Id);
            var scholarshipLocationIdLookup       = context.ScholarshipLocations.ToDictionary(ct => ct.Location, ct => ct.Id);

            return(File.ReadAllLines(csvFileScholarshipItems)
                   .Skip(1)      // skip header row
                   .Select(row => Regex.Split(row, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"))
                   .SelectTry(column => CreateScholarshipItem(column, csvheaders, scholarshipCurrencyIdLookup, scholarshipDurationIdLookup, scholarshipEducationlevelIdLookup, scholarshipInterestIdLookup, scholarshipLocationIdLookup))
                   .OnCaughtException(ex => { logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message); return null; })
                   .Where(x => x != null));
        }
Пример #4
0
        public async Task SeedAsync(ScholarshipContext context, IWebHostEnvironment env, IOptions <ScholarshipSettings> settings, ILogger <ScholarshipContextSeed> logger)
        {
            var policy = CreatePolicy(logger, nameof(ScholarshipContextSeed));

            await policy.ExecuteAsync(async() =>
            {
                var useCustomizationData = settings.Value.UseCustomizationData;
                var contentRootPath      = env.ContentRootPath;
                var picturePath          = env.WebRootPath;

                if (!context.ScholarshipCurrencies.Any())
                {
                    await context.ScholarshipCurrencies.AddRangeAsync(useCustomizationData
                        ? GetScholarshipCurrenciesFromFile(contentRootPath, logger)
                        : GetPreconfiguredScholarshipCurrencies());

                    await context.SaveChangesAsync();
                }

                if (!context.ScholarshipDurations.Any())
                {
                    await context.ScholarshipDurations.AddRangeAsync(useCustomizationData
                        ? GetScholarshipDurationsFromFile(contentRootPath, logger)
                        : GetPreconfiguredScholarshipDurations());

                    await context.SaveChangesAsync();
                }

                if (!context.ScholarshipCourses.Any())
                {
                    await context.ScholarshipCourses.AddRangeAsync(useCustomizationData
                        ? GetScholarshipCoursesFromFile(contentRootPath, context, logger)
                        : GetPreconfiguredScholarshipCourses());

                    await context.SaveChangesAsync();
                }

                if (!context.ScholarshipEducationLevels.Any())
                {
                    await context.ScholarshipEducationLevels.AddRangeAsync(useCustomizationData
                        ? GetScholarshipEducationLevelsFromFile(contentRootPath, logger)
                        : GetPreconfiguredScholarshipEducationLevels());

                    await context.SaveChangesAsync();
                }

                if (!context.ScholarshipInterests.Any())
                {
                    await context.ScholarshipInterests.AddRangeAsync(useCustomizationData
                        ? GetScholarshipInterestsFromFile(contentRootPath, logger)
                        : GetPreconfiguredScholarshipInterests());

                    await context.SaveChangesAsync();
                }

                if (!context.ScholarshipLocations.Any())
                {
                    await context.ScholarshipLocations.AddRangeAsync(useCustomizationData
                        ? GetScholarshipLocationsFromFile(contentRootPath, logger)
                        : GetPreconfiguredScholarshipLocations());

                    await context.SaveChangesAsync();
                }

                if (!context.ScholarshipSchools.Any())
                {
                    await context.ScholarshipSchools.AddRangeAsync(useCustomizationData
                        ? GetScholarshipSchoolsFromFile(contentRootPath, context, logger)
                        : GetPreconfiguredScholarshipSchools());

                    await context.SaveChangesAsync();
                }

                if (!context.ScholarshipFeeStructures.Any())
                {
                    await context.ScholarshipFeeStructures.AddRangeAsync(useCustomizationData
                        ? GetScholarshipFeeStructuresFromFile(contentRootPath, context, logger)
                        : GetPreconfiguredScholarshipFeeStructures());

                    await context.SaveChangesAsync();
                }

                if (!context.ScholarshipItems.Any())
                {
                    await context.ScholarshipItems.AddRangeAsync(useCustomizationData
                        ? GetScholarshipItemsFromFile(contentRootPath, context, logger)
                        : GetPreconfiguredItems());

                    await context.SaveChangesAsync();

                    GetScholarshipItemPictures(contentRootPath, picturePath);
                }
            });
        }