mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 07:05:36 +01:00
Fix bugs
This commit is contained in:
@@ -4,7 +4,7 @@ from app.services.common import TRGMSearchService
|
|||||||
|
|
||||||
|
|
||||||
class AuthorTGRMSearchService(TRGMSearchService):
|
class AuthorTGRMSearchService(TRGMSearchService):
|
||||||
MODEL = Author
|
MODEL_CLASS = Author
|
||||||
FIELDS = [
|
FIELDS = [
|
||||||
Author.Meta.table.c.last_name,
|
Author.Meta.table.c.last_name,
|
||||||
Author.Meta.table.c.first_name,
|
Author.Meta.table.c.first_name,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from app.serializers.book import CreateBook, CreateRemoteBook
|
|||||||
|
|
||||||
|
|
||||||
class BookTGRMSearchService(TRGMSearchService):
|
class BookTGRMSearchService(TRGMSearchService):
|
||||||
MODEL = BookDB
|
MODEL_CLASS = BookDB
|
||||||
FIELDS = [
|
FIELDS = [
|
||||||
BookDB.Meta.table.c.title
|
BookDB.Meta.table.c.title
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -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 itertools import permutations
|
||||||
|
|
||||||
from fastapi_pagination.api import resolve_params
|
from fastapi_pagination.api import resolve_params
|
||||||
from fastapi_pagination.bases import RawParams
|
from fastapi_pagination.bases import AbstractParams, RawParams
|
||||||
from app.utils.pagination import CustomPage
|
from app.utils.pagination import Page, CustomPage
|
||||||
|
|
||||||
from ormar import Model, QuerySet
|
from ormar import Model, QuerySet
|
||||||
from sqlalchemy import text, func, select, desc, Table, Column
|
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
|
PREFETCH_RELATED: Optional[Union[list[str], str]] = None
|
||||||
|
|
||||||
@classmethod
|
@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()
|
return resolve_params().to_raw_params()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -52,7 +56,7 @@ class TRGMSearchService(Generic[T]):
|
|||||||
@property
|
@property
|
||||||
def fields_combinations(cls):
|
def fields_combinations(cls):
|
||||||
assert cls.FIELDS is not None, f"FIELDS in {cls.__name__} don't set!"
|
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:
|
if len(cls.FIELDS) == 1:
|
||||||
return cls.FIELDS
|
return cls.FIELDS
|
||||||
@@ -73,7 +77,7 @@ class TRGMSearchService(Generic[T]):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def get_object_ids_query(cls, query: str):
|
def get_object_ids_query(cls, query: str):
|
||||||
similarity = cls.get_similarity_subquery(query)
|
similarity = cls.get_similarity_subquery(query)
|
||||||
params = cls.get_params()
|
params = cls.get_raw_params()
|
||||||
|
|
||||||
return select(
|
return select(
|
||||||
[cls.table.c.id],
|
[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()
|
return await queryset.filter(id__in=[r.get("id") for r in ids]).all()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def get(cls, query: str) -> CustomPage[T]:
|
async def get(cls, query: str) -> Page[T]:
|
||||||
params = cls.get_params()
|
params = cls.get_params()
|
||||||
|
|
||||||
authors = await cls.get_objects(query)
|
authors = await cls.get_objects(query)
|
||||||
total = await cls.get_objects_count(query)
|
total = await cls.get_objects_count(query)
|
||||||
|
|
||||||
return CustomPage(
|
return CustomPage.create(
|
||||||
items=authors,
|
items=authors,
|
||||||
total=total,
|
total=total,
|
||||||
limit=params.limit,
|
params=params
|
||||||
offset=params.offset
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from app.services.common import TRGMSearchService
|
|||||||
|
|
||||||
|
|
||||||
class SequenceTGRMSearchService(TRGMSearchService):
|
class SequenceTGRMSearchService(TRGMSearchService):
|
||||||
MODEL = Sequence
|
MODEL_CLASS = Sequence
|
||||||
FIELDS = [
|
FIELDS = [
|
||||||
Sequence.Meta.table.c.name
|
Sequence.Meta.table.c.name
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
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 fastapi_pagination.ext.ormar import paginate
|
||||||
from app.utils.pagination import CustomPage
|
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():
|
async def get_authors():
|
||||||
return await paginate(
|
return await paginate(
|
||||||
AuthorDB.objects.prefetch_related("source")
|
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):
|
async def search_authors(query: str):
|
||||||
return await AuthorTGRMSearchService.get(query)
|
return await AuthorTGRMSearchService.get(query)
|
||||||
|
|||||||
Reference in New Issue
Block a user