Feat: basic managent for students

This commit is contained in:
2022-12-27 08:41:59 +01:00
parent fd567c292d
commit e5a50e0be8
2 changed files with 104 additions and 92 deletions

View File

@@ -31,6 +31,10 @@ class StudentSQLiteRepository(AbstractRepository):
},
)
def _rebuild_student(self, row: tuple, tribes: list[Tribe]) -> Student:
tribe = next(filter(lambda t: t.name == row[2], tribes))
return Student(id=row[0], name=row[1], tribe=tribe)
def get(self, id: str, tribes: list[Tribe]) -> Student:
cursor = self.conn.cursor()
cursor.execute(
@@ -42,12 +46,11 @@ class StudentSQLiteRepository(AbstractRepository):
row = cursor.fetchone()
if row:
tribe = next(filter(lambda t: t.name == row[2], tribes))
return Student(id=row[0], name=row[1], tribe=tribe)
return self._rebuild_student(row, tribes)
raise ValueError(f"The student ({id=}) does not exists")
def list(self) -> list[Student]:
def list(self, tribes: list[Tribe]) -> list[Student]:
cursor = self.conn.cursor()
cursor.execute(
"""
@@ -56,14 +59,14 @@ class StudentSQLiteRepository(AbstractRepository):
)
rows = cursor.fetchall()
return [Student(*r) for r in rows]
return [self._rebuild_student(r, tribes) for r in rows]
def delete(self, student: Student) -> None:
self.conn.execute(
"""
DELETE FROM students WHERE name=:name
DELETE FROM students WHERE id=:id
""",
{
"name": student.name,
"id": student.id,
},
)