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

Commit

Permalink
toevoegen van implementatie SqlGroupDAO
Browse files Browse the repository at this point in the history
  • Loading branch information
ALBERICLOOS committed Feb 25, 2024
1 parent 4dac07d commit f9ab3d5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
57 changes: 57 additions & 0 deletions backend/db/implementation/SqlGroupDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from db.errors.database_errors import ItemNotFoundError, UniqueConstraintError
from db.extensions import db
from db.interface.GroupDAO import GroupDAO
from db.models.models import Group, Project, Student
from domain.models.models import GroupDataclass, StudentDataclass


class SqlGroupDAO(GroupDAO):
def create_group(self, group: GroupDataclass, project_id: int):
project = Project.query.get(project_id)
if not project:
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

db.session.add(new_group)
db.session.commit()

group.id = new_group.id

def get_group(self, group_id: int) -> GroupDataclass:
group = Group.query.get(group_id)
if not group:
raise ItemNotFoundError(f"De groep met id {group_id} kon niet in de databank gevonden worden")
return group

def get_groups_project(self, project_id: int) -> list[GroupDataclass]:
project = Project.query.get(project_id)
if not project:
raise ItemNotFoundError(f"Het project met id {project_id} kon niet in de databank gevonden worden")
groups: list[Group] = project.groups
return groups

def get_groups_student(self, student_id: int) -> list[GroupDataclass]:
student = Student.query.get(student_id)
if not student:
raise ItemNotFoundError(f"De student met id {student_id} kon niet in de databank gevonden worden")
groups: list[Group] = student.groups
return groups

def add_student_group(self, student_id: int, group_id: int):
student = Student.query.get(student_id)
group = Group.query.get(group_id)
if not student:
raise ItemNotFoundError(f"De student met id {student_id} kon niet in de databank gevonden worden")
if not group:
raise ItemNotFoundError(f"De group met id {group_id} kon niet in de databank gevonden worden")
if student in group.students:
raise UniqueConstraintError(f"De student met id {student_id} zit al in de groep met id {group_id}")

group.students.append(student)

def get_students_group(self, group_id: int) -> list[StudentDataclass]:
group = Group.query.get(group_id)
if not group:
raise ItemNotFoundError(f"De group met id {group_id} kon niet in de databank gevonden worden")
return group.students
8 changes: 4 additions & 4 deletions backend/db/implementation/SqlProjectDAO.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from db.errors.database_errors import ItemNotFoundError, UniqueConstraintError
from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.ProjectDAO import ProjectDAO
from db.models.models import Student, Subject, Teacher, Project
from domain.models.models import SubjectDataclass, ProjectDataclass
from db.models.models import Project, Subject
from domain.models.models import ProjectDataclass


class SqlProjectDAO(ProjectDAO):
Expand Down Expand Up @@ -35,4 +35,4 @@ def get_projects(self, subject_id: int) -> list[ProjectDataclass]:
if not subject:
raise ItemNotFoundError(f"Het subject met id {subject_id} kon niet in de databank gevonden worden")
projects: list[Project] = subject.projects
return [project.name for project in projects]
return [project.name for project in projects]
22 changes: 21 additions & 1 deletion backend/db/interface/GroupDAO.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod

from domain.models.models import GroupDataclass
from domain.models.models import GroupDataclass, StudentDataclass


class GroupDAO(ABC):
Expand Down Expand Up @@ -45,3 +45,23 @@ def get_groups_student(self, student_id: int) -> list[GroupDataclass]:
:return: Een lijst van groepen die bij een bepaald student horen.
"""
raise NotImplementedError()

@abstractmethod
def add_student_group(self, student_id: int, group_id: int):
"""
Gaat een student toevoegen aan een groep
:param student_id: De student die aan de groep moet toegevoegd worden.
:param group_id: De groep waaraan de student moet toegevoegd worden.
:raises ItemNotFoundException: Als er geen group/student met gegeven id in de databank zit.
"""
raise NotImplementedError()

def get_students_group(self, group_id: int) -> list[StudentDataclass]:
"""
Gaat alle studenten geven die in een bepaalde groep zitten
:param group_id: De groep waarvan de studenten worden opgeroepen
:raises ItemNotFoundException: Als er geen group met gegeven id in de databank zit.
"""
raise NotImplementedError()

0 comments on commit f9ab3d5

Please sign in to comment.