[rewrite] Updated backend structure a bit and started working on registration

This commit is contained in:
Thastertyn committed 2025-02-23 21:14:23 +01:00
1 parent f0c378b20f
commit 5d12391635
17 files changed
+832 -86

No files matched your search

+14 -19
View File
@@ -4,10 +4,8 @@ from contextlib import contextmanager
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy import create_engine, text
from sqlalchemy.exc import DatabaseError as SqlAlchemyDatabaseError
from app.database.exceptions import DatabaseError
from app.models.base_model import Base
class DatabaseManager():
@@ -19,37 +17,34 @@ class DatabaseManager():
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self) -> None:
if hasattr(self, "engine"):
return
def __init__(self, mysql_user: str, mysql_password: str, mysql_host: str, mysql_port: str, mysql_db_name: str) -> None:
self.logger = logging.getLogger(__name__)
self.logger.info("Initializing Database")
self.engine = create_engine('sqlite:///bank.db')
self.Session = sessionmaker(bind=self.engine)
self.create_tables()
def create_tables(self):
self.logger.debug("Creating tables")
Base.metadata.create_all(self.engine)
self.logger.info("Reading database config")
self.engine = create_engine('mysql+mysqlconnector://%s:%s@%s:%s/%s' % (
mysql_user,
mysql_password,
mysql_host,
mysql_port,
mysql_db_name),
pool_pre_ping=True)
self.test_connection()
self.Session = sessionmaker(bind=self.engine) # NOSONAR
def cleanup(self) -> None:
self.logger.debug("Closing connection")
self.engine.dispose()
def test_connection(self) -> bool:
def test_connection(self):
self.logger.debug("Testing database connection")
try:
with self.engine.connect() as connection:
connection.execute(text("select 1"))
self.logger.debug("Database connection successful")
return True
except SqlAlchemyDatabaseError as e:
except DatabaseError as e:
self.logger.critical("Database connection failed: %s", e)
raise DatabaseError("Database connection failed", DatabaseError.CONNECTION_ERROR) from e
return False
@classmethod
@contextmanager