diff --git a/speckle_connector_3/src/accounts/accounts.rb b/speckle_connector_3/src/accounts/accounts.rb index fd8da09..cd53429 100644 --- a/speckle_connector_3/src/accounts/accounts.rb +++ b/speckle_connector_3/src/accounts/accounts.rb @@ -25,6 +25,38 @@ module SpeckleConnector3 rows.map { |row| JSON.parse(row[1]) } end + def self.add_account(account_id, account) + unless File.exist?(SPECKLE_ACCOUNTS_DB_PATH) + File.new(SPECKLE_ACCOUNTS_DB_PATH, "w") + db = Sqlite3::Database.new(SPECKLE_ACCOUNTS_DB_PATH) + create_objects_table(db) + db.close + end + db = Sqlite3::Database.new(SPECKLE_ACCOUNTS_DB_PATH) + account_json = JSON.generate(account) + sql_query = "INSERT OR REPLACE INTO objects (hash, content) VALUES ('#{account_id}', '#{account_json}')" + + begin + db.exec(sql_query) + puts "Account with hash #{account_id} has been added/updated." + rescue StandardError => e + puts "An error occurred while adding the account: #{e}" + ensure + db.close + end + end + + # Creates the 'objects' table in the database if it doesn't already exist. + # @param db [Sqlite3::Database] the SQLite3 database instance. + def self.create_objects_table(db) + db.exec <<-SQL + CREATE TABLE IF NOT EXISTS objects ( + hash TEXT PRIMARY KEY, + content TEXT + ); + SQL + end + def self.remove_account(account_id) db_path = SPECKLE_ACCOUNTS_DB_PATH unless File.exist?(db_path) diff --git a/speckle_connector_3/src/actions/account_actions/add_account.rb b/speckle_connector_3/src/actions/account_actions/add_account.rb new file mode 100644 index 0000000..4da6c1f --- /dev/null +++ b/speckle_connector_3/src/actions/account_actions/add_account.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require_relative '../action' +require_relative '../../accounts/accounts' +require_relative '../load_saved_streams' + +module SpeckleConnector3 + module Actions + # Action to add account to local Account db. + class AddAccount < Action + # @param state [States::State] the current state of the {App::SpeckleConnectorApp} + # @return [States::State] the new updated state object + def self.update_state(state, resolve_id, account_id, account) + SpeckleConnector3::Accounts.add_account(account_id, account) + js_script = "accountsBinding.receiveResponse('#{resolve_id}')" + state.with_add_queue_js_command('addAccount', js_script) + end + end + end +end diff --git a/speckle_connector_3/src/ui/bindings/accounts_binding.rb b/speckle_connector_3/src/ui/bindings/accounts_binding.rb index 9065898..b7c93af 100644 --- a/speckle_connector_3/src/ui/bindings/accounts_binding.rb +++ b/speckle_connector_3/src/ui/bindings/accounts_binding.rb @@ -2,6 +2,7 @@ require_relative 'binding' require_relative '../../actions/account_actions/get_accounts' +require_relative '../../actions/account_actions/add_account' require_relative '../../actions/account_actions/remove_account' module SpeckleConnector3 @@ -13,6 +14,7 @@ module SpeckleConnector3 def commands @commands ||= { getAccounts: Commands::ActionCommand.new(@app, self, Actions::GetAccounts), + addAccount: Commands::ActionCommand.new(@app, self, Actions::AddAccount), removeAccount: Commands::ActionCommand.new(@app, self, Actions::RemoveAccount) }.freeze end