diff --git a/api/thing.py b/api/thing.py index 0babb871..5b8a52e1 100644 --- a/api/thing.py +++ b/api/thing.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # =============================================================================== +from typing import Optional from fastapi import APIRouter, Query, Request from fastapi_pagination.ext.sqlalchemy import paginate from sqlalchemy import select @@ -153,18 +154,26 @@ async def get_water_wells( user: viewer_dependency, session: session_dependency, request: Request, - sort: str = None, - order: str = None, + sort: Optional[str] = None, + order: Optional[str] = None, filter_: str = Query(alias="filter", default=None), - query: str = None, - name: str = None, + query: Optional[str] = None, + name: Optional[str] = None, + include_contacts: bool = False, ) -> CustomPage[WellResponse]: """ Retrieve all wells from the database. """ thing_type = request.url.path.split("/")[2].replace("-", " ") return get_db_things( - filter_, order, query, session, sort, name=name, thing_type=thing_type + filter_, + order, + query, + session, + sort, + name=name, + thing_type=thing_type, + include_contacts=include_contacts, ) @@ -348,11 +357,11 @@ async def get_thing_id_links( async def get_things( user: viewer_dependency, session: session_dependency, - # thing_id: int = None, - within: str = None, - query: str = None, - sort: str = None, - order: str = None, + within: Optional[str] = None, + query: Optional[str] = None, + sort: Optional[str] = None, + order: Optional[str] = None, + include_contacts: bool = False, filter_: str = Query( default=None, alias="filter", @@ -369,6 +378,7 @@ async def get_things( session, sort, within=within, + include_contacts=include_contacts, ) diff --git a/services/thing_helper.py b/services/thing_helper.py index 68218a95..652a893c 100644 --- a/services/thing_helper.py +++ b/services/thing_helper.py @@ -16,7 +16,7 @@ import logging import time from datetime import datetime -from typing import Sequence +from typing import Sequence, Optional from zoneinfo import ZoneInfo from fastapi import Request, HTTPException @@ -43,6 +43,7 @@ ThingIdLink, MonitoringFrequencyHistory, StatusHistory, + search, ) from services.audit_helper import audit_add from services.crud_helper import model_patcher @@ -116,13 +117,18 @@ def get_db_things( query, session, sort, - thing_type: str = None, - within: str = None, - name: str = None, + thing_type: Optional[str] = None, + within: Optional[str] = None, + name: Optional[str] = None, + include_contacts: bool = False, ) -> list: if query: - sql = select(Thing).where(make_query(Thing, query)) + sql = search( + select(Thing), + query, + vector=Thing.search_vector, + ) else: sql = select(Thing) @@ -135,6 +141,13 @@ def get_db_things( # add all eager loads for generic thing query until/unless GET /thing is deprecated sql = sql.options(*WATER_WELL_LOADER_OPTIONS) + if include_contacts: + sql = sql.options( + selectinload(Thing.contact_associations).selectinload( + ThingContactAssociation.contact + ) + ) + if name: sql = sql.where(Thing.name == name)