Example #1
0
        public IActionResult Install(InstallationRequestModel model)
        {
            var databaseSettings  = DependencyResolver.Resolve <IDatabaseSettings>();
            var areTableInstalled = DatabaseManager.IsDatabaseInstalled(databaseSettings);

            if (areTableInstalled)
            {
                return(Json(new { success = false, error = T("Database already installed") }));
            }

            //lets save the database settings to config file
            var connectionString = model.ConnectionString;
            var providerName     = model.ProviderName; // "SqlServer"; //todo: make it selectable to allow sqlite and other providers

            //create the connection string if required
            if (!model.IsConnectionString)
            {
                connectionString = DatabaseManager.CreateConnectionString(new ConnectionStringRequest()
                {
                    IntegratedSecurity = model.IntegratedSecurity,
                    Timeout            = 0,
                    ProviderName       = model.ProviderName,
                    Password           = model.DatabasePassword,
                    ServerName         = model.ServerUrl,
                    UserName           = model.DatabaseUserName,
                    DatabaseName       = model.DatabaseName
                });
            }

            //check if we have correct connection string
            if (!DatabaseManager.IsValidConnection(providerName, connectionString))
            {
                return(Json(new { success = false, error = T("Failed to connect to database") }));
            }

            databaseSettings.WriteSettings(connectionString, providerName);

            //perform the installation
            _installationService.Install();

            //save app settings
            _applicationConfiguration.SetSetting(ApplicationConfig.AppSettingsEncryptionKey, _cryptographyService.GetRandomPassword(32));
            _applicationConfiguration.SetSetting(ApplicationConfig.AppSettingsEncryptionSalt, _cryptographyService.GetRandomPassword(32));
            _applicationConfiguration.SetSetting(ApplicationConfig.AppSettingsApiSecret, _cryptographyService.GetRandomPassword(32));

            //then feed the data
            _installationService.FillRequiredSeedData(model.AdminEmail, model.Password,
                                                      "//" + ApplicationEngine.CurrentHttpContext.Request.Host.Value, model.StoreName);

            //restart the app
            ServerHelper.RestartApplication();

            return(Json(new { success = true }));
        }