Feat: add gets and delete api paths for students

This commit is contained in:
Bertrand Benjamin 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,
},
)

View File

@ -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() == []