diff --git a/backend/api/main.py b/backend/api/main.py index aceb53b..27b6dd0 100644 --- a/backend/api/main.py +++ b/backend/api/main.py @@ -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, + ) diff --git a/backend/repository/student_sqlite_repository.py b/backend/repository/student_sqlite_repository.py index b53f6b9..45ce1e5 100644 --- a/backend/repository/student_sqlite_repository.py +++ b/backend/repository/student_sqlite_repository.py @@ -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, }, ) diff --git a/tests/e2e/test_api_students.py b/tests/e2e/test_api_students.py index b29bd95..5069286 100644 --- a/tests/e2e/test_api_students.py +++ b/tests/e2e/test_api_students.py @@ -2,7 +2,7 @@ import pytest import requests from backend import config -from tests.model.fakes import build_tribes +from tests.model.fakes import build_student, build_tribes @pytest.mark.usefixtures("restart_api") @@ -76,3 +76,24 @@ def test_api_put_student(): assert r2.json()["name"] == "Choupinou" assert r2.json()["tribe_name"] == tribe.name assert r2.json()["id"] == r.json()["id"] + + +@pytest.mark.usefixtures("restart_api") +@pytest.mark.usefixtures("clean_db") +def test_api_delete_student(): + url = config.get_api_url() + tribe = build_tribes(1)[0] + requests.post(f"{url}/tribes", json=tribe.to_dict()) + + student = build_student([tribe], 1)[0] + r = requests.post( + f"{url}/students", json={"name": student.name, "tribe_name": student.tribe.name} + ) + assert r.status_code == 201 + student_id = r.json()["id"] + + r = requests.delete(f"{url}/students/{student_id}") + assert r.status_code == 204 + + r = requests.get(f"{url}/students/") + assert r.json() == []