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

Voeg DAO interfaces + implementaties toe #21

Merged
merged 31 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
af61161
DAO interfaces aanmaken
ALBERICLOOS Feb 24, 2024
33575b8
Merge remote-tracking branch 'origin/add-models' into add-models
ALBERICLOOS Feb 24, 2024
1e67b0e
toevoegen admin en user DAO interfaces + aanpassen andere DAO interfaces
ALBERICLOOS Feb 24, 2024
b96f3be
toevoegen + aanpassen functies van DAO
ALBERICLOOS Feb 24, 2024
47a071c
aanpassen argument give_admin functie
ALBERICLOOS Feb 24, 2024
229b861
aanpassen import
ALBERICLOOS Feb 24, 2024
244c0fa
aanpassen create_admin
ALBERICLOOS Feb 24, 2024
6bd2faf
ruff check
ALBERICLOOS Feb 24, 2024
0b591aa
Toevoegen implementatie UserDAO en AdminDAO
ALBERICLOOS Feb 25, 2024
83e1fc0
aanpassen file name
ALBERICLOOS Feb 25, 2024
df416b7
verder werken aan DAO interfaces + implementaties
ALBERICLOOS Feb 25, 2024
9633432
toevoegen uniqueconstraint error
ALBERICLOOS Feb 25, 2024
707dc45
import veranderen
ALBERICLOOS Feb 25, 2024
88fea97
Subject DAO uitbreiden
ALBERICLOOS Feb 25, 2024
4dac07d
toevoegen van project DAO
ALBERICLOOS Feb 25, 2024
f9ab3d5
toevoegen van implementatie SqlGroupDAO
ALBERICLOOS Feb 25, 2024
8d5e69e
toevoegen van SubmissionsDAO
ALBERICLOOS Feb 25, 2024
ae3c775
nieuwe fillDatabse file
ALBERICLOOS Feb 25, 2024
40e7632
ruff check vergeten
ALBERICLOOS Feb 25, 2024
d9e11da
enkele problemen van PR oplossen
ALBERICLOOS Feb 26, 2024
96ca5d1
aanpassen PR review
ALBERICLOOS Feb 26, 2024
51e6f08
merge met main
ALBERICLOOS Feb 26, 2024
c009d68
Admin DAO afgewerkt
ALBERICLOOS Feb 26, 2024
46398be
Student DAO klaar
ALBERICLOOS Feb 26, 2024
7de801e
teacherDAO klaar
ALBERICLOOS Feb 26, 2024
d531244
subject DAO klaar
ALBERICLOOS Feb 26, 2024
06ef03c
Submissions DAO klaar
ALBERICLOOS Feb 26, 2024
b840cab
DAO's klaar
ALBERICLOOS Feb 26, 2024
d8389ea
aanpassingen maken van PR review
ALBERICLOOS Feb 27, 2024
4f68cea
aanpassen create functies
ALBERICLOOS Feb 27, 2024
a176995
db.session.add verwijderen + toevoegen nieuwe mock data
ALBERICLOOS Feb 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions backend/db/errors/database_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ class ItemNotFoundError(Exception):

def __init__(self, message: str) -> None:
super().__init__(message)


class UniqueConstraintError(Exception):
def __init__(self, message: str) -> None:
super().__init__(message)
27 changes: 27 additions & 0 deletions backend/db/implementation/SqlAdminDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from sqlalchemy import select

from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.AdminDAO import AdminDAO
from db.models.models import Admin
from domain.models.AdminDataclass import AdminDataclass


class SqlAdminDAO(AdminDAO):
def get_admin(self, ident: int) -> AdminDataclass:
admin: Admin | None = db.session.get(Admin, ident=ident)
if not admin:
msg = f"Admin with id {ident} not found"
raise ItemNotFoundError(msg)
return admin.to_domain_model()

def get_all_admins(self) -> list[AdminDataclass]:
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) -> AdminDataclass:
new_admin: Admin = Admin(name=name, email=email)
db.session.add(new_admin)
db.session.commit()
return new_admin.to_domain_model()

66 changes: 66 additions & 0 deletions backend/db/implementation/SqlGroupDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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.GroupDataclass import GroupDataclass
from domain.models.StudentDataclass import StudentDataclass


class SqlGroupDAO(GroupDAO):
def create_group(self, project_id: int) -> GroupDataclass:
project: Project | None = db.session.get(Project, ident=project_id)
if not project:
msg = f"Project with id {project} not found"
raise ItemNotFoundError(msg)
new_group: Group = Group(project_id=project_id)
db.session.add(new_group)
db.session.commit()
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved
return new_group.to_domain_model()

def get_group(self, group_id: int) -> GroupDataclass:
group: Group | None = db.session.get(Group, ident=group_id)
if not group:
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 | None = db.session.get(Project, ident=project_id)
if not project:
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 | None = db.session.get(Student, ident=student_id)
if not student:
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) -> None:
student: Student | None = db.session.get(Student, ident=student_id)
group: Group | None = db.session.get(Group, ident=group_id)
if not student:
msg = f"Student with id {student_id} not found"
raise ItemNotFoundError(msg)
if not group:
msg = f"Group with id {group_id} not found"
raise ItemNotFoundError(msg)
if student in group.students:
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)
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved
db.session.commit()

def get_students_of_group(self, group_id: int) -> list[StudentDataclass]:
group: Group | None = db.session.get(Group, ident=group_id)
if not group:
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]
40 changes: 40 additions & 0 deletions backend/db/implementation/SqlProjectDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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.ProjectDataclass import ProjectDataclass


class SqlProjectDAO(ProjectDAO):
def create_project(self, subject_id: int, name: str, deadline: datetime, archived: bool, requirements: str,
visible: bool, max_students: int) -> ProjectDataclass:
subject: Subject | None = db.session.get(Subject, subject_id)
if not subject:
msg = f"Subject with id {subject_id} not found"
raise ItemNotFoundError(msg)

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

db.session.add(new_project)
db.session.commit()
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved
return new_project.to_domain_model()


def get_project(self, project_id: int) -> ProjectDataclass:
project: Project | None = db.session.get(Project, ident=project_id)
if not project:
msg = f"Project with id {project_id} not found"
raise ItemNotFoundError(msg)
return project.to_domain_model()

def get_projects_of_subject(self, subject_id: int) -> list[ProjectDataclass]:
subject: Subject | None = db.session.get(Subject, ident=subject_id)
if not subject:
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]
26 changes: 26 additions & 0 deletions backend/db/implementation/SqlStudentDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from sqlalchemy import select

from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.StudentDAO import StudentDAO
from db.models.models import Student
from domain.models.StudentDataclass import StudentDataclass


class SqlStudentDAO(StudentDAO):
def get_student(self, ident: int) -> StudentDataclass:
student: Student | None = db.session.get(Student, ident=ident)
if not student:
msg = f"Student with id {ident} not found"
raise ItemNotFoundError(msg)
return student.to_domain_model()

def get_all_students(self) -> list[StudentDataclass]:
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) -> StudentDataclass:
new_student: Student = Student(name=name, email=email)
db.session.add(new_student)
db.session.commit()
return new_student.to_domain_model()
72 changes: 72 additions & 0 deletions backend/db/implementation/SqlSubjectDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from db.errors.database_errors import ItemNotFoundError, UniqueConstraintError
from db.extensions import db
from db.interface.SubjectDAO import SubjectDAO
from db.models.models import Student, Subject, Teacher
from domain.models.SubjectDataclass import SubjectDataclass


class SqlSubjectDAO(SubjectDAO):
def create_subject(self, name: str) -> SubjectDataclass:
new_subject = Subject(name=name)
db.session.add(new_subject)
db.session.commit()
return new_subject.to_domain_model()

def get_subject(self, subject_id: int) -> SubjectDataclass:
subject: Subject | None = db.session.get(Subject, ident=subject_id)
if not subject:
msg = f"Subject with id {subject_id} not found"
raise ItemNotFoundError(msg)
return subject.to_domain_model()

def get_subjects_of_teacher(self, teacher_id: int) -> list[SubjectDataclass]:
teacher: Teacher | None = db.session.get(Teacher, ident=teacher_id)
if not teacher:
msg = f"Teacher with id {teacher_id} not found"
raise ItemNotFoundError(msg)
subjects: list[Subject] = teacher.subjects
return [vak.to_domain_model() for vak in subjects]

def get_subjects_of_student(self, student_id: int) -> list[SubjectDataclass]:
student: Student | None = db.session.get(Student, ident=student_id)
if not student:
msg = f"Student with id {student_id} not found"
raise ItemNotFoundError(msg)
subjects: list[Subject] = student.subjects
return [vak.to_domain_model() for vak in subjects]

def add_student_to_subject(self, student_id: int, subject_id: int) -> None:
student: Student | None = db.session.get(Student, ident=student_id)
subject: Subject | None = db.session.get(Subject, ident=subject_id)

if not student:
msg = f"Student with id {student_id} not found"
raise ItemNotFoundError(msg)
if not subject:
msg = f"Subject with id {subject_id} not found"
raise ItemNotFoundError(msg)
if subject in student.subjects:
msg = f"Student with id {student_id} already has subject with id {subject_id}"
raise UniqueConstraintError(msg)

student.subjects.append(subject)
db.session.add(student)
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved
db.session.commit()

def add_teacher_to_subject(self, teacher_id: int, subject_id: int) -> None:
teacher: Teacher | None = db.session.get(Teacher, ident=teacher_id)
subject: Subject | None = db.session.get(Subject, ident=subject_id)

if not teacher:
msg = f"Teacher with id {teacher_id} not found"
raise ItemNotFoundError(msg)
if not subject:
msg = f"Subject with id {subject_id} not found"
raise ItemNotFoundError(msg)
if subject in teacher.subjects:
msg = f"Teacher with id {teacher_id} already has subject with id {subject_id}"
raise UniqueConstraintError(msg)

teacher.subjects.append(subject)
db.session.add(teacher)
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved
db.session.commit()
49 changes: 49 additions & 0 deletions backend/db/implementation/SqlSubmissionDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from datetime import datetime

from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.SubmissionDAO import SubmissionDAO
from db.models.models import Group, Student, Submission
from domain.models.SubmissionDataclass import SubmissionDataclass, SubmissionState


class SqlSubmissionDAO(SubmissionDAO):
def create_submission(self, student_id: int, group_id: int, message: str,state: SubmissionState,
date_time: datetime) -> SubmissionDataclass:

student: Student | None = db.session.get(Student, ident=student_id)
group: Group | None = db.session.get(Group, ident=group_id)
if not student:
msg = f"Student with id {student_id} not found"
raise ItemNotFoundError(msg)
if not group:
msg = f"Group with id {group_id} not found"
raise ItemNotFoundError(msg)
new_submission: Submission = Submission(student_id=student_id,
group_id=group_id, message=message, state=state, date_time=date_time)
db.session.add(new_submission)
db.session.commit()
return new_submission.to_domain_model()

def get_submission(self, submission_id: int) -> SubmissionDataclass:
submission: Submission | None = db.session.get(Submission, ident=submission_id)
if not submission:
msg = f"Submission with id {submission_id} not found"
raise ItemNotFoundError(msg)
return submission.to_domain_model()

def get_submissions_of_student(self, student_id: int) -> list[SubmissionDataclass]:
student: Student | None = db.session.get(Student, ident=student_id)
if not student:
msg = f"Student with id {student_id} not found"
raise ItemNotFoundError(msg)
submissions: list[Submission] = student.submissions
return [submission.to_domain_model() for submission in submissions]

def get_submissions_of_group(self, group_id: int) -> list[SubmissionDataclass]:
group: Group | None = db.session.get(Group, ident=group_id)
if not group:
msg = f"Group with id {group_id} not found"
raise ItemNotFoundError(msg)
submissions: list[Submission] = group.submissions
return [submission.to_domain_model() for submission in submissions]
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from sqlalchemy import select

from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.TeacherDAO import TeacherDAO
Expand All @@ -7,22 +9,20 @@

class SqlTeacherDAO(TeacherDAO):
def get_teacher(self, ident: int) -> TeacherDataclass:
teacher: Teacher | None = Teacher.query.get(ident=ident)
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] = Teacher.query.all()
return [lesgever.to_domain_model() for lesgever in teachers]

def create_teacher(self, teacher: TeacherDataclass) -> None:
new_teacher = Teacher(name=teacher.name, email=teacher.email)
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) -> TeacherDataclass:
new_teacher = Teacher(name=name, email=email)
db.session.add(new_teacher)
db.session.commit()

teacher.id = new_teacher.id
return new_teacher.to_domain_model()
21 changes: 21 additions & 0 deletions backend/db/implementation/SqlUserDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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.UserDataclass import UserDataclass


class SqlUserDAO(UserDAO):
def get_user(self, ident: int) -> UserDataclass:
user: User | None = db.session.get(User, ident=ident)
if not user:
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] = list(db.session.scalars(select(User)).all())
return [user.to_domain_model() for user in users]

34 changes: 0 additions & 34 deletions backend/db/implementation/SqlVakDAO.py

This file was deleted.

Loading