Esempio n. 1
0
        public int ImportEntitiesFromFile(string filePath)
        {
            var existingEntitiesCount = _repository.GetAll().Count();

            if (existingEntitiesCount > 0)
            {
                _logger.LogInformation($"Entities of type '{typeof(T).Name}' have been already imported.");
                return(0);
            }

            _logger.LogInformation($"Reading file with {typeof(T).Name}s . . .");

            var entityLines = File.ReadLines(filePath).ToList();

            _entityBuilder.ReadEntityMappings(entityLines.First());
            entityLines.RemoveAt(0);

            _logger.LogInformation($"Mapping to {typeof(T).Name} objects . . .");
            var mappedEntities = entityLines.Select(line => _entityBuilder.Build(line)).ToList();

            _logger.LogInformation($"Read {mappedEntities.Count} lines of {typeof(T).Name}.");
            SortEntities(mappedEntities);
            var entitiesCount = SaveEntities(mappedEntities);

            _logger.LogInformation($"{entitiesCount} entities of type '{typeof(T).Name}' have been successfully saved.");

            return(entitiesCount);
        }