feat: added search for apps

This commit is contained in:
2024-10-10 16:55:33 +02:00
parent b95e40b40d
commit 133e3cf2a9
3 changed files with 30 additions and 8 deletions

View File

@@ -4,13 +4,14 @@ from creyPY.fastapi.crud import (
from creyPY.fastapi.db.session import get_db from creyPY.fastapi.db.session import get_db
from fastapi import APIRouter, Depends, Security, HTTPException from fastapi import APIRouter, Depends, Security, HTTPException
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from pydantic.json_schema import SkipJsonSchema
from app.services.auth import verify from app.services.auth import verify
from app.schema.app import AppIN, AppOUT from app.schema.app import AppIN, AppOUT
from app.models.app import Application from app.models.app import Application
from fastapi_pagination.ext.sqlalchemy import paginate
from creyPY.fastapi.pagination import Page from creyPY.fastapi.pagination import Page
from uuid import UUID from uuid import UUID
from fastapi_pagination.ext.sqlalchemy import paginate
from sqlalchemy import select
router = APIRouter(prefix="/app", tags=["apps"]) router = APIRouter(prefix="/app", tags=["apps"])
@@ -46,11 +47,14 @@ async def delete_app(
@router.get("/") @router.get("/")
async def get_apps( async def get_apps(
search: str | SkipJsonSchema[None] = None,
sub: str = Security(verify), sub: str = Security(verify),
db: Session = Depends(get_db), db: Session = Depends(get_db),
) -> Page[AppOUT]: ) -> Page[AppOUT]:
the_select = db.query(Application).filter_by(created_by_id=sub) the_select = select(Application).filter(Application.created_by_id == sub)
return paginate(the_select) if search:
the_select = the_select.filter(Application.name.ilike(f"%{search}%"))
return paginate(db, the_select)
@router.get("/{app_id}") @router.get("/{app_id}")

View File

@@ -61,5 +61,8 @@ async def get_logs(
sub: str = Security(verify), sub: str = Security(verify),
db: Session = Depends(get_db), db: Session = Depends(get_db),
) -> Page[LogOUT]: ) -> Page[LogOUT]:
# add filters
# add sorting
# add search
the_select = db.query(LogEntry).filter_by(created_by_id=sub) the_select = db.query(LogEntry).filter_by(created_by_id=sub)
return paginate(the_select) return paginate(the_select)

View File

@@ -13,8 +13,8 @@ CURRENT_USER = "api-key|testing"
@contextlib.contextmanager @contextlib.contextmanager
def app_context(self): def app_context(self, name: str = "Testing"):
app_id = self.create_app() app_id = self.create_app(name)
try: try:
yield app_id yield app_id
finally: finally:
@@ -58,8 +58,23 @@ class TestAPI:
def test_application_api(self): def test_application_api(self):
self.c.obj_lifecycle({"name": "Testing"}, "/app/") self.c.obj_lifecycle({"name": "Testing"}, "/app/")
def create_app(self): def test_application_search(self):
re = self.c.post("/app/", {"name": "Testing"}) 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"] return re["id"]
def destroy_app(self, app_id): def destroy_app(self, app_id):