Feat: add gets and delete api paths for students

This commit is contained in:
2022-12-30 14:41:23 +01:00
parent f3302e2132
commit 07595f1fd8
3 changed files with 77 additions and 4 deletions

View File

@@ -37,7 +37,6 @@ app = FastAPI()
@app.post("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel)
async def post_tribe(item: TribeModel):
try:
tribe = services.add_tribe(
name=item.name, level=item.level, tribe_repo=tribe_repo, conn=conn
@@ -120,6 +119,21 @@ async def post_student(item: StudentModel):
return student.to_dict()
@app.get("/students/{id}", status_code=status.HTTP_200_OK, response_model=StudentModel)
async def get_student(id: str):
tribes = tribe_repo.list()
student = student_repo.get(id, tribes)
return student.to_dict()
@app.get("/students", status_code=status.HTTP_200_OK, response_model=list[StudentModel])
async def list_students():
tribes = tribe_repo.list()
students = student_repo.list(tribes)
return [t.to_dict() for t in students]
@app.put(
"/students/{student_id}",
status_code=status.HTTP_200_OK,
@@ -153,3 +167,24 @@ async def put_student(student_id, item: StudentModel):
)
return student.to_dict()
@app.delete(
"/students/{student_id}",
)
async def delete_student(student_id):
try:
student = services.delete_student(
id=student_id,
student_repo=student_repo,
conn=conn,
)
except StudentDoesExist:
return JSONResponse(
status_code=status.HTTP_409_CONFLICT,
content=f"The student ({student_id=}) does not exists. You can't delete it.",
)
return Response(
status_code=status.HTTP_204_NO_CONTENT,
)

View File

@@ -77,12 +77,29 @@ class StudentSQLiteRepository(AbstractRepository):
rows = cursor.fetchall()
return [self._rebuild_student(r, tribes) for r in rows]
def delete(self, student: Student) -> None:
def list_id(self):
cursor = self.conn.cursor()
cursor.execute(
"""
SELECT id FROM students
"""
)
rows = cursor.fetchall()
return [r[0] for r in rows]
def delete(self, id: str) -> None:
students_id = self.list_id()
if id not in students_id:
raise StudentRepositoryError(
f"The student {id} doesn't exists. Can't delete it."
)
self.conn.execute(
"""
DELETE FROM students WHERE id=:id
""",
{
"id": student.id,
"id": id,
},
)