summaryrefslogtreecommitdiff
path: root/persistence.py
diff options
context:
space:
mode:
authorDmitrii Morozov <snoopdesigns@gmail.com>2024-03-28 19:31:10 +0100
committerDmitrii Morozov <snoopdesigns@gmail.com>2024-03-28 19:31:10 +0100
commit9546faa2601c1b314bda0f6a0ee7bdd799a13061 (patch)
tree875ea567953fe4b5e94594d3bd4f59b5ff734f2d /persistence.py
parent27334b6e89cdec616ea6ac180ec1047a0bd4f54f (diff)
Async notification for changes in status
Diffstat (limited to 'persistence.py')
-rw-r--r--persistence.py28
1 files changed, 24 insertions, 4 deletions
diff --git a/persistence.py b/persistence.py
index eb1903f..d2998e9 100644
--- a/persistence.py
+++ b/persistence.py
@@ -10,12 +10,32 @@ class UserRepository:
def __initialize(self):
cur = self.__getConnection().cursor()
cur.execute("CREATE TABLE IF NOT EXISTS user(chat_id INT)")
+ cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS chat_id_idx ON user(chat_id)")
def __getConnection(self):
return sqlite3.connect('db.sqlite')
+ def getUser(self, chat_id):
+ connection = self.__getConnection()
+ cur = connection.cursor()
+ query = "select * from user where chat_id = {chat_id}".format(chat_id = chat_id)
+ cur.execute(query)
+ return cur.fetchone()
+
+ def getAllUsers(self):
+ connection = self.__getConnection()
+ cur = connection.cursor()
+ query = "select * from user"
+ cur.execute(query)
+ return cur.fetchall()
+
+
def putUser(self, chat_id):
- cur = self.__getConnection().cursor()
- data_tuple = (chat_id)
- print(type(chat_id))
- cur.execute("INSERT INTO user(chat_id) VALUES(?)", (chat_id, ))
+ if not self.getUser(chat_id):
+ connection = self.__getConnection()
+ cur = connection.cursor()
+ query = "INSERT INTO user(chat_id) VALUES({chat_id})".format(chat_id = chat_id)
+ cur.execute(query)
+ connection.commit()
+ else:
+ print("User {} already exsits, skipping putUser()".format(chat_id))