fix removal feed, refactor return id and add type hints
This commit is contained in:
parent
1bdb00e8e7
commit
f11b0b91af
32
database.py
32
database.py
|
@ -1,5 +1,6 @@
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extras
|
from psycopg2.extensions import connection
|
||||||
|
from psycopg2.extras import DictCursor, DictRow
|
||||||
|
|
||||||
from logging import Logger
|
from logging import Logger
|
||||||
|
|
||||||
|
@ -14,16 +15,16 @@ class Database:
|
||||||
"""Create a database file if not exists."""
|
"""Create a database file if not exists."""
|
||||||
self.log: Logger = log
|
self.log: Logger = log
|
||||||
self.log.debug('Database.__init__(DSN=\'%s\')', dsn)
|
self.log.debug('Database.__init__(DSN=\'%s\')', dsn)
|
||||||
self.conn = psycopg2.connect(dsn)
|
self.conn: connection = psycopg2.connect(dsn)
|
||||||
self.cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
self.cur: DictCursor = self.conn.cursor(cursor_factory=DictCursor)
|
||||||
self.__init_schema()
|
self.__init_schema()
|
||||||
|
|
||||||
def add_user(self, telegram_id: int) -> int:
|
def add_user(self, telegram_id: int) -> int:
|
||||||
"""Add a user's telegram id to the database and return its database id."""
|
"""Add a user's telegram id to the database and return its database id."""
|
||||||
self.log.debug('add_user(telegram_id=\'%s\')', telegram_id)
|
self.log.debug('add_user(telegram_id=\'%s\')', telegram_id)
|
||||||
self.cur.execute('INSERT INTO users (telegram_id) VALUES (%s)', [telegram_id])
|
id = self.cur.execute('INSERT INTO users (telegram_id) VALUES (%s) RETURNING id', [telegram_id])
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
return self.find_user(telegram_id)
|
return id
|
||||||
|
|
||||||
def find_user(self, telegram_id: int) -> int | None:
|
def find_user(self, telegram_id: int) -> int | None:
|
||||||
"""Get a user's telegram id and return its database id."""
|
"""Get a user's telegram id and return its database id."""
|
||||||
|
@ -37,9 +38,9 @@ class Database:
|
||||||
def add_feed(self, url: str) -> int:
|
def add_feed(self, url: str) -> int:
|
||||||
"""Add a feed to the database and return its id."""
|
"""Add a feed to the database and return its id."""
|
||||||
self.log.debug('add_feed(url=\'%s\')', url)
|
self.log.debug('add_feed(url=\'%s\')', url)
|
||||||
self.cur.execute('INSERT INTO feeds (url) VALUES (%s)', [url])
|
id = self.cur.execute('INSERT INTO feeds (url) VALUES (%s) RETURNING id', [url])
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
return self.find_feed_by_url(url)
|
return id
|
||||||
|
|
||||||
def find_feed_by_url(self, url: str) -> int | None:
|
def find_feed_by_url(self, url: str) -> int | None:
|
||||||
"""Find feed ID by url."""
|
"""Find feed ID by url."""
|
||||||
|
@ -126,14 +127,14 @@ class Database:
|
||||||
self.cur.execute('SELECT * FROM feeds')
|
self.cur.execute('SELECT * FROM feeds')
|
||||||
return self.cur.fetchall()
|
return self.cur.fetchall()
|
||||||
|
|
||||||
def find_user_feeds(self, user_id: int) -> list[psycopg2.extras.DictRow]:
|
def find_user_feeds(self, user_id: int) -> list[DictRow]:
|
||||||
"""Return a list of feeds the user is subscribed to."""
|
"""Return a list of feeds the user is subscribed to."""
|
||||||
self.log.debug('find_user_feeds(user_id=\'%s\')', user_id)
|
self.log.debug('find_user_feeds(user_id=\'%s\')', user_id)
|
||||||
self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscriptions WHERE user_id = %s)',
|
self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscriptions WHERE user_id = %s)',
|
||||||
[user_id])
|
[user_id])
|
||||||
return self.cur.fetchall()
|
return self.cur.fetchall()
|
||||||
|
|
||||||
def find_feed_items(self, feed_id: int) -> list[psycopg2.extras.DictRow]:
|
def find_feed_items(self, feed_id: int) -> list[DictRow]:
|
||||||
"""Get last feed items."""
|
"""Get last feed items."""
|
||||||
self.log.debug('find_feed_items(feed_id=\'%s\')', feed_id)
|
self.log.debug('find_feed_items(feed_id=\'%s\')', feed_id)
|
||||||
self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = %s', [feed_id])
|
self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = %s', [feed_id])
|
||||||
|
@ -166,19 +167,16 @@ class Database:
|
||||||
self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id SERIAL PRIMARY KEY, url TEXT NOT NULL UNIQUE)')
|
self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id SERIAL PRIMARY KEY, url TEXT NOT NULL UNIQUE)')
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
'CREATE TABLE IF NOT EXISTS subscriptions ('
|
'CREATE TABLE IF NOT EXISTS subscriptions ('
|
||||||
' user_id INTEGER,'
|
' user_id INTEGER REFERENCES users,'
|
||||||
' feed_id INTEGER,'
|
' feed_id INTEGER REFERENCES feeds,'
|
||||||
' UNIQUE (user_id, feed_id),'
|
' UNIQUE (user_id, feed_id)'
|
||||||
' FOREIGN KEY(user_id) REFERENCES users(id),'
|
|
||||||
' FOREIGN KEY(feed_id) REFERENCES feeds(id)'
|
|
||||||
')'
|
')'
|
||||||
)
|
)
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
'CREATE TABLE IF NOT EXISTS feeds_last_items ('
|
'CREATE TABLE IF NOT EXISTS feeds_last_items ('
|
||||||
' feed_id INTEGER,'
|
' feed_id INTEGER REFERENCES feeds ON DELETE CASCADE,'
|
||||||
' url TEXT NOT NULL,'
|
' url TEXT NOT NULL,'
|
||||||
' title TEXT,'
|
' title TEXT,'
|
||||||
' description TEXT,'
|
' description TEXT'
|
||||||
' FOREIGN KEY(feed_id) REFERENCES feeds(id)'
|
|
||||||
')'
|
')'
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue