Example #1
0
        public async Task SeedAsync(CommentContext context, IWebHostEnvironment env, IOptions <CommentSettings> settings, ILogger <CommentContextSeed> logger)
        {
            var policy = CreatePolicy(logger, nameof(CommentContextSeed));

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

                if (!context.Comments.Any())
                {
                    await context.Comments.AddRangeAsync(useCustomizationData
                        ? GetCommentsFromFile(contentRootPath, context, logger)
                        : GetPreconfiguredComments());

                    await context.SaveChangesAsync();
                }
            });
        }
 public CommentRepository(CommentContext context)
 {
     _context = context;
 }
Example #3
0
        private IEnumerable <CommentItem> GetCommentsFromFile(string contentRootPath, CommentContext context, ILogger <CommentContextSeed> logger)
        {
            string csvFileComments = Path.Combine(contentRootPath, "Setup", "Comments.csv");

            if (!File.Exists(csvFileComments))
            {
                return(GetPreconfiguredComments());
            }

            string[] csvheaders;
            try
            {
                string[] requiredHeaders = { "catalogtypename", "catalogbrandname", "description", "name", "price", "pictureFileName" };
                string[] optionalheaders = { "availablestock", "restockthreshold", "maxstockthreshold", "onreorder" };
                csvheaders = GetHeaders(csvFileComments, requiredHeaders, optionalheaders);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message);
                return(GetPreconfiguredComments());
            }

            return(File.ReadAllLines(csvFileComments)
                   .Skip(1)      // skip header row
                   .Select(row => Regex.Split(row, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"))
                   .SelectTry(column => CreateCommentItem(column, csvheaders))
                   .OnCaughtException(ex => { logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message); return null; })
                   .Where(x => x != null));
        }