2022-12-26 17:07:23 +00:00
|
|
|
from backend.model.tribe import Tribe
|
|
|
|
from backend.repository.abstract_repository import AbstractRepository
|
|
|
|
|
|
|
|
|
|
|
|
class TribeSQLiteRepository(AbstractRepository):
|
|
|
|
def __init__(self, conn) -> None:
|
|
|
|
self.conn = conn
|
|
|
|
|
|
|
|
def add(self, tribe: Tribe) -> None:
|
2022-12-26 18:39:44 +00:00
|
|
|
self.conn.execute(
|
2022-12-26 18:21:00 +00:00
|
|
|
"""
|
|
|
|
INSERT INTO tribes(name, level) VALUES (?, ?)
|
|
|
|
""",
|
|
|
|
(
|
|
|
|
tribe.name,
|
|
|
|
tribe.level,
|
|
|
|
),
|
|
|
|
)
|
2022-12-26 17:07:23 +00:00
|
|
|
|
|
|
|
def update(self, name: str, tribe: Tribe) -> None:
|
2022-12-26 18:39:44 +00:00
|
|
|
self.conn.execute(
|
|
|
|
"""
|
|
|
|
UPDATE tribes SET name=:newname, level=:newlevel WHERE name=:name
|
|
|
|
""",
|
|
|
|
{
|
|
|
|
"newname": tribe.name,
|
|
|
|
"newlevel": tribe.level,
|
|
|
|
"name": name,
|
|
|
|
},
|
|
|
|
)
|
2022-12-26 17:07:23 +00:00
|
|
|
|
|
|
|
def get(self, name: str) -> Tribe:
|
2022-12-26 18:05:50 +00:00
|
|
|
cursor = self.conn.cursor()
|
|
|
|
cursor.execute(
|
|
|
|
"""
|
|
|
|
SELECT * FROM tribes WHERE name=?
|
|
|
|
""",
|
|
|
|
(name,),
|
|
|
|
)
|
|
|
|
|
|
|
|
row = cursor.fetchone()
|
|
|
|
if row:
|
|
|
|
return Tribe(*row)
|
|
|
|
|
|
|
|
raise ValueError(f"The tribe {name} does not exists")
|
2022-12-26 17:07:23 +00:00
|
|
|
|
|
|
|
def list(self) -> list[Tribe]:
|
|
|
|
cursor = self.conn.cursor()
|
|
|
|
cursor.execute(
|
|
|
|
"""
|
|
|
|
SELECT * FROM tribes
|
|
|
|
"""
|
|
|
|
)
|
2022-12-26 18:05:50 +00:00
|
|
|
|
2022-12-26 17:07:23 +00:00
|
|
|
rows = cursor.fetchall()
|
|
|
|
return [Tribe(*r) for r in rows]
|
|
|
|
|
|
|
|
def delete(self, tribe: Tribe) -> None:
|
2022-12-26 18:39:44 +00:00
|
|
|
self.conn.execute(
|
|
|
|
"""
|
|
|
|
DELETE FROM tribes WHERE name=:name
|
|
|
|
""",
|
|
|
|
{
|
|
|
|
"name": tribe.name,
|
|
|
|
},
|
|
|
|
)
|