mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 07:05:36 +01:00
Add genre search endpoint
This commit is contained in:
10
fastapi_book_server/app/services/genre.py
Normal file
10
fastapi_book_server/app/services/genre.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from app.models import Genre
|
||||
from app.services.common import MeiliSearchService
|
||||
|
||||
|
||||
class GenreMeiliSearchService(MeiliSearchService):
|
||||
MODEL_CLASS = Genre
|
||||
PREFETCH_RELATED = ["source"]
|
||||
|
||||
MS_INDEX_NAME = "genres"
|
||||
MS_INDEX_LANG_KEY = "langs"
|
||||
@@ -1,11 +1,13 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Request
|
||||
|
||||
from fastapi_pagination import Params, Page
|
||||
from fastapi_pagination import Params
|
||||
from fastapi_pagination.ext.ormar import paginate
|
||||
|
||||
from app.depends import check_token
|
||||
from app.depends import check_token, get_allowed_langs
|
||||
from app.models import Genre as GenreDB
|
||||
from app.serializers.genre import Genre
|
||||
from app.services.genre import GenreMeiliSearchService
|
||||
from app.utils.pagination import CustomPage
|
||||
|
||||
|
||||
genre_router = APIRouter(
|
||||
@@ -16,7 +18,7 @@ genre_router = APIRouter(
|
||||
PREFETCH_RELATED_FIELDS = ["source"]
|
||||
|
||||
|
||||
@genre_router.get("/", response_model=Page[Genre], dependencies=[Depends(Params)])
|
||||
@genre_router.get("/", response_model=CustomPage[Genre], dependencies=[Depends(Params)])
|
||||
async def get_genres():
|
||||
return await paginate(GenreDB.objects.prefetch_related(PREFETCH_RELATED_FIELDS))
|
||||
|
||||
@@ -31,3 +33,17 @@ async def get_genre(id: int):
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||
|
||||
return genre
|
||||
|
||||
|
||||
@genre_router.get(
|
||||
"/search/{query}", response_model=CustomPage[Genre], dependencies=[Depends(Params)]
|
||||
)
|
||||
async def search_genres(
|
||||
query: str,
|
||||
request: Request,
|
||||
allowed_langs: frozenset[str] = Depends(get_allowed_langs),
|
||||
):
|
||||
return await GenreMeiliSearchService.get(
|
||||
{"query": query, "allowed_langs": allowed_langs},
|
||||
request.app.state.redis,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user