recopytex/backend/service/services.py

32 lines
906 B
Python
Raw Normal View History

2022-12-30 06:19:48 +00:00
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
2022-12-30 06:35:05 +00:00
class TribeDosNotExists(Exception):
pass
def add_tribe(name: str, level: str, tribe_repo: AbstractRepository, conn):
tribe = Tribe(name=name, level=level)
2022-12-30 06:19:48 +00:00
try:
tribe_repo.add(tribe)
except TribeRepositoryError:
raise TribeExists(f"The tribe {tribe.name} already exists")
conn.commit()
return tribe
2022-12-30 06:35:05 +00:00
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