From 8d5e69ef7ea81859fe7fefdee4796cf47cda95f4 Mon Sep 17 00:00:00 2001 From: ALBERICLOOS Date: Sun, 25 Feb 2024 15:21:47 +0100 Subject: [PATCH] toevoegen van SubmissionsDAO --- backend/db/implementation/SqlGroupDAO.py | 2 +- backend/db/implementation/SqlSubmissionDAO.py | 48 +++++++++++++++++++ backend/db/interface/SubmissionDAO.py | 8 ++-- 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 backend/db/implementation/SqlSubmissionDAO.py diff --git a/backend/db/implementation/SqlGroupDAO.py b/backend/db/implementation/SqlGroupDAO.py index cacd9716..1e940df6 100644 --- a/backend/db/implementation/SqlGroupDAO.py +++ b/backend/db/implementation/SqlGroupDAO.py @@ -12,7 +12,7 @@ def create_group(self, group: GroupDataclass, project_id: int): raise ItemNotFoundError(f"Het project met id {project_id} kon niet in de databank gevonden worden") new_group: Group = Group() new_group.project_id = project_id - + new_group.project = project db.session.add(new_group) db.session.commit() diff --git a/backend/db/implementation/SqlSubmissionDAO.py b/backend/db/implementation/SqlSubmissionDAO.py new file mode 100644 index 00000000..36d4426b --- /dev/null +++ b/backend/db/implementation/SqlSubmissionDAO.py @@ -0,0 +1,48 @@ +from typing import List + +from db.errors.database_errors import ItemNotFoundError, UniqueConstraintError +from db.extensions import db +from db.interface.SubmissionDAO import SubmissionDAO +from db.models.models import Student, Subject, Teacher, Project, Group, Submission +from domain.models.models import SubjectDataclass, SubmissionDataclass + + +class SqlSubmissionDAO(SubmissionDAO): + def create_submission(self, submission: SubmissionDataclass, student_id: int, group_id: int): + student = Student.query.get(student_id) + group = Group.query.get(group_id) + if not student: + raise ItemNotFoundError(f"student met id {student_id} kon niet in de databank gevonden worden") + if not group: + raise ItemNotFoundError(f"group met id {group_id} kon niet in de databank gevonden worden") + new_submission: Submission = Submission() + new_submission.group_id = group_id + new_submission.group = group + new_submission.student_id = student_id + new_submission.student = student + new_submission.date_time = submission.date_time + new_submission.state = submission.state + new_submission.message = submission.message + + db.session.add(new_submission) + db.session.commit() + + def get_submission(self, submission_id: int) -> SubmissionDataclass: + submission = Submission.query.get(submission_id) + if not submission: + raise ItemNotFoundError(f"submission met id {submission_id} kon niet in de databank gevonden worden") + return submission + + def get_submissions_student(self, student_id: int) -> list[SubmissionDataclass]: + student = Student.query.get(student_id) + if not student: + raise ItemNotFoundError(f"student met id {student_id} kon niet in de databank gevonden worden") + submissions: list[SubmissionDataclass] = student.submissions + return submissions + + def get_submissions_group(self, group_id: int) -> list[SubmissionDataclass]: + group = Group.query.get(group_id) + if not group: + raise ItemNotFoundError(f"group met id {group_id} kon niet in de databank gevonden worden") + submissions: list[SubmissionDataclass] = group.submissions + return submissions diff --git a/backend/db/interface/SubmissionDAO.py b/backend/db/interface/SubmissionDAO.py index f6496598..a120a72b 100644 --- a/backend/db/interface/SubmissionDAO.py +++ b/backend/db/interface/SubmissionDAO.py @@ -5,15 +5,15 @@ class SubmissionDAO(ABC): @abstractmethod - def create_submission(self, submission: SubmissionDataclass, project_id: int, group_id: int): + def create_submission(self, submission: SubmissionDataclass, student_id: int, group_id: int): """ - Creëert een nieuw SubmissionDataClass in de database en associeert het met een ProjectDataClass en een + Creëert een nieuw SubmissionDataClass in de database en associeert het met een StudentDataclass en een GroupDataClass. :param submission: De SubmissionDataClass domeinmodel-instantie die aan de database moet worden toegevoegd. - :param project_id: De identificatie van de ProjectDataClass waarmee het SubmissionDataClass geassocieerd wordt. + :param student_id: De identificatie van de StudentDataclass waarmee het SubmissionDataClass geassocieerd wordt. :param group_id: De identificatie van de GroupDataClass waarmee het SubmissionDataClass geassocieerd wordt - :raises: ItemNotFoundException: Als er geen ProjectDataClass of GroupDataClass met de opgegeven `project_id` of + :raises: ItemNotFoundException: Als er geen StudentDataclass of GroupDataClass met de opgegeven `student_id` of `group_id` in de database is. """ raise NotImplementedError()