diff --git a/app/routes/app.py b/app/routes/app.py index 0e3ca5e..063ce35 100644 --- a/app/routes/app.py +++ b/app/routes/app.py @@ -4,13 +4,14 @@ from creyPY.fastapi.crud import ( from creyPY.fastapi.db.session import get_db from fastapi import APIRouter, Depends, Security, HTTPException from sqlalchemy.orm import Session - +from pydantic.json_schema import SkipJsonSchema from app.services.auth import verify from app.schema.app import AppIN, AppOUT from app.models.app import Application -from fastapi_pagination.ext.sqlalchemy import paginate from creyPY.fastapi.pagination import Page from uuid import UUID +from fastapi_pagination.ext.sqlalchemy import paginate +from sqlalchemy import select router = APIRouter(prefix="/app", tags=["apps"]) @@ -46,11 +47,14 @@ async def delete_app( @router.get("/") async def get_apps( + search: str | SkipJsonSchema[None] = None, sub: str = Security(verify), db: Session = Depends(get_db), ) -> Page[AppOUT]: - the_select = db.query(Application).filter_by(created_by_id=sub) - return paginate(the_select) + the_select = select(Application).filter(Application.created_by_id == sub) + if search: + the_select = the_select.filter(Application.name.ilike(f"%{search}%")) + return paginate(db, the_select) @router.get("/{app_id}") diff --git a/app/routes/entry.py b/app/routes/entry.py index 7b3a033..6e92aa6 100644 --- a/app/routes/entry.py +++ b/app/routes/entry.py @@ -61,5 +61,8 @@ async def get_logs( sub: str = Security(verify), db: Session = Depends(get_db), ) -> Page[LogOUT]: + # add filters + # add sorting + # add search the_select = db.query(LogEntry).filter_by(created_by_id=sub) return paginate(the_select) diff --git a/app/test_main.py b/app/test_main.py index d335cea..99dd857 100644 --- a/app/test_main.py +++ b/app/test_main.py @@ -13,8 +13,8 @@ CURRENT_USER = "api-key|testing" @contextlib.contextmanager -def app_context(self): - app_id = self.create_app() +def app_context(self, name: str = "Testing"): + app_id = self.create_app(name) try: yield app_id finally: @@ -58,8 +58,23 @@ class TestAPI: def test_application_api(self): self.c.obj_lifecycle({"name": "Testing"}, "/app/") - def create_app(self): - re = self.c.post("/app/", {"name": "Testing"}) + def test_application_search(self): + with app_context(self, "testing 1") as app_id1: + with app_context(self, "second app 2") as app_id2: + re = self.c.get("/app/") + assert re["total"] == 2 + assert len(re["results"]) == 2 + + re = self.c.get("/app/?search=testing") + assert re["total"] == 1 + assert len(re["results"]) == 1 + + re = self.c.get("/app/?search=2") + assert re["total"] == 1 + assert len(re["results"]) == 1 + + def create_app(self, name: str = "Testing"): + re = self.c.post("/app/", {"name": name}) return re["id"] def destroy_app(self, app_id):