Feat: add put for student and fix fixture around database
This commit is contained in:
@@ -86,3 +86,26 @@ async def post_student(item: StudentModel):
|
||||
conn.commit()
|
||||
|
||||
return student.to_dict()
|
||||
|
||||
|
||||
@app.put(
|
||||
"/students/{student_id}",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=StudentModel,
|
||||
)
|
||||
async def put_student(student_id, item: StudentModel):
|
||||
tribe_name = item.tribe_name
|
||||
try:
|
||||
tribe = tribe_repo.get(tribe_name)
|
||||
except TribeRepositoryError:
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
content=f"The tribe {tribe_name} does not exists. You can't add a student in it.",
|
||||
)
|
||||
|
||||
student = Student(name=item.name, tribe=tribe, id=student_id)
|
||||
|
||||
student_repo.update(student)
|
||||
conn.commit()
|
||||
|
||||
return student.to_dict()
|
||||
|
@@ -3,6 +3,10 @@ from backend.model.tribe import Tribe
|
||||
from backend.repository.abstract_repository import AbstractRepository
|
||||
|
||||
|
||||
class StudentRepositoryError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class StudentSQLiteRepository(AbstractRepository):
|
||||
def __init__(self, conn) -> None:
|
||||
self.conn = conn
|
||||
@@ -19,7 +23,16 @@ class StudentSQLiteRepository(AbstractRepository):
|
||||
),
|
||||
)
|
||||
|
||||
def update(self, name: str, student: Student) -> None:
|
||||
def update(self, student: Student) -> None:
|
||||
search_student = self.conn.execute(
|
||||
"""
|
||||
SELECT id FROM students WHERE id=:id
|
||||
""",
|
||||
{"id": student.id},
|
||||
).fetchone()
|
||||
if search_student is None:
|
||||
raise StudentRepositoryError(f"The student ({student.id=}) does not exists")
|
||||
|
||||
self.conn.execute(
|
||||
"""
|
||||
UPDATE students SET name=:newname, tribe_name=:newtribe WHERE id=:id
|
||||
@@ -32,6 +45,8 @@ class StudentSQLiteRepository(AbstractRepository):
|
||||
)
|
||||
|
||||
def _rebuild_student(self, row: tuple, tribes: list[Tribe]) -> Student:
|
||||
print(row)
|
||||
print([t.name for t in tribes])
|
||||
tribe = next(filter(lambda t: t.name == row[2], tribes))
|
||||
return Student(id=row[0], name=row[1], tribe=tribe)
|
||||
|
||||
|
Reference in New Issue
Block a user