mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-08 09:30:43 +01:00
Fix joins and subqueries
This commit is contained in:
@@ -27,34 +27,49 @@ author_router = APIRouter(
|
||||
)
|
||||
|
||||
|
||||
PREFETCH_RELATED = ["source", "annotations"]
|
||||
SELECT_RELATED_FIELDS = ["source"]
|
||||
PREFETCH_RELATED_FIELDS = ["annotations"]
|
||||
|
||||
|
||||
@author_router.get(
|
||||
"/", response_model=CustomPage[Author], dependencies=[Depends(Params)]
|
||||
)
|
||||
async def get_authors():
|
||||
return await paginate(AuthorDB.objects.prefetch_related(PREFETCH_RELATED))
|
||||
return await paginate(
|
||||
AuthorDB.objects.select_related(SELECT_RELATED_FIELDS).prefetch_related(
|
||||
PREFETCH_RELATED_FIELDS
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@author_router.post("/", response_model=Author, dependencies=[Depends(Params)])
|
||||
async def create_author(data: CreateAuthor):
|
||||
author = await AuthorDB.objects.create(**data.dict())
|
||||
|
||||
return await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get(id=author.id)
|
||||
return (
|
||||
await AuthorDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||
.get(id=author.id)
|
||||
)
|
||||
|
||||
|
||||
@author_router.get("/random", response_model=Author)
|
||||
async def get_random_author(allowed_langs: list[str] = Depends(get_allowed_langs)):
|
||||
author_id = await GetRandomAuthorService.get_random_id(allowed_langs)
|
||||
|
||||
return await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get(id=author_id)
|
||||
return (
|
||||
await AuthorDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||
.get(id=author_id)
|
||||
)
|
||||
|
||||
|
||||
@author_router.get("/{id}", response_model=Author)
|
||||
async def get_author(id: int):
|
||||
author = await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get_or_none(
|
||||
id=id
|
||||
author = (
|
||||
await AuthorDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||
.get_or_none(id=id)
|
||||
)
|
||||
|
||||
if author is None:
|
||||
@@ -92,7 +107,8 @@ async def get_author_books(
|
||||
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
|
||||
):
|
||||
return await paginate(
|
||||
BookDB.objects.select_related(["source", "annotations", "translators"])
|
||||
BookDB.objects.select_related(["source"])
|
||||
.prefetch_related(["annotations", "translators"])
|
||||
.filter(authors__id=id, lang__in=allowed_langs, is_deleted=False)
|
||||
.order_by("title")
|
||||
)
|
||||
@@ -121,7 +137,9 @@ async def get_translated_books(
|
||||
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
|
||||
):
|
||||
return await paginate(
|
||||
BookDB.objects.select_related(["source", "annotations", "authors"]).filter(
|
||||
BookDB.objects.select_related(["source"])
|
||||
.prefetch_related(["annotations", "authors"])
|
||||
.filter(
|
||||
translators__id=id,
|
||||
lang__in=allowed_langs,
|
||||
is_deleted=False,
|
||||
|
||||
@@ -29,8 +29,8 @@ book_router = APIRouter(
|
||||
dependencies=[Depends(check_token)],
|
||||
)
|
||||
|
||||
|
||||
SELECT_RELATED_FIELDS = ["source", "authors", "translators", "annotations"]
|
||||
SELECT_RELATED_FIELDS = ["source"]
|
||||
PREFETCH_RELATED_FIELDS = ["authors", "translators", "annotations"]
|
||||
|
||||
|
||||
@book_router.get(
|
||||
@@ -38,7 +38,9 @@ SELECT_RELATED_FIELDS = ["source", "authors", "translators", "annotations"]
|
||||
)
|
||||
async def get_books(book_filter: dict = Depends(get_book_filter)):
|
||||
return await paginate(
|
||||
BookDB.objects.select_related(SELECT_RELATED_FIELDS).filter(**book_filter)
|
||||
BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||
.filter(**book_filter)
|
||||
)
|
||||
|
||||
|
||||
@@ -46,19 +48,31 @@ async def get_books(book_filter: dict = Depends(get_book_filter)):
|
||||
async def create_book(data: Union[CreateBook, CreateRemoteBook]):
|
||||
book = await BookCreator.create(data)
|
||||
|
||||
return await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get(id=book.id)
|
||||
return (
|
||||
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||
.get(id=book.id)
|
||||
)
|
||||
|
||||
|
||||
@book_router.get("/random", response_model=BookDetail)
|
||||
async def get_random_book(allowed_langs: list[str] = Depends(get_allowed_langs)):
|
||||
book_id = await GetRandomBookService.get_random_id(allowed_langs)
|
||||
|
||||
return await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get(id=book_id)
|
||||
return (
|
||||
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||
.get(id=book_id)
|
||||
)
|
||||
|
||||
|
||||
@book_router.get("/{id}", response_model=BookDetail)
|
||||
async def get_book(id: int):
|
||||
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id)
|
||||
book = (
|
||||
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||
.get_or_none(id=id)
|
||||
)
|
||||
|
||||
if book is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||
@@ -68,8 +82,10 @@ async def get_book(id: int):
|
||||
|
||||
@book_router.get("/remote/{source_id}/{remote_id}", response_model=Book)
|
||||
async def get_remote_book(source_id: int, remote_id: int):
|
||||
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(
|
||||
source=source_id, remote_id=remote_id
|
||||
book = (
|
||||
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||
.get_or_none(source=source_id, remote_id=remote_id)
|
||||
)
|
||||
|
||||
if book is None:
|
||||
@@ -80,7 +96,11 @@ async def get_remote_book(source_id: int, remote_id: int):
|
||||
|
||||
@book_router.put("/{id}", response_model=Book)
|
||||
async def update_book(id: int, data: UpdateBook):
|
||||
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id)
|
||||
book = (
|
||||
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
|
||||
.prefetch_related(PREFETCH_RELATED_FIELDS)
|
||||
.get_or_none(id=id)
|
||||
)
|
||||
|
||||
if book is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||
|
||||
@@ -47,9 +47,8 @@ async def get_sequence_books(
|
||||
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
|
||||
):
|
||||
return await paginate(
|
||||
BookDB.objects.select_related(
|
||||
["source", "annotations", "authors", "translators"]
|
||||
)
|
||||
BookDB.objects.select_related(["source"])
|
||||
.prefetch_related(["annotations", "authors", "translators"])
|
||||
.filter(sequences__id=id, lang__in=allowed_langs, is_deleted=False)
|
||||
.order_by("sequences__booksequences__position")
|
||||
)
|
||||
|
||||
@@ -27,21 +27,21 @@ translation_router = APIRouter(
|
||||
"/", response_model=CustomPage[Translation], dependencies=[Depends(Params)]
|
||||
)
|
||||
async def get_translations():
|
||||
return await paginate(TranslationDB.objects.prefetch_related(["book", "author"]))
|
||||
return await paginate(TranslationDB.objects.select_related(["book", "author"]))
|
||||
|
||||
|
||||
@translation_router.post("/", response_model=Translation)
|
||||
async def create_translation(data: Union[CreateTranslation, CreateRemoteTranslation]):
|
||||
translation = await TranslationCreator.create(data)
|
||||
|
||||
return await TranslationDB.objects.prefetch_related(["book", "author"]).get(
|
||||
return await TranslationDB.objects.select_related(["book", "author"]).get(
|
||||
id=translation.id
|
||||
)
|
||||
|
||||
|
||||
@translation_router.delete("/{id}", response_model=Translation)
|
||||
async def delete_translation(id: int):
|
||||
translation = await TranslationDB.objects.prefetch_related(
|
||||
translation = await TranslationDB.objects.select_related(
|
||||
["book", "author"]
|
||||
).get_or_none(id=id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user