From a46d29b94f95fdc5fc59375eb09bf6101f154e15 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Sun, 3 Mar 2024 11:10:37 +0100 Subject: [PATCH] Add DAOProvider --- backend/db/implementation/SqlDAOProvider.py | 43 ++++++++++++++++++++ backend/db/interface/DAOProvider.py | 44 +++++++++++++++++++++ backend/db/interface/SubmissionDAO.py | 2 +- backend/domain/models/APIUser.py | 15 +++---- backend/routes/db.py | 6 +++ backend/routes/login.py | 13 +++--- backend/routes/users.py | 28 +++++++------ 7 files changed, 120 insertions(+), 31 deletions(-) create mode 100644 backend/db/implementation/SqlDAOProvider.py create mode 100644 backend/db/interface/DAOProvider.py create mode 100644 backend/routes/db.py diff --git a/backend/db/implementation/SqlDAOProvider.py b/backend/db/implementation/SqlDAOProvider.py new file mode 100644 index 00000000..e8b2cb9f --- /dev/null +++ b/backend/db/implementation/SqlDAOProvider.py @@ -0,0 +1,43 @@ +from db.implementation.SqlAdminDAO import SqlAdminDAO +from db.implementation.SqlGroupDAO import SqlGroupDAO +from db.implementation.SqlProjectDAO import SqlProjectDAO +from db.implementation.SqlStudentDAO import SqlStudentDAO +from db.implementation.SqlSubjectDAO import SqlSubjectDAO +from db.implementation.SqlSubmissionDAO import SqlSubmissionDAO +from db.implementation.SqlTeacherDAO import SqlTeacherDAO +from db.implementation.SqlUserDAO import SqlUserDAO +from db.interface.AdminDAO import AdminDAO +from db.interface.DAOProvider import DAOProvider +from db.interface.GroupDAO import GroupDAO +from db.interface.ProjectDAO import ProjectDAO +from db.interface.StudentDAO import StudentDAO +from db.interface.SubjectDAO import SubjectDAO +from db.interface.SubmissionDAO import SubmissionDAO +from db.interface.TeacherDAO import TeacherDAO +from db.interface.UserDAO import UserDAO + + +class SqlDAOProvider(DAOProvider): + def get_admin_dao(self) -> AdminDAO: + return SqlAdminDAO() + + def get_group_dao(self) -> GroupDAO: + return SqlGroupDAO() + + def get_project_dao(self) -> ProjectDAO: + return SqlProjectDAO() + + def get_student_dao(self) -> StudentDAO: + return SqlStudentDAO() + + def get_subject_dao(self) -> SubjectDAO: + return SqlSubjectDAO() + + def get_submission_dao(self) -> SubmissionDAO: + return SqlSubmissionDAO() + + def get_teacher_dao(self) -> TeacherDAO: + return SqlTeacherDAO() + + def get_user_dao(self) -> UserDAO: + return SqlUserDAO() diff --git a/backend/db/interface/DAOProvider.py b/backend/db/interface/DAOProvider.py new file mode 100644 index 00000000..6ebe43c5 --- /dev/null +++ b/backend/db/interface/DAOProvider.py @@ -0,0 +1,44 @@ +from abc import ABC, abstractmethod + +from db.interface.AdminDAO import AdminDAO +from db.interface.GroupDAO import GroupDAO +from db.interface.ProjectDAO import ProjectDAO +from db.interface.StudentDAO import StudentDAO +from db.interface.SubjectDAO import SubjectDAO +from db.interface.SubmissionDAO import SubmissionDAO +from db.interface.TeacherDAO import TeacherDAO +from db.interface.UserDAO import UserDAO + + +class DAOProvider(ABC): + @abstractmethod + def get_admin_dao(self) -> AdminDAO: + raise NotImplementedError + + @abstractmethod + def get_group_dao(self) -> GroupDAO: + raise NotImplementedError + + @abstractmethod + def get_project_dao(self) -> ProjectDAO: + raise NotImplementedError + + @abstractmethod + def get_student_dao(self) -> StudentDAO: + raise NotImplementedError + + @abstractmethod + def get_subject_dao(self) -> SubjectDAO: + raise NotImplementedError + + @abstractmethod + def get_submission_dao(self) -> SubmissionDAO: + raise NotImplementedError + + @abstractmethod + def get_teacher_dao(self) -> TeacherDAO: + raise NotImplementedError + + @abstractmethod + def get_user_dao(self) -> UserDAO: + raise NotImplementedError diff --git a/backend/db/interface/SubmissionDAO.py b/backend/db/interface/SubmissionDAO.py index da61d4bc..58f2c946 100644 --- a/backend/db/interface/SubmissionDAO.py +++ b/backend/db/interface/SubmissionDAO.py @@ -11,7 +11,7 @@ class SubmissionDAO(AbstractDAO): @abstractmethod def create_submission( - self, student_id: int, group_id: int, message: str, state: "SubmissionState", date_time: datetime + self, student_id: int, group_id: int, message: str, state: "SubmissionState", date_time: datetime, ) -> "SubmissionDataclass": """ Creƫert een nieuw SubmissionDataClass in de database en associeert het met een StudentDataclass en een diff --git a/backend/domain/models/APIUser.py b/backend/domain/models/APIUser.py index 716ea3d1..85f34c0f 100644 --- a/backend/domain/models/APIUser.py +++ b/backend/domain/models/APIUser.py @@ -1,8 +1,6 @@ from pydantic import BaseModel, EmailStr -from db.implementation.SqlAdminDAO import SqlAdminDAO -from db.implementation.SqlStudentDAO import SqlStudentDAO -from db.implementation.SqlTeacherDAO import SqlTeacherDAO +from db.interface.DAOProvider import DAOProvider from domain.models.UserDataclass import UserDataclass @@ -13,15 +11,12 @@ class APIUser(BaseModel): roles: list[str] -def convert_user(user: UserDataclass) -> APIUser: +def convert_user(user: UserDataclass, dao_provider: DAOProvider) -> APIUser: result = APIUser(id=user.id, name=user.name, email=user.email, roles=[]) - teacher_dao = SqlTeacherDAO() - admin_dao = SqlAdminDAO() - student_dao = SqlStudentDAO() - if teacher_dao.is_user_teacher(user.id): + if dao_provider.get_teacher_dao().is_user_teacher(user.id): result.roles.append("teacher") - if admin_dao.is_user_admin(user.id): + if dao_provider.get_admin_dao().is_user_admin(user.id): result.roles.append("admin") - if student_dao.is_user_student(user.id): + if dao_provider.get_student_dao().is_user_student(user.id): result.roles.append("student") return result diff --git a/backend/routes/db.py b/backend/routes/db.py new file mode 100644 index 00000000..584aedad --- /dev/null +++ b/backend/routes/db.py @@ -0,0 +1,6 @@ +from db.implementation.SqlDAOProvider import SqlDAOProvider +from db.interface.DAOProvider import DAOProvider + + +def get_dao_provider() -> DAOProvider: + return SqlDAOProvider() diff --git a/backend/routes/login.py b/backend/routes/login.py index 5661d40b..d2e32ced 100644 --- a/backend/routes/login.py +++ b/backend/routes/login.py @@ -1,13 +1,12 @@ -from fastapi import Depends - -from db.implementation.SqlAdminDAO import SqlAdminDAO -from db.implementation.SqlUserDAO import SqlUserDAO from domain.models.UserDataclass import UserDataclass +from routes.db import get_dao_provider def get_authenticated_user() -> UserDataclass: - return SqlUserDAO().get(1) # Actually authenticate user + return get_dao_provider().get_user_dao().get(1) # Actually authenticate user -def is_user_admin(user: UserDataclass = Depends(get_authenticated_user)) -> bool: - return SqlAdminDAO().is_user_admin(user.id) +def is_user_admin() -> bool: + user = get_authenticated_user() + admin_dao = get_dao_provider().get_admin_dao() + return admin_dao.is_user_admin(user.id) diff --git a/backend/routes/users.py b/backend/routes/users.py index 06bc3479..2a247b51 100644 --- a/backend/routes/users.py +++ b/backend/routes/users.py @@ -1,33 +1,35 @@ -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, HTTPException from db.errors.database_errors import ItemNotFoundError -from db.implementation.SqlUserDAO import SqlUserDAO from domain.models.APIUser import APIUser, convert_user -from domain.models.UserDataclass import UserDataclass +from routes.db import get_dao_provider from routes.login import get_authenticated_user, is_user_admin users_router = APIRouter() @users_router.get("/user") -def get_current_user(user: UserDataclass = Depends(get_authenticated_user)) -> APIUser: - return convert_user(user) +def get_current_user() -> APIUser: + user = get_authenticated_user() + return convert_user(user, get_dao_provider()) @users_router.get("/users") -def get_users(admin: bool = Depends(is_user_admin)) -> list[APIUser]: - if not admin: +def get_users() -> list[APIUser]: + if not is_user_admin(): raise HTTPException(status_code=403) - users = SqlUserDAO().get_all() - return [convert_user(user) for user in users] + user_dao = get_dao_provider().get_user_dao() + users = user_dao.get_all() + return [convert_user(user, get_dao_provider()) for user in users] @users_router.get("/users/{uid}") -def get_user(uid: int, admin: bool = Depends(is_user_admin)) -> APIUser: - if not admin: +def get_user(uid: int) -> APIUser: + if not is_user_admin(): raise HTTPException(status_code=403) + user_dao = get_dao_provider().get_user_dao() try: - user = SqlUserDAO().get(uid) + user = user_dao.get(uid) except ItemNotFoundError as err: raise HTTPException(status_code=404) from err - return convert_user(user) + return convert_user(user, get_dao_provider())