recopytex/backend/service/services.py

32 lines
906 B
Python

from backend.model.tribe import Tribe
from backend.repository.abstract_repository import AbstractRepository
from backend.repository.tribe_sqlite_repository import TribeRepositoryError
class TribeExists(Exception):
pass
class TribeDosNotExists(Exception):
pass
def add_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn):
tribe = Tribe(name=name, level=level)
try:
tribe_repo.add(tribe)
except TribeRepositoryError:
raise TribeExists(f"The tribe {tribe.name} already exists")
conn.commit()
return tribe
def update_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn):
tribe = Tribe(name=name, level=level)
try:
tribe_repo.update(name=name, tribe=tribe)
except TribeRepositoryError:
raise TribeDosNotExists(f"The tribe {name} does not exists you can't update it")
conn.commit()
return tribe