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

Commit

Permalink
Add more routes
Browse files Browse the repository at this point in the history
  • Loading branch information
msathieu committed Mar 10, 2024
1 parent 7e6363a commit 1bc9b94
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 21 deletions.
28 changes: 18 additions & 10 deletions backend/domain/logic/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

from sqlalchemy.orm import Session

from db.models.models import Project, Subject
from db.models.models import Project, Student, Subject
from domain.logic.basic_operations import get, get_all
from domain.models.ProjectDataclass import ProjectDataclass


def create_project(
session: Session,
subject_id: int,
name: str,
deadline: datetime,
archived: bool,
description: str,
requirements: str,
visible: bool,
max_students: int,
session: Session,
subject_id: int,
name: str,
deadline: datetime,
archived: bool,
description: str,
requirements: str,
visible: bool,
max_students: int,
) -> ProjectDataclass:
subject: Subject = get(session, Subject, subject_id)

Expand Down Expand Up @@ -49,3 +49,11 @@ def get_projects_of_subject(session: Session, subject_id: int) -> list[ProjectDa
projects: list[Project] = subject.projects
return [project.to_domain_model() for project in projects]


def get_projects_of_student(session: Session, user_id: int) -> list[ProjectDataclass]:
student = get(session, Student, ident=user_id)
subjects = student.subjects
projects = []
for i in subjects:
projects += i.projects
return [project.to_domain_model() for project in projects]
10 changes: 10 additions & 0 deletions backend/domain/models/ProjectDataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ class ProjectDataclass(BaseModel):
visible: bool
max_students: PositiveInt
subject_id: int


class ProjectInput(BaseModel):
name: str
deadline: datetime
archived: bool
description: str
requirements: str
visible: bool
max_students: PositiveInt
11 changes: 11 additions & 0 deletions backend/routes/dependencies/role_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,14 @@ def get_authenticated_student_for_subject(
if subject_id not in [subject.id for subject in subjects_of_student]:
raise NoAccessToSubjectError
return student


def get_authenticated_teacher_for_subject(
subject_id: int,
session: Session = Depends(get_session),
teacher: TeacherDataclass = Depends(get_authenticated_teacher),
) -> TeacherDataclass:
subjects_of_teacher = get_subjects_of_teacher(session, teacher.id)
if subject_id not in [subject.id for subject in subjects_of_teacher]:
raise NoAccessToSubjectError
return teacher
14 changes: 12 additions & 2 deletions backend/routes/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from sqlalchemy.orm import Session

from db.sessions import get_session
from domain.logic.project import get_projects_of_student
from domain.logic.subject import get_subjects_of_student
from domain.models.ProjectDataclass import ProjectDataclass
from domain.models.StudentDataclass import StudentDataclass
from domain.models.SubjectDataclass import SubjectDataclass
from routes.dependencies.role_dependencies import get_authenticated_student
Expand All @@ -12,7 +14,15 @@

@student_router.get("/student/subjects")
def subjects_of_student_get(
session: Session = Depends(get_session),
student: StudentDataclass = Depends(get_authenticated_student),
session: Session = Depends(get_session),
student: StudentDataclass = Depends(get_authenticated_student),
) -> list[SubjectDataclass]:
return get_subjects_of_student(session, student.id)


@student_router.get("/student/projects")
def projects_of_student_get(
session: Session = Depends(get_session),
student: StudentDataclass = Depends(get_authenticated_student),
) -> list[ProjectDataclass]:
return get_projects_of_student(session, student.id)
24 changes: 22 additions & 2 deletions backend/routes/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from sqlalchemy.orm import Session

from db.sessions import get_session
from domain.logic.project import get_projects_of_subject
from domain.logic.project import create_project, get_projects_of_subject
from domain.logic.subject import get_subject
from domain.models.ProjectDataclass import ProjectDataclass
from domain.models.ProjectDataclass import ProjectDataclass, ProjectInput
from domain.models.SubjectDataclass import SubjectDataclass
from routes.dependencies.role_dependencies import (
ensure_user_authorized_for_subject,
get_authenticated_teacher_for_subject,
get_authenticated_user,
)

Expand All @@ -22,3 +23,22 @@ def subject_get(subject_id: int, session: Session = Depends(get_session)) -> Sub
@subject_router.get("/subjects/{subject_id}/projects", dependencies=[Depends(ensure_user_authorized_for_subject)])
def get_subject_projects(subject_id: int, session: Session = Depends(get_session)) -> list[ProjectDataclass]:
return get_projects_of_subject(session, subject_id)


@subject_router.post("/subjects/{subject_id}/projects", dependencies=[Depends(get_authenticated_teacher_for_subject)])
def new_project(
subject_id: int,
project: ProjectInput,
session: Session = Depends(get_session),
) -> ProjectDataclass:
return create_project(
session,
subject_id=subject_id,
name=project.name,
deadline=project.deadline,
archived=project.archived,
description=project.description,
requirements=project.requirements,
visible=project.visible,
max_students=project.max_students,
)
15 changes: 8 additions & 7 deletions backend/routes/teacher.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from fastapi import APIRouter, Depends, status
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from starlette.responses import Response

from db.sessions import get_session
from domain.logic.subject import create_subject, get_subjects_of_teacher
from domain.logic.subject import add_teacher_to_subject, create_subject, get_subjects_of_teacher
from domain.models.SubjectDataclass import SubjectDataclass, SubjectInput
from domain.models.TeacherDataclass import TeacherDataclass
from routes.dependencies.role_dependencies import get_authenticated_teacher
Expand All @@ -19,10 +18,12 @@ def subjects_of_teacher_get(
return get_subjects_of_teacher(session, teacher.id)


@teacher_router.post("/teacher/subjects", dependencies=[Depends(get_authenticated_teacher)])
@teacher_router.post("/teacher/subjects")
def create_subject_post(
subject: SubjectInput,
teacher: TeacherDataclass = Depends(get_authenticated_teacher),
session: Session = Depends(get_session),
) -> Response:
create_subject(session, name=subject.name)
return Response(status_code=status.HTTP_201_CREATED)
) -> SubjectDataclass:
new_subject = create_subject(session, name=subject.name)
add_teacher_to_subject(session, teacher_id=teacher.id, subject_id=new_subject.id)
return new_subject

0 comments on commit 1bc9b94

Please sign in to comment.