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

Commit

Permalink
DAO's klaar
Browse files Browse the repository at this point in the history
  • Loading branch information
ALBERICLOOS committed Feb 26, 2024
1 parent 06ef03c commit b840cab
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 193 deletions.
2 changes: 1 addition & 1 deletion backend/db/implementation/SqlAdminDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_admin(self, ident: int) -> AdminDataclass:
return admin.to_domain_model()

def get_all_admins(self) -> list[AdminDataclass]:
admins: list[Admin] = db.session.scalars(select(Admin)).all()
admins: list[Admin] = list(db.session.scalars(select(Admin)).all())
return [admin.to_domain_model() for admin in admins]

def create_admin(self, name: str, email: str) -> None:
Expand Down
53 changes: 30 additions & 23 deletions backend/db/implementation/SqlGroupDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,64 @@
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
from domain.models.GroupDataclass import GroupDataclass
from domain.models.StudentDataclass import StudentDataclass


class SqlGroupDAO(GroupDAO):
def create_group(self, group: GroupDataclass, project_id: int):
project = Project.query.get(project_id)
def create_group(self, project_id: int) -> None:
project: Project | None = db.session.get(Project, ident=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(project_id=project_id, project=project, students=students, )
new_group.project_id = project_id
new_group.project = project
msg = f"Project with id {project} not found"
raise ItemNotFoundError(msg)
new_group: Group = Group(project_id=project_id, project=project)
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)
group: Group | None = db.session.get(Group, ident=group_id)
if not group:
raise ItemNotFoundError(f"De groep met id {group_id} kon niet in de databank gevonden worden")
msg = f"Group with id {group_id} not found"
raise ItemNotFoundError(msg)
return group.to_domain_model()

def get_groups_of_project(self, project_id: int) -> list[GroupDataclass]:
project = Project.query.get(project_id)
project: Project | None = db.session.get(Project, ident=project_id)
if not project:
raise ItemNotFoundError(f"Het project met id {project_id} kon niet in de databank gevonden worden")
msg = f"Project with id {project} not found"
raise ItemNotFoundError(msg)
groups: list[Group] = project.groups
return [group.to_domain_model() for group in groups]

def get_groups_of_student(self, student_id: int) -> list[GroupDataclass]:
student = Student.query.get(student_id)
student: Student | None = db.session.get(Student, ident=student_id)
if not student:
raise ItemNotFoundError(f"De student met id {student_id} kon niet in de databank gevonden worden")
msg = f"Student with id {student_id} not found"
raise ItemNotFoundError(msg)
groups: list[Group] = student.groups
return [group.to_domain_model() for group in groups]

def add_student_to_group(self, student_id: int, group_id: int):
student = Student.query.get(student_id)
group = Group.query.get(group_id)
def add_student_to_group(self, student_id: int, group_id: int) -> None:
student: Student | None = db.session.get(Student, ident=student_id)
group: Group | None = db.session.get(Group, ident=group_id)
if not student:
raise ItemNotFoundError(f"De student met id {student_id} kon niet in de databank gevonden worden")
msg = f"Student with id {student_id} not found"
raise ItemNotFoundError(msg)
if not group:
raise ItemNotFoundError(f"De group met id {group_id} kon niet in de databank gevonden worden")
msg = f"Group with id {group_id} not found"
raise ItemNotFoundError(msg)
if student in group.students:
raise UniqueConstraintError(f"De student met id {student_id} zit al in de groep met id {group_id}")
msg = f"Student with id {student_id} already in group with id {group_id}"
raise UniqueConstraintError(msg)

group.students.append(student)
db.session.add(group)
db.session.commit()

def get_students_of_group(self, group_id: int) -> list[StudentDataclass]:
group = Group.query.get(group_id)
group: Group | None = db.session.get(Group, ident=group_id)
if not group:
raise ItemNotFoundError(f"De group met id {group_id} kon niet in de databank gevonden worden")
msg = f"Group with id {group_id} not found"
raise ItemNotFoundError(msg)
students: list[Student] = group.students
return [student.to_domain_model() for student in students]
39 changes: 20 additions & 19 deletions backend/db/implementation/SqlProjectDAO.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
from datetime import datetime

from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.ProjectDAO import ProjectDAO
from db.models.models import Project, Subject
from domain.models.models import ProjectDataclass
from domain.models.ProjectDataclass import ProjectDataclass


class SqlProjectDAO(ProjectDAO):
def create_project(self, project: ProjectDataclass, subject_id: int):
subject = Subject.query.get(subject_id)
def create_project(self, subject_id: int, name: str, deadline: datetime, archived: bool, requirements: str,
visible: bool, max_student: int) -> None:
subject: Subject | None = db.session.get(Subject, subject_id)
if not subject:
raise ItemNotFoundError(f"Het subject met id {subject_id} kon niet in de databank gevonden worden")

new_project = Project()
new_project.subject_id = subject_id
new_project.name = project.name
new_project.deadline = project.deadline
new_project.archived = project.archived
new_project.requirements = project.requirements
new_project.visible = project.visible
new_project.max_students = project.max_students
msg = f"Subject with id {subject_id} not found"
raise ItemNotFoundError(msg)

new_project: Project = Project(subject_id=subject_id, subject=subject, name=name, deadline=deadline,
archived=archived, requirements=requirements, visible=visible,
max_students=max_student)

db.session.add(new_project)
db.session.commit()

project.id = new_project.id

def get_project(self, project_id: int) -> ProjectDataclass:
project = Project.query.get(project_id)
project: Project | None = db.session.get(Project, ident=project_id)
if not project:
raise ItemNotFoundError(f"Het project met id {project_id} kon niet in de databank gevonden worden")
msg = f"Project with id {project_id} not found"
raise ItemNotFoundError(msg)
return project.to_domain_model()

def get_projects(self, subject_id: int) -> list[ProjectDataclass]:
subject = Subject.query.get(subject_id)
def get_projects_of_subject(self, subject_id: int) -> list[ProjectDataclass]:
subject: Subject | None = db.session.get(Subject, ident=subject_id)
if not subject:
raise ItemNotFoundError(f"Het subject met id {subject_id} kon niet in de databank gevonden worden")
msg = f"Subject with id {subject_id} not found"
raise ItemNotFoundError(msg)
projects: list[Project] = subject.projects
return [project.to_domain_model() for project in projects]
2 changes: 1 addition & 1 deletion backend/db/implementation/SqlStudentDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_student(self, ident: int) -> StudentDataclass:
return student.to_domain_model()

def get_all_students(self) -> list[StudentDataclass]:
students: list[Student] = db.session.scalars(select(Student)).all()
students: list[Student] = list(db.session.scalars(select(Student)).all())
return [student.to_domain_model() for student in students]

def create_student(self, name: str, email: str) -> None:
Expand Down
4 changes: 2 additions & 2 deletions backend/db/implementation/SqlTeacherDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def get_teacher(self, ident: int) -> TeacherDataclass:
teacher: Teacher | None = db.session.get(Teacher, ident)

if not teacher:
msg = f"Teacher with id {ident} not found"
msg = f"Teacher with id {ident} not found"
raise ItemNotFoundError(msg)

return teacher.to_domain_model()

def get_all_teachers(self) -> list[TeacherDataclass]:
teachers: list[Teacher] = db.session.scalars(select(Teacher)).all()
teachers: list[Teacher] = list(db.session.scalars(select(Teacher)).all())
return [teacher.to_domain_model() for teacher in teachers]

def create_teacher(self, name: str, email: str) -> None:
Expand Down
22 changes: 9 additions & 13 deletions backend/db/implementation/SqlUserDAO.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
from sqlalchemy import select

from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.UserDAO import UserDAO
from db.models.models import User
from domain.models.models import UserDataclass
from domain.models.UserDataclass import UserDataclass


class SqlUserDAO(UserDAO):
def get_user(self, ident: int) -> UserDataclass:
user: User = User.query.get(ident=ident)

user: User | None = db.session.get(User, ident=ident)
if not user:
raise ItemNotFoundError("UserDataClass with given id not found.")

msg = f"User with id {ident} not found"
raise ItemNotFoundError(msg)
return user.to_domain_model()

def get_all_users(self) -> list[UserDataclass]:
users: list[User] = User.query.all()
users: list[User] = list(db.session.scalars(select(User)).all())
return [user.to_domain_model() for user in users]

def create_user(self, user: UserDataclass):
new_user: User = User()
new_user.email = user.email
new_user.name = user.name

def create_user(self, name: str, email: str) -> None:
new_user: User = User(name=name, email=email)
db.session.add(new_user)
db.session.commit()

user.id = new_user.id
22 changes: 11 additions & 11 deletions backend/db/interface/GroupDAO.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from abc import ABC, abstractmethod

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


class GroupDAO(ABC):
@abstractmethod
def create_group(self, group: GroupDataclass, project_id: int):
def create_group(self, project_id: int) -> None:
"""
Creëert een nieuw GroupDataClass in de database en associeert het met een ProjectDataClass.
:param group: De GroupDataClass domeinmodel-instantie die aan de database moet worden toegevoegd.
:param project_id: De identificatie van de ProjectDataClass waarmee het GroupDataClass geassocieerd wordt.
:param project_id: Id van het project dat gelinkt is aan de groep
:raises: ItemNotFoundException: Als er geen ProjectDataClass met de opgegeven `project_id` in de database is.
"""
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def get_group(self, group_id: int) -> GroupDataclass:
Expand All @@ -24,7 +24,7 @@ def get_group(self, group_id: int) -> GroupDataclass:
:raises ItemNotFoundException: Als er geen GroupDataClass met de opgegeven `group_id` in de database bestaat.
:returns: De domeinmodel-instantie van het opgehaalde GroupDataClass.
"""
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def get_groups_of_project(self, project_id: int) -> list[GroupDataclass]:
Expand All @@ -34,7 +34,7 @@ def get_groups_of_project(self, project_id: int) -> list[GroupDataclass]:
:param project_id: Het subject waarvan de projecten opgehaald moeten worden.
:return: Een lijst van projecten die bij een bepaald project horen.
"""
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def get_groups_of_student(self, student_id: int) -> list[GroupDataclass]:
Expand All @@ -44,18 +44,18 @@ def get_groups_of_student(self, student_id: int) -> list[GroupDataclass]:
:param student_id: De student waarvan de groepen opgehaald moeten worden.
:return: Een lijst van groepen die bij een bepaald student horen.
"""
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def add_student_to_group(self, student_id: int, group_id: int):
def add_student_to_group(self, student_id: int, group_id: int) -> None:
"""
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()
raise NotImplementedError

def get_students_of_group(self, group_id: int) -> list[StudentDataclass]:
"""
Expand All @@ -64,4 +64,4 @@ def get_students_of_group(self, group_id: int) -> list[StudentDataclass]:
: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()
raise NotImplementedError
21 changes: 14 additions & 7 deletions backend/db/interface/ProjectDAO.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
from abc import ABC, abstractmethod
from datetime import datetime

from domain.models.models import ProjectDataclass
from domain.models.ProjectDataclass import ProjectDataclass


class ProjectDAO(ABC):
@abstractmethod
def create_project(self, project: ProjectDataclass, subject_id: int):
def create_project(self, subject_id: int, name: str, deadline: datetime, archived: bool, requirements: str,
visible: bool, max_student: int) -> None:
"""
Creëert een nieuw ProjectDataClass in de database en associeert het met een SubjectDataClass.
:param project: De ProjectDataClass domeinmodel-instantie die aan de database moet worden toegevoegd.
:param max_student: maximaal aantal studenten per groep per project
:param visible: of het project zichtbaar is voor de studenten
:param requirements: Uitleg van het project
:param archived: Of het project gearchiveerd is
:param name: De naam van het project
:param deadline: De deadline van het project
:param subject_id: De identificatie van de SubjectDataClass waarmee het ProjectDataClass geassocieerd wordt.
:raises: ItemNotFoundException: Als er geen SubjectDataClass met de opgegeven `teacher_id` in de database is.
"""
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def get_project(self, project_id: int) -> ProjectDataclass:
Expand All @@ -24,14 +31,14 @@ def get_project(self, project_id: int) -> ProjectDataclass:
:raises ItemNotFoundException: Als er geen ProjectDataClass met de opgegeven `project_id` in de database bestaat
:returns: De domeinmodel-instantie van het opgehaalde ProjectDataClass.
"""
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def get_projects(self, subject_id: int) -> list[ProjectDataclass]:
def get_projects_of_subject(self, subject_id: int) -> list[ProjectDataclass]:
"""
Haalt alle projecten op die bij een bepaald subject horen.
:param subject_id: Het subject waarvan de projecten opgehaald moeten worden.
:return: Een lijst van projecten die bij een bepaald subject horen.
"""
raise NotImplementedError()
raise NotImplementedError
13 changes: 7 additions & 6 deletions backend/db/interface/UserDAO.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 UserDataclass
from domain.models.UserDataclass import UserDataclass


class UserDAO(ABC):
Expand All @@ -13,7 +13,7 @@ def get_user(self, ident: int) -> UserDataclass:
:return: De user die overeenkomt met de gegeven id.
:raises ItemNotFoundException: Als geen user met het gegeven id gevonden werd.
"""
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def get_all_users(self) -> list[UserDataclass]:
Expand All @@ -22,13 +22,14 @@ def get_all_users(self) -> list[UserDataclass]:
:return: Een lijst van alle users.
"""
raise NotImplementedError()
raise NotImplementedError

@abstractmethod
def create_user(self, user: UserDataclass):
def create_user(self, name: str, email: str) -> None:
"""
Maakt een nieuwe user aan.
:param user: De user die aangemaakt moet worden.
:param name: De naam van de nieuwe user
:param email: De email van de nieuwe user
"""
raise NotImplementedError()
raise NotImplementedError
Loading

0 comments on commit b840cab

Please sign in to comment.