Feat: add add_student to service

This commit is contained in:
2022-12-30 08:15:37 +01:00
parent 12b3220170
commit 94c942d055
2 changed files with 96 additions and 12 deletions

View File

@@ -1,5 +1,7 @@
from backend.model.student import Student
from backend.model.tribe import Tribe
from backend.repository.abstract_repository import AbstractRepository
from backend.repository.student_sqlite_repository import StudentRepositoryError
from backend.repository.tribe_sqlite_repository import TribeRepositoryError
@@ -7,7 +9,15 @@ class TribeExists(Exception):
pass
class TribeDosNotExists(Exception):
class TribeDoesNotExist(Exception):
pass
class StudentExists(Exception):
pass
class StudentDoesExist(Exception):
pass
@@ -26,7 +36,7 @@ def update_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn) ->
try:
tribe_repo.update(name=name, tribe=tribe)
except TribeRepositoryError:
raise TribeDosNotExists(f"The tribe {name} does not exists you can't update it")
raise TribeDoesNotExist(f"The tribe {name} does not exists you can't update it")
conn.commit()
return tribe
@@ -35,6 +45,32 @@ def delete_tribe(name: str, tribe_repo: AbstractRepository, conn) -> None:
try:
tribe_repo.delete(name=name)
except TribeRepositoryError:
raise TribeDosNotExists(f"The tribe {name} does not exists you can't delete it")
raise TribeDoesNotExist(f"The tribe {name} does not exists you can't delete it")
conn.commit()
def add_student(
name: str,
tribe: str,
student_repo: AbstractRepository,
tribe_repo: AbstractRepository,
conn,
) -> Student:
try:
_tribe = tribe_repo.get(tribe)
except TribeRepositoryError:
raise TribeDoesNotExist(
f"The tribe {tribe} does not exists. Can't add a student in it"
)
student = Student(name=name, tribe=_tribe)
try:
student_repo.add(student)
except StudentRepositoryError:
raise StudentExists(f"The student {student.name} already exists. Can't add it.")
conn.commit()
return student