From 319a21472fb34410cb8b0b81c9add6ba117d0309 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 28 May 2022 22:00:04 +0000 Subject: [PATCH] add subscribe_user method and rename some variables --- database.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/database.py b/database.py index f10687f..e8ce88f 100644 --- a/database.py +++ b/database.py @@ -4,29 +4,33 @@ class Database(): self.conn = sqlite3.connect(path) self.cur = self.conn.cursor() self.cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER, telegram_id NUMERIC NOT NULL UNIQUE, PRIMARY KEY(id))') - self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id INTEGER, link TEXT NOT NULL UNIQUE, PRIMARY KEY(id))') + self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id INTEGER, url TEXT NOT NULL UNIQUE, PRIMARY KEY(id))') self.cur.execute('CREATE TABLE IF NOT EXISTS subscribes (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))') - self.cur.execute('CREATE TABLE IF NOT EXISTS feeds_last_items (feed_id INTEGER, link TEXT NOT NULL UNIQUE, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))') + self.cur.execute('CREATE TABLE IF NOT EXISTS feeds_last_items (feed_id INTEGER, url TEXT NOT NULL UNIQUE, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))') def add_user(self, telegram_id: str) -> int: - self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id}) + self.cur.execute('INSERT INTO users (telegram_id) VALUES (?)', (telegram_id,)) self.conn.commit() return self.cur.lastrowid def find_user(self, telegram_id: str) -> int | None: - self.cur.execute('SELECT id FROM users WHERE telegram_id = :telegram_id', {'telegram_id': telegram_id}) - user_id = self.cur.fetchone() - if user_id is None: + self.cur.execute('SELECT id FROM users WHERE telegram_id = ?', (telegram_id,)) + row = self.cur.fetchone() + if row is None: return None - return user_id[0] + return row[0] - def add_feed(self, link: str) -> int: - self.cur.execute('INSERT INTO feeds (link) VALUES (:link)', {'link': link}) + def add_feed(self, url: str) -> int: + self.cur.execute('INSERT INTO feeds (url) VALUES (?)', (url,)) self.conn.commit() return self.cur.lastrowid + def subscribe_user(self, user_id: int, feed_id: int): + self.cur.execute('INSERT INTO subscribes (user_id, feed_id) VALUES (?, ?)', (user_id, feed_id)) + self.conn.commit() + def delete_feed(self, feed_id: int) -> None: - self.cur.execute('DELETE FROM feeds WHERE id = :feed_id', {'feed_id': feed_id}) + self.cur.execute('DELETE FROM feeds WHERE id = ?', (feed_id,)) self.conn.commit() def get_feeds():