initial commit
This commit is contained in:
8
database/Subscriber.py
Normal file
8
database/Subscriber.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Subscriber:
|
||||
id: int
|
||||
user_id: int
|
||||
since: str
|
40
database/controller.py
Normal file
40
database/controller.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import sqlite3
|
||||
from database.Subscriber import Subscriber
|
||||
|
||||
db = sqlite3.connect("database.db")
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self, filename: str) -> None:
|
||||
db = sqlite3.connect(filename)
|
||||
|
||||
def add_subscriber(self, user_id=int):
|
||||
cursor = db.cursor()
|
||||
cursor.execute("SELECT user_id FROM subscribers WHERE user_id = ?", (user_id,))
|
||||
result = cursor.fetchone()
|
||||
if result:
|
||||
return False
|
||||
else:
|
||||
cursor.execute(
|
||||
"INSERT INTO subscribers (user_id, since) VALUES (?, DATETIME('now'))",
|
||||
(user_id,),
|
||||
)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
def get_subscribers(self):
|
||||
cursor = db.cursor()
|
||||
cursor.execute("SELECT user_id FROM subscribers")
|
||||
result = cursor.fetchall()
|
||||
return [Subscriber(id=row[0], user_id=row[1], since=row[2]) for row in result]
|
||||
|
||||
def remove_subscriber(self, user_id=int):
|
||||
cursor = db.cursor()
|
||||
cursor.execute("SELECT user_id FROM subscribers WHERE user_id = ?", (user_id,))
|
||||
result = cursor.fetchone()
|
||||
if result:
|
||||
cursor.execute("DELETE FROM subscribers WHERE user_id = ?", (user_id,))
|
||||
db.commit()
|
||||
return True
|
||||
else:
|
||||
return False
|
Reference in New Issue
Block a user