diff --git a/fastapi_book_server/app/services/author.py b/fastapi_book_server/app/services/author.py index 3cbd096..be3c6e8 100644 --- a/fastapi_book_server/app/services/author.py +++ b/fastapi_book_server/app/services/author.py @@ -4,7 +4,7 @@ from app.services.common import TRGMSearchService class AuthorTGRMSearchService(TRGMSearchService): - MODEL = Author + MODEL_CLASS = Author FIELDS = [ Author.Meta.table.c.last_name, Author.Meta.table.c.first_name, diff --git a/fastapi_book_server/app/services/book.py b/fastapi_book_server/app/services/book.py index 0a28c62..c0e1f21 100644 --- a/fastapi_book_server/app/services/book.py +++ b/fastapi_book_server/app/services/book.py @@ -9,7 +9,7 @@ from app.serializers.book import CreateBook, CreateRemoteBook class BookTGRMSearchService(TRGMSearchService): - MODEL = BookDB + MODEL_CLASS = BookDB FIELDS = [ BookDB.Meta.table.c.title ] diff --git a/fastapi_book_server/app/services/common.py b/fastapi_book_server/app/services/common.py index d22b044..955ea51 100644 --- a/fastapi_book_server/app/services/common.py +++ b/fastapi_book_server/app/services/common.py @@ -1,9 +1,9 @@ -from typing import Optional, Generic, TypeVar, Union, Any, cast +from typing import Optional, Generic, TypeVar, Union, cast from itertools import permutations from fastapi_pagination.api import resolve_params -from fastapi_pagination.bases import RawParams -from app.utils.pagination import CustomPage +from fastapi_pagination.bases import AbstractParams, RawParams +from app.utils.pagination import Page, CustomPage from ormar import Model, QuerySet from sqlalchemy import text, func, select, desc, Table, Column @@ -29,7 +29,11 @@ class TRGMSearchService(Generic[T]): PREFETCH_RELATED: Optional[Union[list[str], str]] = None @classmethod - def get_params(cls) -> RawParams: + def get_params(cls) -> AbstractParams: + return resolve_params() + + @classmethod + def get_raw_params(cls) -> RawParams: return resolve_params().to_raw_params() @classmethod @@ -52,7 +56,7 @@ class TRGMSearchService(Generic[T]): @property def fields_combinations(cls): assert cls.FIELDS is not None, f"FIELDS in {cls.__name__} don't set!" - assert len(cls.FIELDS) == 0, f"FIELDS in {cls.__name__} must be not empty!" + assert len(cls.FIELDS) != 0, f"FIELDS in {cls.__name__} must be not empty!" if len(cls.FIELDS) == 1: return cls.FIELDS @@ -73,7 +77,7 @@ class TRGMSearchService(Generic[T]): @classmethod def get_object_ids_query(cls, query: str): similarity = cls.get_similarity_subquery(query) - params = cls.get_params() + params = cls.get_raw_params() return select( [cls.table.c.id], @@ -120,15 +124,14 @@ class TRGMSearchService(Generic[T]): return await queryset.filter(id__in=[r.get("id") for r in ids]).all() @classmethod - async def get(cls, query: str) -> CustomPage[T]: + async def get(cls, query: str) -> Page[T]: params = cls.get_params() authors = await cls.get_objects(query) total = await cls.get_objects_count(query) - return CustomPage( + return CustomPage.create( items=authors, total=total, - limit=params.limit, - offset=params.offset + params=params ) diff --git a/fastapi_book_server/app/services/sequence.py b/fastapi_book_server/app/services/sequence.py index 4b19bf2..225de36 100644 --- a/fastapi_book_server/app/services/sequence.py +++ b/fastapi_book_server/app/services/sequence.py @@ -4,7 +4,7 @@ from app.services.common import TRGMSearchService class SequenceTGRMSearchService(TRGMSearchService): - MODEL = Sequence + MODEL_CLASS = Sequence FIELDS = [ Sequence.Meta.table.c.name ] diff --git a/fastapi_book_server/app/views/author.py b/fastapi_book_server/app/views/author.py index 821480a..0575e53 100644 --- a/fastapi_book_server/app/views/author.py +++ b/fastapi_book_server/app/views/author.py @@ -1,6 +1,6 @@ from fastapi import APIRouter, Depends, HTTPException, status -from fastapi_pagination import Params, Page +from fastapi_pagination import Params from fastapi_pagination.ext.ormar import paginate from app.utils.pagination import CustomPage @@ -18,7 +18,7 @@ author_router = APIRouter( ) -@author_router.get("/", response_model=Page[Author], dependencies=[Depends(Params)]) +@author_router.get("/", response_model=CustomPage[Author], dependencies=[Depends(Params)]) async def get_authors(): return await paginate( AuthorDB.objects.prefetch_related("source") @@ -80,6 +80,6 @@ async def get_translated_books(id: int): ) -@author_router.get("/search/{query}", response_model=Page[Author], dependencies=[Depends(Params)]) +@author_router.get("/search/{query}", response_model=CustomPage[Author], dependencies=[Depends(Params)]) async def search_authors(query: str): return await AuthorTGRMSearchService.get(query)