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 19 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
4 changes: 4 additions & 0 deletions backend/db/errors/database_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class ItemNotFoundError(Exception):
def __init__(self, message: str):
super().__init__(message)


class UniqueConstraintError(Exception):
def __init__(self, message: str):
super().__init__(message)
31 changes: 31 additions & 0 deletions backend/db/implementation/SqlAdminDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.AdminDAO import AdminDAO
from db.models.models import Admin, User
from domain.models.models import AdminDataclass


class SqlAdminDAO(AdminDAO):
def get_admin(self, ident: int) -> AdminDataclass:
admin: Admin = Admin.query.get(ident=ident)
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved
if not admin:
raise ItemNotFoundError("AdminDataclass with given id not found")
return admin.to_domain_model()

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

def create_admin(self, user_id: int):
user: User = User.query.get(ident=user_id)

if not user:
raise ItemNotFoundError("User with given id not found")

new_admin: Admin = Admin()
new_admin.id = user_id

db.session.add(new_admin)
db.session.commit()
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved


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
new_group.project = project
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved
db.session.add(new_group)
db.session.commit()
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved

group.id = new_group.id
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved

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]:
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved
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
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved

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
38 changes: 38 additions & 0 deletions backend/db/implementation/SqlProjectDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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


class SqlProjectDAO(ProjectDAO):
def create_project(self, project: ProjectDataclass, subject_id: int):
subject = Subject.query.get(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
db.session.add(new_project)
db.session.commit()
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved

project.id = new_project.id

def get_project(self, project_id: int) -> ProjectDataclass:
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")
return project

def get_projects(self, subject_id: int) -> list[ProjectDataclass]:
subject = Subject.query.get(subject_id)
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]
29 changes: 29 additions & 0 deletions backend/db/implementation/SqlStudentDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.StudentDAO import StudentDAO
from db.models.models import Student, User
from domain.models.models import StudentDataclass


class SqlStudentDAO(StudentDAO):
def get_student(self, ident: int) -> StudentDataclass:
student: Student = Student.query.get(ident=ident)
if not student:
raise ItemNotFoundError("StudentDataclass with given id not found")
return student

def get_all_students(self) -> list[StudentDataclass]:
students: list[Student] = Student.query.all()
return students

def create_student(self, user_id: int):
user: User = User.query.get(ident=user_id)

if not user:
raise ItemNotFoundError("User with given id not found.")

new_student: Student = Student()
new_student.id = user_id

db.session.add(new_student)
db.session.commit()
73 changes: 73 additions & 0 deletions backend/db/implementation/SqlSubjectDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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.models import SubjectDataclass


class SqlSubjectDAO(SubjectDAO):
def create_subject(self, subject: SubjectDataclass, teacher_id: int):
teacher = Teacher.query.get(teacher_id)

if not teacher:
raise ItemNotFoundError(f"De teacher met id {teacher_id} kon niet in de databank gevonden worden")

new_subject = Subject()
new_subject.name = subject.name
new_subject.teachers.append(teacher)

db.session.add(new_subject)
db.session.commit()

subject.id = new_subject.id

def get_subject(self, teacher_id: int):
subject = Subject.query.get(teacher_id)
if not subject:
raise ItemNotFoundError(f"De lesgever met id {teacher_id} kon niet in de databank gevonden worden")

return subject.to_domain_model()

def get_subjects_teacher(self, teacher_id: int) -> list[SubjectDataclass]:
teacher: Teacher = Teacher.query.get(ident=teacher_id)

if not teacher:
raise ItemNotFoundError(f"De teacher met id {teacher_id} kon niet in de databank gevonden worden")

subjects: list[Subject] = teacher.subjects
return [vak.name for vak in subjects]
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved

def get_subjects_student(self, student_id: int) -> list[SubjectDataclass]:
student: Student = Student.query.get(ident=student_id)

if not student:
raise ItemNotFoundError(f"De student met id {student_id} kon niet in de databank gevonden worden")

subjects: list[Subject] = student.subjects
return [vak.name for vak in subjects]

def add_subject_student(self, subject_id: int, student_id: int):
student: Student = Student.query.get(ident=student_id)
subject: Subject = Subject.query.get(ident=subject_id)

if not student:
raise ItemNotFoundError(f"De student met id {student_id} kon niet in de databank gevonden worden")
if not subject:
raise ItemNotFoundError(f"Het subject met id {subject_id} kon niet in de databank gevonden worden")
if subject in student.subjects:
raise UniqueConstraintError(f"De student met id {student_id} volgt het vak met id {student_id} al")

student.subjects.append(subject)

def add_subject_teacher(self, subject_id: int, teacher_id: int):
teacher: Teacher = Teacher.query.get(ident=teacher_id)
subject: Subject = Subject.query.get(ident=subject_id)

if not teacher:
raise ItemNotFoundError(f"De teacher met id {teacher_id} kon niet in de databank gevonden worden")
if not subject:
raise ItemNotFoundError(f"Het subject met id {subject_id} kon niet in de databank gevonden worden")
if subject in teacher.subjects:
raise UniqueConstraintError(f"De teacher met id {teacher_id} volgt het vak met id {subject_id} al")

teacher.subjects.append(subject)
47 changes: 47 additions & 0 deletions backend/db/implementation/SqlSubmissionDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

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.models import 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.TeacherDAO import TeacherDAO
from db.models.models import Teacher
from db.models.models import Teacher, User
from domain.models.models import TeacherDataclass


Expand All @@ -18,10 +18,14 @@ 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):
new_teacher = Teacher(name=teacher.name)
def create_teacher(self, user_id: int):
user: User = User.query.get(ident=user_id)
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved

if not user:
raise ItemNotFoundError("User with given id not found.")

new_teacher: Teacher = Teacher()
new_teacher.id = user_id

db.session.add(new_teacher)
db.session.commit()

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


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

if not user:
raise ItemNotFoundError("UserDataClass with given id not found.")

return user.to_domain_model()

def get_all_users(self) -> list[UserDataclass]:
users: list[User] = User.query.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

db.session.add(new_user)
db.session.commit()

user.id = new_user.id
ALBERICLOOS marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 0 additions & 37 deletions backend/db/implementation/SqlVakDAO.py

This file was deleted.

Loading