feat: deals filters
This commit is contained in:
21
utils/sorting.py
Normal file
21
utils/sorting.py
Normal file
@ -0,0 +1,21 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Query
|
||||
from sqlalchemy import Select
|
||||
|
||||
from schemas.base import SortingSchema, SortDir
|
||||
from utils.strings import camel_to_snake
|
||||
|
||||
|
||||
async def sorting_parameters(
|
||||
field: Optional[str] = Query(default=None, alias="sortingField"),
|
||||
direction: Optional[SortDir] = Query(default=None, alias="sortingDirection"),
|
||||
) -> SortingSchema:
|
||||
return SortingSchema(field=camel_to_snake(field), direction=direction)
|
||||
|
||||
|
||||
def apply_sorting(stmt: Select, cls: type, field: str, direction: SortDir) -> Select:
|
||||
attr = getattr(cls, field)
|
||||
if direction == "asc":
|
||||
return stmt.order_by(attr.asc())
|
||||
return stmt.order_by(attr.desc())
|
||||
Reference in New Issue
Block a user