public LocalizationCollectionEntity Create(LocalizationCollectionEntity record, bool writeToFile = true, bool createFromStatic = false) { using (IDatabaseProvider provider = ProviderFactory.GetProvider(_connectionName)) { LocalizationCollectionEntity collection = Read(record.Locale); if (collection == null) { if (createFromStatic) { return provider.Insert(record); } collection = CreateCleanLocalizationDictionary(record.Locale); } else { collection.LocalizationItems = record.LocalizationItems; provider.Update(collection); } if (writeToFile) SerializationUtilities.ReplaceFile(ConfigSettings.LocalizationFilePath + @"\" + record.Locale + ".json", collection); return collection; } }
public LocalizationCollectionEntity Read(LocalizationCollectionEntity collection) { try { LocalizationCollectionEntity fullCollection = Read(collection.Locale); if (fullCollection != null) { for (int i = 0; i < collection.LocalizationItems.Count; i++) { LocalizationEntity item = fullCollection.LocalizationItems.Find(x => x.Key == collection.LocalizationItems[i].Key); if (item == null) { continue; } collection.LocalizationItems[i] = item; } return collection; } return null; } catch (Exception ex) { throw new DataAccessException("Error querying Mongo Database: " + ex.Message); } }
private LocalizationKeyEntity GetKeyValueByNameAndLocale(LocalizationCollectionEntity collection, string keyName) { if (!collection.LocalizationItems.Any(e => e.Key == keyName)) return null; LocalizationEntity value = collection.LocalizationItems.First(e => e.Key == keyName); return new LocalizationKeyEntity { LocaleId = collection.Id, Locale = collection.Locale, Description = value.Description, Value = value.Value }; }
private LocalizationCollectionEntity CreateCleanLocalizationDictionary(string localeName) { using (IDatabaseProvider provider = ProviderFactory.GetProvider(_connectionName)) { LocalizationCollectionEntity collection = Read("default"); if (collection == null) { throw new DataException("Unable to find the default locale"); } LocalizationCollectionEntity newLocale = new LocalizationCollectionEntity(); newLocale.Locale = localeName; foreach (LocalizationEntity localizationEntity in collection.LocalizationItems) { newLocale.LocalizationItems.Add(new LocalizationEntity { Key = localizationEntity.Key, Value = localizationEntity.Key, Description = "" }); } return provider.Insert(newLocale); } }
public void ShouldReadLocalizationCollectionForAllItems() { // Arrange LocalizationRepository repository = new LocalizationRepository(ConfigSettings.LocalizationDatabaseConnectionName); LocalizationCollectionEntity assertItem = LocalizationDataProvider.PreInsertArrange(repository); LocalizationCollectionEntity request = new LocalizationCollectionEntity(); request.Locale = assertItem.Locale; // Act LocalizationCollectionEntity response = repository.Read(request.Locale); // Assert Assert.IsNotNull(response); Assert.That(response.LocalizationItems.Count, Is.GreaterThan(0)); }
public void ShouldReadLocalizationCollectionForSpecificItem() { // Arrange LocalizationRepository repository = new LocalizationRepository(ConfigSettings.LocalizationDatabaseConnectionName); LocalizationCollectionEntity assertItem = LocalizationDataProvider.PreInsertArrange(repository); LocalizationCollectionEntity request = new LocalizationCollectionEntity(); request.LocalizationItems.Add(new LocalizationEntity() { Key = assertItem.LocalizationItems[0].Key }); request.Locale = assertItem.Locale; // Act LocalizationCollectionEntity response = repository.Read(request); // Assert Assert.IsNotNull(response); Assert.That(response.LocalizationItems.Count, Is.EqualTo(1)); Assert.That(response.LocalizationItems[0].Value, Is.EqualTo(assertItem.LocalizationItems[0].Value)); }
private LocalizationDictionary TranslateToResponse(LocalizationCollectionEntity collection) { LocalizationDictionary response = collection.TranslateTo<LocalizationDictionary>(); collection.LocalizationItems.ForEach(x => response.LocalizationData.Add(x.TranslateTo<LocalizationItem>())); return response; }