private async Task CreateEmptyContentItem(string key) { using (var scope = _serviceProvider.CreateScope()) { var appId = _options.AppId; // Service locate scoped components. It's not the most elegant thing, but it's the easiest solution. var contentStore = scope.ServiceProvider.GetRequiredService <IContentStore>(); var applicationStore = scope.ServiceProvider.GetRequiredService <IApplicationStore>(); var versioner = scope.ServiceProvider.GetRequiredService <IVersioner>(); var dispatcher = scope.ServiceProvider.GetRequiredService <IDispatcher>(); var contentItem = (await contentStore.GetContentItems(new ContentItemQuery { AppId = appId, ContentKey = key })).FirstOrDefault(); if (contentItem == null) { var initPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "apptext-localization") })); var saveContentItemCommandHandler = new SaveContentItemCommandHandler(contentStore, versioner, new ContentItemValidator(contentStore, applicationStore), initPrincipal, dispatcher); var collection = (await contentStore.GetContentCollections(new ContentCollectionQuery { AppId = appId, Name = _options.CollectionName })).FirstOrDefault(); await saveContentItemCommandHandler.Handle(new SaveContentItemCommand { AppId = appId, CollectionId = collection.Id, ContentKey = key, Content = new Dictionary <string, object>() }); } } }
private async Task InitCollection(string collectionName, IContentStore contentStore, ContentType translationsContentType, IServiceProvider serviceProvider) { var collection = (await contentStore.GetContentCollections(new ContentCollectionQuery { AppId = Constants.AppTextAdminAppId, Name = collectionName })).FirstOrDefault(); if (collection == null) { collection = new ContentCollection { ContentType = translationsContentType, Name = collectionName, ListDisplayField = TranslationConstants.TranslationTextFieldName, Version = 1 }; var contentCollectionSaveCommandHandler = serviceProvider.GetRequiredService <ICommandHandler <SaveContentCollectionCommand> >(); await contentCollectionSaveCommandHandler.Handle(new SaveContentCollectionCommand(Constants.AppTextAdminAppId, collection)); } // Ensure content var collectionContainsItems = (await contentStore.GetContentItems(new ContentItemQuery { AppId = Constants.AppTextAdminAppId, CollectionId = collection.Id, First = 1 })).Length > 0; if (!collectionContainsItems) { // Read content from embedded json files and add to storage var contentFiles = _translationsContents.Where(tc => tc.Name.StartsWith($"Content.{collectionName}")); var initPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "apptextadmin-init") })); var saveContentItemCommandHandler = new SaveContentItemCommandHandler( serviceProvider.GetRequiredService <IContentStore>(), serviceProvider.GetRequiredService <IVersioner>(), serviceProvider.GetRequiredService <ContentItemValidator>(), initPrincipal, serviceProvider.GetRequiredService <IDispatcher>()); foreach (var contentFile in contentFiles) { _logger.LogInformation("Importing content from {0}", contentFile.Name); var language = contentFile.Name.Substring($"Content.{collectionName}".Length + 1).Replace(".json", String.Empty); using (var contentStream = contentFile.CreateReadStream()) using (var sr = new StreamReader(contentStream)) using (var jsonReader = new JsonTextReader(sr)) { var items = JArray.Load(jsonReader).Children <JObject>(); foreach (var item in items) { foreach (var prop in item.Properties()) { var saveContentItemCommand = new SaveContentItemCommand { AppId = Constants.AppTextAdminAppId, CollectionId = collection.Id, LanguagesToValidate = new[] { language }, ContentKey = prop.Name }; var contentItem = (await contentStore.GetContentItems(new ContentItemQuery { AppId = Constants.AppTextAdminAppId, CollectionId = collection.Id, ContentKey = prop.Name, First = 1 })).FirstOrDefault(); if (contentItem != null) { saveContentItemCommand.Id = contentItem.Id; saveContentItemCommand.Version = contentItem.Version; saveContentItemCommand.Content = contentItem.Content; } var contentFieldValue = contentItem != null ? JObject.FromObject(contentItem.Content[TranslationConstants.TranslationTextFieldName]) : new JObject(); contentFieldValue[language] = prop.Value; saveContentItemCommand.Content[TranslationConstants.TranslationTextFieldName] = contentFieldValue; var result = await saveContentItemCommandHandler.Handle(saveContentItemCommand); } } } } } }