mirror of
https://github.com/cssgunc/compass.git
synced 2025-05-18 22:29:50 -04:00
basic search feature down
This commit is contained in:
parent
bdc6600a3f
commit
b4c9e5e93a
18
backend/api/search.py
Normal file
18
backend/api/search.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
|
||||||
|
from ..services import SearchService
|
||||||
|
from ..models.resource_model import Resource
|
||||||
|
from ..models.service_model import Service
|
||||||
|
|
||||||
|
|
||||||
|
api = APIRouter(prefix="/api/search")
|
||||||
|
|
||||||
|
openapi_tags = {
|
||||||
|
"name": "Search",
|
||||||
|
"description": "Search through all resources and services for a string.",
|
||||||
|
}
|
||||||
|
|
||||||
|
@api.post("", tags=["Search"])
|
||||||
|
def search(query: str, search_svc: SearchService = Depends()) -> list[Resource | Service]:
|
||||||
|
return search_svc.search(query)
|
||||||
|
|
|
@ -3,7 +3,7 @@ from fastapi.responses import JSONResponse
|
||||||
from fastapi.middleware.gzip import GZipMiddleware
|
from fastapi.middleware.gzip import GZipMiddleware
|
||||||
|
|
||||||
|
|
||||||
from .api import user, health, service, resource, tag
|
from .api import user, health, service, resource, tag, search
|
||||||
|
|
||||||
description = """
|
description = """
|
||||||
Welcome to the **COMPASS** RESTful Application Programming Interface.
|
Welcome to the **COMPASS** RESTful Application Programming Interface.
|
||||||
|
@ -24,7 +24,7 @@ app = FastAPI(
|
||||||
|
|
||||||
app.add_middleware(GZipMiddleware)
|
app.add_middleware(GZipMiddleware)
|
||||||
|
|
||||||
feature_apis = [user, health, service, resource, tag]
|
feature_apis = [user, health, service, resource, tag, search]
|
||||||
|
|
||||||
for feature_api in feature_apis:
|
for feature_api in feature_apis:
|
||||||
app.include_router(feature_api.api)
|
app.include_router(feature_api.api)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from .user import UserService
|
from .user import UserService
|
||||||
from .resource import ResourceService
|
from .resource import ResourceService
|
||||||
from .tag import TagService
|
from .tag import TagService
|
||||||
from .service import ServiceService
|
from .service import ServiceService
|
||||||
|
from .search import SearchService
|
37
backend/services/search.py
Normal file
37
backend/services/search.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
from fastapi import Depends
|
||||||
|
from ..database import db_session
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import or_, select
|
||||||
|
from ..models.resource_model import Resource
|
||||||
|
from ..models.service_model import Service
|
||||||
|
from ..entities.resource_entity import ResourceEntity
|
||||||
|
from backend.entities.service_entity import ServiceEntity
|
||||||
|
from ..models.user_model import User, UserTypeEnum
|
||||||
|
|
||||||
|
|
||||||
|
class SearchService:
|
||||||
|
def __init__(self, session: Session = Depends(db_session)):
|
||||||
|
self._session = session
|
||||||
|
|
||||||
|
def search(self, query_str: str) -> list[Resource | Service]:
|
||||||
|
results = []
|
||||||
|
models = [
|
||||||
|
ResourceEntity,
|
||||||
|
ServiceEntity,
|
||||||
|
]
|
||||||
|
|
||||||
|
for model in models:
|
||||||
|
columns = [column for column in model.__table__.columns]
|
||||||
|
|
||||||
|
filters = [
|
||||||
|
column.ilike(f"%{query_str}%")
|
||||||
|
for column in columns
|
||||||
|
if column.type.__class__.__name__ in ["String", "Text"]
|
||||||
|
]
|
||||||
|
|
||||||
|
if filters:
|
||||||
|
query = self._session.query(model).filter(or_(*filters))
|
||||||
|
results.extend(query.all())
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
Loading…
Reference in New Issue
Block a user