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

Commit

Permalink
Add subject routes
Browse files Browse the repository at this point in the history
  • Loading branch information
msathieu committed Mar 3, 2024
1 parent e317fa4 commit 54b6e4c
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 9 deletions.
2 changes: 2 additions & 0 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import uvicorn
from fastapi import FastAPI

from routes.subjects import subjects_router
from routes.users import users_router

app = FastAPI()

# Koppel routes uit andere modules.
app.include_router(subjects_router, prefix="/api")
app.include_router(users_router, prefix="/api")

if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion backend/db/interface/AdminDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from db.interface.AbstractDAO import AbstractDAO

if TYPE_CHECKING:
from db.models.models import Admin # noqa: F401
from domain.models.AdminDataclass import AdminDataclass


class AdminDAO(AbstractDAO):
class AdminDAO(AbstractDAO["Admin", "AdminDataclass"]):
@abstractmethod
def create_admin(self, name: str, email: str) -> "AdminDataclass":
"""
Expand Down
3 changes: 2 additions & 1 deletion backend/db/interface/GroupDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from db.interface.AbstractDAO import AbstractDAO

if TYPE_CHECKING:
from db.models.models import Group # noqa: F401
from domain.models.GroupDataclass import GroupDataclass
from domain.models.StudentDataclass import StudentDataclass


class GroupDAO(AbstractDAO):
class GroupDAO(AbstractDAO["Group", "GroupDataclass"]):
@abstractmethod
def create_group(self, project_id: int) -> "GroupDataclass":
"""
Expand Down
3 changes: 2 additions & 1 deletion backend/db/interface/ProjectDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from db.interface.AbstractDAO import AbstractDAO

if TYPE_CHECKING:
from db.models.models import Project # noqa: F401
from domain.models.ProjectDataclass import ProjectDataclass


class ProjectDAO(AbstractDAO):
class ProjectDAO(AbstractDAO["Project", "ProjectDataclass"]):
@abstractmethod
def create_project(
self,
Expand Down
3 changes: 2 additions & 1 deletion backend/db/interface/StudentDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from db.interface.AbstractDAO import AbstractDAO

if TYPE_CHECKING:
from db.models.models import Student # noqa: F401
from domain.models.StudentDataclass import StudentDataclass


class StudentDAO(AbstractDAO):
class StudentDAO(AbstractDAO["Student", "StudentDataclass"]):
@abstractmethod
def create_student(self, name: str, email: str) -> "StudentDataclass":
"""
Expand Down
3 changes: 2 additions & 1 deletion backend/db/interface/SubjectDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from db.interface.AbstractDAO import AbstractDAO

if TYPE_CHECKING:
from db.models.models import Subject # noqa: F401
from domain.models.SubjectDataclass import SubjectDataclass


class SubjectDAO(AbstractDAO):
class SubjectDAO(AbstractDAO["Subject", "SubjectDataclass"]):
@abstractmethod
def create_subject(self, name: str) -> "SubjectDataclass":
"""
Expand Down
10 changes: 8 additions & 2 deletions backend/db/interface/SubmissionDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
from db.interface.AbstractDAO import AbstractDAO

if TYPE_CHECKING:
from db.models.models import Submission # noqa: F401
from domain.models.SubmissionDataclass import SubmissionDataclass, SubmissionState


class SubmissionDAO(AbstractDAO):
class SubmissionDAO(AbstractDAO["Submission", "SubmissionDataclass"]):
@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
3 changes: 2 additions & 1 deletion backend/db/interface/TeacherDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from db.interface.AbstractDAO import AbstractDAO

if TYPE_CHECKING:
from db.models.models import Teacher # noqa: F401
from domain.models.TeacherDataclass import TeacherDataclass


class TeacherDAO(AbstractDAO):
class TeacherDAO(AbstractDAO["Teacher", "TeacherDataclass"]):
@abstractmethod
def create_teacher(self, name: str, email: str) -> "TeacherDataclass":
"""
Expand Down
8 changes: 7 additions & 1 deletion backend/db/interface/UserDAO.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from typing import TYPE_CHECKING

from db.interface.AbstractDAO import AbstractDAO

if TYPE_CHECKING:
from db.models.models import User # noqa: F401
from domain.models.UserDataclass import UserDataclass # noqa: F401


class UserDAO(AbstractDAO):
class UserDAO(AbstractDAO["User", "UserDataclass"]):
pass
13 changes: 13 additions & 0 deletions backend/domain/models/SubjectDataclass.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
from pydantic import BaseModel

from db.interface.DAOProvider import DAOProvider
from domain.models.UserDataclass import UserDataclass


class SubjectDataclass(BaseModel):
id: int
name: str

def is_user_authorized(self, user: UserDataclass, dao_provider: DAOProvider) -> bool:
teacher_dao = dao_provider.get_teacher_dao()
student_dao = dao_provider.get_student_dao()
subject_dao = dao_provider.get_subject_dao()
if teacher_dao.is_user_teacher(user.id) and self in subject_dao.get_subjects_of_teacher(user.id):
return True
if student_dao.is_user_student(user.id) and self in subject_dao.get_subjects_of_student(user.id):
return True
return False
43 changes: 43 additions & 0 deletions backend/routes/subjects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from fastapi import APIRouter, HTTPException

from db.errors.database_errors import ItemNotFoundError
from domain.models.ProjectDataclass import ProjectDataclass
from domain.models.SubjectDataclass import SubjectDataclass
from routes.db import get_dao_provider
from routes.login import get_authenticated_user

subjects_router = APIRouter()


@subjects_router.get("/subjects")
def get_subjects(teacher: bool = False) -> list[SubjectDataclass]:
user = get_authenticated_user()
subject_dao = get_dao_provider().get_subject_dao()
try:
if teacher:
return subject_dao.get_subjects_of_teacher(user.id)
return subject_dao.get_subjects_of_student(user.id)
except ItemNotFoundError as err:
raise HTTPException(status_code=404) from err


@subjects_router.get("/subjects/{subject_id}")
def get_subject(subject_id: int) -> SubjectDataclass:
subject_dao = get_dao_provider().get_subject_dao()
try:
return subject_dao.get(subject_id)
except ItemNotFoundError as err:
raise HTTPException(status_code=404) from err


@subjects_router.get("/subjects/{subject_id}/projects")
def get_subject_projects(subject_id: int) -> list[ProjectDataclass]:
subject_dao = get_dao_provider().get_subject_dao()
project_dao = get_dao_provider().get_project_dao()
try:
subject = subject_dao.get(subject_id)
if not subject.is_user_authorized(get_authenticated_user(), get_dao_provider()):
raise HTTPException(status_code=403)
return project_dao.get_projects_of_subject(subject_id)
except ItemNotFoundError as err:
raise HTTPException(status_code=404) from err

0 comments on commit 54b6e4c

Please sign in to comment.