Vyhledavani v POI
This commit is contained in:
@@ -21,10 +21,11 @@ class PointOfInterestQueries:
|
|||||||
async def points_of_interest(
|
async def points_of_interest(
|
||||||
root, info,
|
root, info,
|
||||||
limit: int, offset: int = 0,
|
limit: int, offset: int = 0,
|
||||||
|
search: Optional[str] = None,
|
||||||
public: bool = False
|
public: bool = False
|
||||||
) -> PaginationWindow[PointOfInterest]:
|
) -> PaginationWindow[PointOfInterest]:
|
||||||
query = BaseQueryResolver(PointOfInterest, models.PointOfInterest).get_query(
|
query = BaseQueryResolver(PointOfInterest, models.PointOfInterest).get_query(
|
||||||
info.context.user_id, only_public=public
|
info.context.user_id, only_public=public, search=search,
|
||||||
)
|
)
|
||||||
return await get_pagination_window(
|
return await get_pagination_window(
|
||||||
query=query,
|
query=query,
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
from typing import Optional, Type, TypeVar, Generic, List
|
from typing import Optional, Type, TypeVar, Generic, List
|
||||||
|
|
||||||
|
from sqlalchemy import or_
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from database import models
|
from database import models
|
||||||
from database.query_builder import QueryBuilder
|
from database.query_builder import QueryBuilder
|
||||||
@@ -42,6 +44,18 @@ class BaseQueryResolver(BaseResolver):
|
|||||||
raise AssertionError(f"Model {self.model} has no ID column! Cannot query by ID!")
|
raise AssertionError(f"Model {self.model} has no ID column! Cannot query by ID!")
|
||||||
query = query.filter(self.model.id == object_id)
|
query = query.filter(self.model.id == object_id)
|
||||||
|
|
||||||
|
search = kwargs.pop("search", None)
|
||||||
|
if search:
|
||||||
|
search_clauses = []
|
||||||
|
if hasattr(self.model, "name"):
|
||||||
|
search_clauses.append(self.model.name.contains(search))
|
||||||
|
|
||||||
|
if hasattr(self.model, "description"):
|
||||||
|
search_clauses.append(self.model.description.contains(search))
|
||||||
|
|
||||||
|
if search_clauses:
|
||||||
|
query = query.filter(or_(*search_clauses))
|
||||||
|
|
||||||
if kwargs:
|
if kwargs:
|
||||||
query = query.filter_by(**kwargs)
|
query = query.filter_by(**kwargs)
|
||||||
|
|
||||||
@@ -101,4 +115,3 @@ class BaseMutationResolver(BaseResolver):
|
|||||||
await db.delete(model)
|
await db.delete(model)
|
||||||
|
|
||||||
return self.graphql_type(**model.as_dict())
|
return self.graphql_type(**model.as_dict())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user