Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Add DAOProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
msathieu committed Mar 3, 2024
1 parent e2411e6 commit a46d29b
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 31 deletions.
43 changes: 43 additions & 0 deletions backend/db/implementation/SqlDAOProvider.py
Original file line number Diff line number Diff line change
@@ -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()
44 changes: 44 additions & 0 deletions backend/db/interface/DAOProvider.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion backend/db/interface/SubmissionDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions backend/domain/models/APIUser.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
6 changes: 6 additions & 0 deletions backend/routes/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from db.implementation.SqlDAOProvider import SqlDAOProvider
from db.interface.DAOProvider import DAOProvider


def get_dao_provider() -> DAOProvider:
return SqlDAOProvider()
13 changes: 6 additions & 7 deletions backend/routes/login.py
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 15 additions & 13 deletions backend/routes/users.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit a46d29b

Please sign in to comment.