This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
85 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |