private void InsertOrUpdate(string json, MongoContext context, Arguments arguments) { BabelSettingsModel currentObject = JsonConvert.DeserializeObject <BabelSettingsModel>(json); IMongoCollection <BabelSettingsModel> collection = context.Database.GetCollection <BabelSettingsModel>("defaultsettings"); BabelSettingsModel dbObject = collection.Find( Builders <BabelSettingsModel> .Filter.Eq(x => x.SettingCode, currentObject.SettingCode)) .ToList() .FirstOrDefault(); if (dbObject == null) { collection.InsertOne(currentObject); Console.WriteLine("Successfully inserted your document. Program complete."); } else { Console.WriteLine("File detected, trying to update."); List <List <DefaultLayout> > languages = new List <List <DefaultLayout> >() { currentObject.DefaultLayout.Italian, currentObject.DefaultLayout.English, currentObject.DefaultLayout.Deutsch, currentObject.DefaultLayout.Francaise, currentObject.DefaultLayout.Portoguese, }; foreach (List <DefaultLayout> lang in languages) { if (arguments.Language == LanguageEnum.it && (dbObject.DefaultLayout.Italian == null || arguments.Override)) { dbObject.DefaultLayout.Italian = lang; } else if (arguments.Language == LanguageEnum.en && (dbObject.DefaultLayout.English == null || arguments.Override)) { dbObject.DefaultLayout.English = lang; } else if (arguments.Language == LanguageEnum.dt && (dbObject.DefaultLayout.Deutsch == null || arguments.Override)) { dbObject.DefaultLayout.Deutsch = lang; } else if (arguments.Language == LanguageEnum.fr && (dbObject.DefaultLayout.Francaise == null || arguments.Override)) { dbObject.DefaultLayout.Francaise = lang; } else if (arguments.Language == LanguageEnum.pt && (dbObject.DefaultLayout.Portoguese == null || arguments.Override)) { dbObject.DefaultLayout.Portoguese = lang; } } collection.ReplaceOne(Builders <BabelSettingsModel> .Filter.Eq(x => x.SettingCode, currentObject.SettingCode), dbObject); Console.WriteLine($"Your {currentObject.SettingCode}.json was updated for language -> {arguments.Language}"); } }
private string GeneratePropertiesBasedJson(Dictionary <string, Type> keyValuePairs, Arguments arguments) { List <DefaultLayout> defaultLayout = new List <DefaultLayout>(); List <DefaultSettings> defaultSettings = new List <DefaultSettings>(); int count = 1; Console.WriteLine("Generating json file. Please wait."); foreach (KeyValuePair <string, Type> kvp in keyValuePairs) { string format = GetFormat(kvp.Value); int decimals = kvp.Value == typeof(double) || kvp.Value == typeof(double?) || kvp.Value == typeof(float) || kvp.Value == typeof(float?) ? 2 : 0; defaultSettings.Add(new DefaultSettings() { DataField = kvp.Key, Alignment = format.Equals("number") ? "right" : "left", Format = format, DataSource = string.Empty, Decimals = decimals, Mandatory = false, ReadOnly = false }); defaultLayout.Add(new DefaultLayout() { DataField = kvp.Key, IsVisible = false, Caption = kvp.Key, SortOrder = count * 10, GroupOrder = -1, Width = 0, }); count++; } LanguageLayout languageLayout = new LanguageLayout(); switch (arguments.Language) { case LanguageEnum.en: languageLayout.English = defaultLayout; break; case LanguageEnum.it: languageLayout.Italian = defaultLayout; break; case LanguageEnum.dt: languageLayout.Deutsch = defaultLayout; break; case LanguageEnum.fr: languageLayout.Francaise = defaultLayout; break; case LanguageEnum.pt: languageLayout.Portoguese = defaultLayout; break; } BabelSettingsModel settingsModel = new BabelSettingsModel { SettingCode = arguments.SettingsCode, DefaultLayout = languageLayout, DefaultSettings = defaultSettings, }; string json = JsonConvert.SerializeObject(settingsModel, new JsonSerializerSettings() { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() }); babelFileManager.WriteFile(json, arguments); babelFileManager.OpenFile(arguments); if (_applicationSettings.ConnectionString != null && !string.IsNullOrEmpty(_applicationSettings.ConnectionString.Database) && !string.IsNullOrEmpty(_applicationSettings.ConnectionString.MongoConnection)) { Console.Write("Connection string detected, would you like to update database?(y/n): "); ConsoleKeyInfo key = Console.ReadKey(); if (key.KeyChar.ToString().Equals("y", StringComparison.OrdinalIgnoreCase)) { babelDatabaseManger.AddDefaultSettingsInMongodb(json, _applicationSettings, arguments); } else { Console.WriteLine("\nIgnore update database. Program complete."); } } return(json); }