This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
toevoegen van implementatie SqlGroupDAO
- Loading branch information
1 parent
4dac07d
commit f9ab3d5
Showing
3 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters