internal static bool TryGetCollectionId(LarsCosmosDbCollectionSettings settings, string fileName, out string collectionId)
        {
            collectionId = string.Empty;

            if (fileName == settings.CollectionSettings.Category.FileName)
            {
                collectionId = settings.CollectionSettings.Category.CollectionId;
            }
            if (fileName == settings.CollectionSettings.LearningDelivery.FileName)
            {
                collectionId = settings.CollectionSettings.LearningDelivery.CollectionId;
            }
            if (fileName == settings.CollectionSettings.LearningDeliveryCategory.FileName)
            {
                collectionId = settings.CollectionSettings.LearningDeliveryCategory.CollectionId;
            }
            if (fileName == settings.CollectionSettings.AwardOrgCode.FileName)
            {
                collectionId = settings.CollectionSettings.AwardOrgCode.CollectionId;
            }
            if (fileName == settings.CollectionSettings.SectorSubjectAreaTier1.FileName)
            {
                collectionId = settings.CollectionSettings.SectorSubjectAreaTier1.CollectionId;
            }
            if (fileName == settings.CollectionSettings.SectorSubjectAreaTier2.FileName)
            {
                collectionId = settings.CollectionSettings.SectorSubjectAreaTier2.CollectionId;
            }
            if (fileName == settings.CollectionSettings.MINotionalNVQLevelv2.FileName)
            {
                collectionId = settings.CollectionSettings.MINotionalNVQLevelv2.CollectionId;
            }

            return(!string.IsNullOrEmpty(collectionId));
        }
        internal static async Task CreateOrRecreateDocumentCollectionAsync(LarsCosmosDbCollectionSettings settings, DocumentClient client, string fileName)
        {
            if (TryGetCollectionId(settings, fileName, out string collectionId))
            {
                var collection = client
                                 .CreateDocumentCollectionQuery(UriFactory.CreateDatabaseUri(settings.DatabaseId))
                                 .ToArray()
                                 .FirstOrDefault(c => c.Id == collectionId);

                if (collection != null)
                {
                    await client.DeleteDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(settings.DatabaseId, collectionId));
                }

                await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(settings.DatabaseId), new DocumentCollection { Id = collectionId });
            }
        }
        internal static async Task ImportFileContentAsync(LarsCosmosDbCollectionSettings settings, DocumentClient client, string fileName, Stream stream)
        {
            if (TryGetCollectionId(settings, fileName, out string collectionId))
            {
                var headings = new List <string>();

                using (var reader = new StreamReader(stream))
                {
                    var lineNumber = 0;

                    foreach (var line in DelimitedFileReader.ReadLines(reader, new DelimitedFileSettings(true)))
                    {
                        lineNumber++;

                        if (lineNumber == 1)
                        {
                            headings = line.Fields.Select(f => CleanHeading(f.Value)).ToList();
                            continue;
                        }

                        var dict = new Dictionary <string, object>();

                        foreach (var field in line.Fields)
                        {
                            dict[headings[field.Number - 1]] = field.Value;
                        }

                        var doc = dict.ToExpandoObject();

                        await client.CreateDocumentAsync(
                            UriFactory.CreateDocumentCollectionUri(settings.DatabaseId, collectionId),
                            doc);
                    }
                }
            }
        }
 internal static async Task CreateDatabaseIfNotExistsAsync(LarsCosmosDbCollectionSettings settings, DocumentClient client)
 {
     await client.CreateDatabaseIfNotExistsAsync(new Database { Id = settings.DatabaseId });
 }