add connect method
This commit is contained in:
14
database.py
14
database.py
@@ -6,10 +6,18 @@ import jwt
|
||||
import datetime
|
||||
import security
|
||||
|
||||
|
||||
def connect() -> (sqlite3.Connection, sqlite3.Cursor):
|
||||
"""Connects to the database and returns the connection and cursor."""
|
||||
connection = sqlite3.connect('database.db')
|
||||
connection.row_factory = sqlite3.Row
|
||||
cursor = connection.cursor()
|
||||
|
||||
return connection, cursor
|
||||
|
||||
|
||||
connection, cursor = connect()
|
||||
|
||||
|
||||
def init() -> None:
|
||||
"""Initializes the database."""
|
||||
@@ -46,7 +54,7 @@ def register(user: User) -> None:
|
||||
cursor.execute(
|
||||
"INSERT INTO users (name, password) VALUES (?, ?)",
|
||||
(user.name,
|
||||
security.hash_password(user.password))
|
||||
security.hash_password(user.password)))
|
||||
connection.commit()
|
||||
|
||||
|
||||
@@ -65,9 +73,7 @@ def get_user_by_token(request: Request) -> User:
|
||||
algorithms=[
|
||||
settings.jwt_algorithm])
|
||||
|
||||
connection=sqlite3.connect('database.db')
|
||||
connection.row_factory=sqlite3.Row
|
||||
cursor=connection.cursor()
|
||||
connection, cursor = connect()
|
||||
cursor.execute(
|
||||
"SELECT id, name, password FROM users WHERE id = ?", (payload["id"],))
|
||||
row = cursor.fetchone()
|
||||
|
||||
Reference in New Issue
Block a user