def get_all_accounts_in_range(self, account): sql = "SELECT * FROM accounts WHERE account_client_id = %s" less = float(account.account_name) more_than = float(account.account_amount) cursor = connection.cursor() cursor.execute(sql, [account.account_client_id]) records = cursor.fetchall() account_list = [] if records: for record in records: temp_account = Account(record[0], record[1], float(record[2]), record[3]) if less > temp_account.account_amount & temp_account.account_amount > more_than: account_list.append(temp_account.json()) return account_list else: raise ResourceNotFound( f"Client with id: {account.account_client_id} has no accounts." )
def get_deposit(self, account): sql = "SELECT * FROM accounts WHERE id = %s AND account_client_id = %s" cursor = connection.cursor() cursor.execute(sql, [account.account_id, account.account_client_id]) record = cursor.fetchone() if record: return Account(record[0], record[1], (float(record[2]) + account.account_amount), record[3]).json() else: raise ResourceNotFound(f"Client id and account id does not exist.")
def get_account(self, account): sql = "SELECT * FROM accounts WHERE id = %s AND account_client_id = %s" cursor = connection.cursor() cursor.execute( sql, (int(account.account_id), int(account.account_client_id))) record = cursor.fetchone() if record: return Account(record[0], record[1], float(record[2]), record[3]).json() else: raise ResourceNotFound( f"Account id or client account id - NOT FOUND")
def update_accounts_after_transfer(self, account): sql = "UPDATE accounts SET amount=%s WHERE id = %s RETURNING *" cursor = connection.cursor() cursor.execute(sql, [account.account_amount, account.account_id]) connection.commit() record = cursor.fetchone() if record: return Account(record[0], record[1], float(record[2]), record[3]).json() else: raise ResourceNotFound( f"No account has account and client id provided")
def delete_account(self, account): sql = "DELETE FROM accounts WHERE id=%s AND account_client_id = %s RETURNING *" cursor = connection.cursor() cursor.execute( sql, (int(account.account_id), int(account.account_client_id))) connection.commit() record = cursor.fetchone() if record: changed_account = Account(record[0], record[1], float(record[2]), record[3]) return changed_account else: raise ResourceNotFound( f"No account has account and client id provided")
def create_account(self, account): self.client_id_existence_test(int(account.account_client_id)) sql = "INSERT INTO accounts (name,amount, account_client_id) VALUES (%s, %s, %s) RETURNING *" cursor = connection.cursor() cursor.execute(sql, (account.account_name, account.account_amount, account.account_client_id)) connection.commit() record = cursor.fetchone() if record: return Account(record[0], record[1], float(record[2]), record[3]) else: raise ResourceNotFound( f"The Client id enter can't be verified. Unable to create an account without an activity client id" )
def get_transfer_account_from(self, account): sql = "SELECT * FROM accounts WHERE id = %s AND account_client_id = %s" cursor = connection.cursor() cursor.execute(sql, [account.account_id, account.account_client_id]) record = cursor.fetchone() if record: if float(record[2] > account.account_amount): return Account(record[0], record[1], (float(record[2]) - account.account_amount), record[3]) else: raise ResourceAmountInsufficient( f"The account selected has insuffiecient funds for withdrawal" ) else: raise ResourceNotFound(f"Client id and account id does not exist.")
def get_account(client_id, account_id): try: account = Account(account_id, "", 0, client_id) return jsonify(AccountService.get_account(account)), 200 except ResourceNotFound as r: return r.message, 404