diff --git a/goodchain/src/classes/User.py b/goodchain/src/classes/User.py index 9c09827..32ee093 100644 --- a/goodchain/src/classes/User.py +++ b/goodchain/src/classes/User.py @@ -35,6 +35,7 @@ class User: # check if username is already taken if self.db.fetchUserByUsername(input_username): + print("Username already taken") return False # create sig for user @@ -73,3 +74,20 @@ class User: print('Password updated') else: print('Something went wrong while trying to update password..') + + def updateAccount(self): + # Get new username + new_username = input('Enter your new username: ') + + # check if username is already taken + if self.db.fetchUserByUsername(new_username): + print("Username already taken") + return False + + private_key_bytes = Signature.privateKeyToBytes(self.private_key) + + if self.db.changeUsername(private_key_bytes, new_username) == True: + print('Username updated') + else: + print('Something went wrong while trying to update username..') + diff --git a/goodchain/src/helpers/DatabaseHelper.py b/goodchain/src/helpers/DatabaseHelper.py index 2726289..b3885f2 100644 --- a/goodchain/src/helpers/DatabaseHelper.py +++ b/goodchain/src/helpers/DatabaseHelper.py @@ -53,7 +53,7 @@ class DatabaseHelper: def createUser(self, private_key, public_key, username, password): if not self.conn: - return False + return None try: self.cursor.execute("INSERT INTO `users` (private_key, public_key, username, password) VALUES (?, ?, ?, ?)", (private_key, public_key, username, password,)) self.commit() @@ -76,6 +76,37 @@ class DatabaseHelper: print(error) return None + def changeUsername(self, user_privatekey, username): + if not self.conn: + return None + + # Execute the query + try: + self.cursor.execute("UPDATE `users` SET username = ? WHERE `private_key` = ?", (username, user_privatekey,)) + self.commit() + + return True + + except sqlite3.Error as error: + print(error) + return None + + def deleteUser(self, user_privatekey): + # Check if the database is open + if not self.conn: + return None + + # Execute the query + try: + self.cursor.execute("DELETE FROM `users` WHERE `private_key` = ?", (user_privatekey, )) + self.commit() + + return True + + except sqlite3.Error as error: + print(error) + return None + def fetchUserByUsername(self, username): if not self.conn: return None diff --git a/goodchain/src/helpers/MenuHelper.py b/goodchain/src/helpers/MenuHelper.py index 62384a4..4cdf2ae 100644 --- a/goodchain/src/helpers/MenuHelper.py +++ b/goodchain/src/helpers/MenuHelper.py @@ -164,7 +164,7 @@ class MenuHelper: print(user.private_key) case "Change username": - print("TODO") + user.updateAccount() case "Change password": user.updatePassword()