public static void WriteToSettingsFile(TweezersInternalSettings settings)
 {
     using (FileStream fs = new FileStream(internalSettingsFileName, FileMode.OpenOrCreate))
     {
         fs.SetLength(0);
         using (StreamWriter sw = new StreamWriter(fs))
         {
             sw.Write(settings.Serialize());
         }
     }
 }
        public static TweezersInternalSettings Init()
        {
            if (File.Exists(internalSettingsFileName))
            {
                string settings = File.ReadAllText(internalSettingsFileName);
                TweezersInternalSettings internalSettings = settings.Deserialize <TweezersInternalSettings>();
                TweezersSchemaFactory.DatabaseProxy = GetDatabaseProxyInstance(internalSettings.DBDetails);

                SchemaManagement.CanChangeSchema = internalSettings.AppDetails.CanChangeSchema;

                if (internalSettings.AppDetails.UseIdentity)
                {
                    IdentityManager.RegisterIdentity();
                    if (!TweezersRuntimeSettings.Instance.IsInitialized)
                    {
                        string  username = internalSettings.AppDetails.InitialUsername;
                        JObject user     = UsersController.FindUser(username);
                        if (user != null)
                        {
                            TweezersObject usersObjectMetadata = TweezersSchemaFactory.Find(IdentityManager.UsersCollectionName,
                                                                                            true, true);
                            usersObjectMetadata.Delete(TweezersSchemaFactory.DatabaseProxy, user["_id"].ToString());
                        }

                        UsersController.CreateUser(new CreateUserRequest()
                        {
                            Username = username,
                            Password = internalSettings.AppDetails.InitialPassword,
                            Name     = "Administrator",
                            RoleId   = Schemas.AdministratorRoleTemplate.Deserialize <JObject>()["_id"].ToString(),
                        });
                    }
                }

                if (internalSettings.Schema != null)
                {
                    foreach (TweezersObject obj in internalSettings.Schema)
                    {
                        if (TweezersSchemaFactory.Find(obj.CollectionName, safe: true) == null)
                        {
                            TweezersSchemaFactory.AddObject(obj);
                        }
                    }
                }

                Instance = internalSettings;
            }

            return(Instance);
        }
        public ActionResult <JObject> SetMetadata([FromBody] TweezersInternalSettings settings)
        {
            try
            {
                if (!TweezersRuntimeSettings.Instance.IsInitialized)
                {
                    TweezersInternalSettings.WriteToSettingsFile(settings);
                    TweezersInternalSettings.Init();
                    TweezersRuntimeSettings.Instance.IsInitialized = true;
                    TweezersRuntimeSettings.Instance.WriteToSettingsFile();
                    return(TweezersOk());
                }
            }
            catch (Exception e)
            {
                return(TweezersBadRequest($"Unable to connect to DB: {e.Message}"));
            }

            return(TweezersBadRequest("Invalid Settings File"));
        }