Feat: add put and delete path to api
This commit is contained in:
parent
3b98a881e7
commit
f3302e2132
@ -1,7 +1,7 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
from fastapi import FastAPI, status
|
from fastapi import FastAPI, status
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse, Response
|
||||||
|
|
||||||
from backend.adapters.sqlite import create_db
|
from backend.adapters.sqlite import create_db
|
||||||
from backend.api.model import StudentModel, TribeModel
|
from backend.api.model import StudentModel, TribeModel
|
||||||
@ -37,21 +37,50 @@ app = FastAPI()
|
|||||||
|
|
||||||
@app.post("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel)
|
@app.post("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel)
|
||||||
async def post_tribe(item: TribeModel):
|
async def post_tribe(item: TribeModel):
|
||||||
tribe = Tribe(**item.dict())
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
services.add_tribe(
|
tribe = services.add_tribe(
|
||||||
name=item.name, level=item.level, tribe_repo=tribe_repo, conn=conn
|
name=item.name, level=item.level, tribe_repo=tribe_repo, conn=conn
|
||||||
)
|
)
|
||||||
except TribeExists:
|
except TribeExists:
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_409_CONFLICT,
|
status_code=status.HTTP_409_CONFLICT,
|
||||||
content=f"The tribe {tribe.name} already exists",
|
content=f"The tribe {item.name} already exists",
|
||||||
)
|
)
|
||||||
|
|
||||||
return tribe.to_dict()
|
return tribe.to_dict()
|
||||||
|
|
||||||
|
|
||||||
|
@app.put("/tribes/{name}", status_code=status.HTTP_200_OK, response_model=TribeModel)
|
||||||
|
async def put_tribe(name: str, item: TribeModel):
|
||||||
|
try:
|
||||||
|
tribe = services.update_tribe(
|
||||||
|
name=item.name, level=item.level, tribe_repo=tribe_repo, conn=conn
|
||||||
|
)
|
||||||
|
except TribeDoesNotExist:
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=status.HTTP_409_CONFLICT,
|
||||||
|
content=f"The tribe {name} does not exists",
|
||||||
|
)
|
||||||
|
|
||||||
|
return tribe.to_dict()
|
||||||
|
|
||||||
|
|
||||||
|
@app.delete("/tribes/{name}")
|
||||||
|
async def delete_tribe(name: str):
|
||||||
|
try:
|
||||||
|
services.delete_tribe(name=name, tribe_repo=tribe_repo, conn=conn)
|
||||||
|
except TribeDoesNotExist:
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=status.HTTP_409_CONFLICT,
|
||||||
|
content=f"The tribe {name} does not exists",
|
||||||
|
)
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
status_code=status.HTTP_204_NO_CONTENT,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/tribes", response_model=list[TribeModel])
|
@app.get("/tribes", response_model=list[TribeModel])
|
||||||
async def list_tribes():
|
async def list_tribes():
|
||||||
tribes = tribe_repo.list()
|
tribes = tribe_repo.list()
|
||||||
|
@ -72,11 +72,11 @@ class TribeSQLiteRepository(AbstractRepository):
|
|||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
return [Tribe(*r) for r in rows]
|
return [Tribe(*r) for r in rows]
|
||||||
|
|
||||||
def delete(self, tribe: Tribe) -> None:
|
def delete(self, name: str) -> None:
|
||||||
tribes = self.list()
|
tribes = self.list()
|
||||||
if tribe.name not in map(lambda x: x.name, tribes):
|
if name not in map(lambda x: x.name, tribes):
|
||||||
raise TribeRepositoryError(
|
raise TribeRepositoryError(
|
||||||
f"The tribe {tribe.name} doesn't exists. Can't delete it."
|
f"The tribe {name} doesn't exists. Can't delete it."
|
||||||
)
|
)
|
||||||
|
|
||||||
self.conn.execute(
|
self.conn.execute(
|
||||||
@ -84,6 +84,6 @@ class TribeSQLiteRepository(AbstractRepository):
|
|||||||
DELETE FROM tribes WHERE name=:name
|
DELETE FROM tribes WHERE name=:name
|
||||||
""",
|
""",
|
||||||
{
|
{
|
||||||
"name": tribe.name,
|
"name": name,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -35,6 +35,70 @@ def test_api_post_tribe_already_exists():
|
|||||||
assert r.json() == f"The tribe {data['name']} already exists"
|
assert r.json() == f"The tribe {data['name']} already exists"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("restart_api")
|
||||||
|
@pytest.mark.usefixtures("clean_db")
|
||||||
|
def test_api_put_tribe():
|
||||||
|
tribe = build_tribes(1)[0]
|
||||||
|
|
||||||
|
url = config.get_api_url()
|
||||||
|
r = requests.post(f"{url}/tribes", json=tribe.to_dict())
|
||||||
|
assert r.status_code == 201
|
||||||
|
|
||||||
|
mod_tribe = tribe
|
||||||
|
mod_tribe.level = "other level"
|
||||||
|
r = requests.put(f"{url}/tribes/{tribe.name}", json=mod_tribe.to_dict())
|
||||||
|
assert r.status_code == 200
|
||||||
|
|
||||||
|
r = requests.get(f"{url}/tribes")
|
||||||
|
assert [t["name"] for t in r.json()] == [mod_tribe.name]
|
||||||
|
assert [t["level"] for t in r.json()] == [mod_tribe.level]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("restart_api")
|
||||||
|
@pytest.mark.usefixtures("clean_db")
|
||||||
|
def test_api_put_tribe_doesnt_exists():
|
||||||
|
tribe = build_tribes(1)[0]
|
||||||
|
|
||||||
|
url = config.get_api_url()
|
||||||
|
r = requests.put(f"{url}/tribes/{tribe.name}", json=tribe.to_dict())
|
||||||
|
assert r.status_code == 409
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("restart_api")
|
||||||
|
@pytest.mark.usefixtures("clean_db")
|
||||||
|
def test_api_delete_tribe():
|
||||||
|
tribe = build_tribes(1)[0]
|
||||||
|
|
||||||
|
url = config.get_api_url()
|
||||||
|
r = requests.post(f"{url}/tribes", json=tribe.to_dict())
|
||||||
|
|
||||||
|
assert r.status_code == 201
|
||||||
|
|
||||||
|
r = requests.delete(f"{url}/tribes/{tribe.name}")
|
||||||
|
assert r.status_code == 204
|
||||||
|
|
||||||
|
r = requests.get(f"{url}/tribes")
|
||||||
|
assert r.json() == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("restart_api")
|
||||||
|
@pytest.mark.usefixtures("clean_db")
|
||||||
|
def test_api_delete_tribe_doesnt_exists():
|
||||||
|
tribe = build_tribes(1)[0]
|
||||||
|
|
||||||
|
url = config.get_api_url()
|
||||||
|
r = requests.post(f"{url}/tribes", json=tribe.to_dict())
|
||||||
|
|
||||||
|
assert r.status_code == 201
|
||||||
|
|
||||||
|
r = requests.delete(f"{url}/tribes/notexisting")
|
||||||
|
assert r.status_code == 409
|
||||||
|
|
||||||
|
r = requests.get(f"{url}/tribes")
|
||||||
|
assert [t["name"] for t in r.json()] == [tribe.name]
|
||||||
|
assert [t["level"] for t in r.json()] == [tribe.level]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("restart_api")
|
@pytest.mark.usefixtures("restart_api")
|
||||||
@pytest.mark.usefixtures("clean_db")
|
@pytest.mark.usefixtures("clean_db")
|
||||||
def test_api_post_list_tribe():
|
def test_api_post_list_tribe():
|
||||||
|
Loading…
Reference in New Issue
Block a user