rename get_users_subscribes to get_user_feeds method and rename subscribes table to subscriptions

This commit is contained in:
mitsuha_s 2022-05-29 14:04:35 +00:00
parent f1d2ab3b9b
commit 49678a4d6b

View file

@ -5,44 +5,45 @@ class Database():
self.cur = self.conn.cursor() 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 users (id INTEGER, telegram_id NUMERIC 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 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 subscriptions (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))')
self.cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS subscriptions_index ON subscriptions (user_id, feed_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))') 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: def add_user(self, telegram_id: str) -> int:
self.cur.execute('INSERT INTO users (telegram_id) VALUES (?)', (telegram_id,)) self.cur.execute('INSERT INTO users (telegram_id) VALUES (?)', [telegram_id])
self.conn.commit() self.conn.commit()
return self.cur.lastrowid return self.cur.lastrowid
def find_user(self, telegram_id: str) -> int | None: def find_user(self, telegram_id: str) -> int | None:
self.cur.execute('SELECT id FROM users WHERE telegram_id = ?', (telegram_id,)) self.cur.execute('SELECT id FROM users WHERE telegram_id = ?', [telegram_id])
row = self.cur.fetchone() row = self.cur.fetchone()
if row is None: if row is None:
return None return None
return row[0] return row[0]
def add_feed(self, url: str) -> int: def add_feed(self, url: str) -> int:
self.cur.execute('INSERT INTO feeds (url) VALUES (?)', (url,)) self.cur.execute('INSERT INTO feeds (url) VALUES (?)', [url])
self.conn.commit() self.conn.commit()
return self.cur.lastrowid return self.cur.lastrowid
def subscribe_user(self, user_id: int, feed_id: int) -> None: def subscribe_user(self, user_id: int, feed_id: int) -> None:
self.cur.execute('INSERT INTO subscribes (user_id, feed_id) VALUES (?, ?)', (user_id, feed_id)) self.cur.execute('INSERT INTO subscriptions (user_id, feed_id) VALUES (?, ?)', [user_id, feed_id])
self.conn.commit() self.conn.commit()
def unsubscribe_user(self, user_id: int, feed_id: int) -> None: def unsubscribe_user(self, user_id: int, feed_id: int) -> None:
self.cur.execute('DELETE FROM subscribes WHERE feed_id = ? AND user_id = ?', (feed_id, user_id)) self.cur.execute('DELETE FROM subscriptions WHERE feed_id = ? AND user_id = ?', [feed_id, user_id])
self.conn.commit() self.conn.commit()
def delete_feed(self, feed_id: int) -> None: def delete_feed(self, feed_id: int) -> None:
self.cur.execute('DELETE FROM feeds WHERE id = ?', (feed_id,)) self.cur.execute('DELETE FROM feeds WHERE id = ?', [feed_id])
self.conn.commit() self.conn.commit()
def get_feeds(self) -> list: def get_feeds(self) -> list:
self.cur.execute('SELECT * FROM feeds') self.cur.execute('SELECT * FROM feeds')
return self.cur.fetchall() return self.cur.fetchall()
def get_users_subscribes(self, user_id: int) -> list: def get_user_feeds(self, user_id: int) -> list:
self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscribes WHERE user_id = ?)', (user_id,)) self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscriptions WHERE user_id = ?)', [user_id])
return self.cur.fetchall() return self.cur.fetchall()
def get_feed_items(): def get_feed_items():