diff --git a/src/database/member.py b/src/database/member.py index 8f84bb2..19cd104 100644 --- a/src/database/member.py +++ b/src/database/member.py @@ -1,9 +1,12 @@ -from sqlalchemy.exc import IntegrityError +from sqlalchemy.exc import IntegrityError, SQLAlchemyError from models.member import Member from utils.database import DatabaseManager +from utils.errors.database.database_error import DatabaseError +from utils.errors.database.duplicate_entry_error import DuplicateEntryError + def create_new_member(new_member: Member): try: with DatabaseManager.get_session() as session: @@ -12,3 +15,9 @@ def create_new_member(new_member: Member): except IntegrityError as e: print(e) session.rollback() + raise DuplicateEntryError from e + except SQLAlchemyError as e: + print(e) + session.rollback() + raise DatabaseError from e + diff --git a/src/ui/main_window_tabs/member_list/member_list.py b/src/ui/main_window_tabs/member_list/member_list.py index 38dc64a..739b163 100644 --- a/src/ui/main_window_tabs/member_list/member_list.py +++ b/src/ui/main_window_tabs/member_list/member_list.py @@ -11,7 +11,7 @@ from utils.database import DatabaseManager from ui.member_editor.member_editor import MemberEditor -class MemberDashboard(QWidget): +class MemberList(QWidget): def __init__(self): super().__init__() diff --git a/src/ui/member_editor/member_editor.py b/src/ui/member_editor/member_editor.py index 53089c0..11d3bba 100644 --- a/src/ui/member_editor/member_editor.py +++ b/src/ui/member_editor/member_editor.py @@ -1,17 +1,23 @@ +import logging + from PySide6.QtGui import QGuiApplication, QAction from PySide6.QtQml import QQmlApplicationEngine from PySide6 import QtWidgets, QtCore -from PySide6.QtWidgets import QVBoxLayout, QFormLayout, QLineEdit, QHBoxLayout, QPushButton, QDialog +from PySide6.QtWidgets import QVBoxLayout, QFormLayout, QLineEdit, QHBoxLayout, QPushButton, QDialog, QMessageBox from models.member import Member from database.member import create_new_member +from utils.errors.database.database_error import DatabaseError +from utils.errors.database.duplicate_entry_error import DuplicateEntryError + class MemberEditor(QDialog): def __init__(self, member: Member = None): super().__init__() + self.logger = logging.getLogger(__name__) self.create_layout() if member: @@ -75,7 +81,11 @@ class MemberEditor(QDialog): self.member.email = self.email_input.text() self.member.phone = self.phone_number_input.text() - if self.create_new: - create_new_member(self.member) + try: + if self.create_new: + self.logger.debug("Creating new member") + create_new_member(self.member) + except DuplicateEntryError: + QMessageBox.critical(None, "Details already in use", "Cannot create a new user", QMessageBox.StandardButton.Ok, QMessageBox.StandardButtons.NoButton) self.accept() \ No newline at end of file diff --git a/src/ui/window.py b/src/ui/window.py index 02c7554..1ce6715 100644 --- a/src/ui/window.py +++ b/src/ui/window.py @@ -5,7 +5,7 @@ from PySide6.QtWidgets import QMessageBox, QFileDialog from PySide6.QtCore import QStandardPaths from ui.dashboard.dashboard import LibraryDashboard -from ui.main_window_tabs.member_list.member_list import MemberDashboard +from ui.main_window_tabs.member_list.member_list import MemberList from ui.book_editor.book_editor import BookEditor from ui.member_editor.member_editor import MemberEditor @@ -40,7 +40,7 @@ class LibraryWindow(QtWidgets.QMainWindow): self.dashboard = LibraryDashboard() - self.member_list = MemberDashboard() + self.member_list = MemberList() central_widget.addTab(self.dashboard, "Dashboard") central_widget.addTab(self.member_list, "Members") diff --git a/src/utils/errors/database/duplicate_entry_error.py b/src/utils/errors/database/duplicate_entry_error.py new file mode 100644 index 0000000..1264108 --- /dev/null +++ b/src/utils/errors/database/duplicate_entry_error.py @@ -0,0 +1,6 @@ +from .database_error import DatabaseError + +class DuplicateEntryError(DatabaseError): + def __init__(self, message: str): + super().__init__(message) + self.message = message \ No newline at end of file