diff --git a/fastapi_book_server/app/models.py b/fastapi_book_server/app/models.py index 8b40c58..ca63178 100644 --- a/fastapi_book_server/app/models.py +++ b/fastapi_book_server/app/models.py @@ -103,6 +103,7 @@ class BookGenres(ormar.Model): class BookSequences(ormar.Model): class Meta(BaseMeta): tablename = "book_sequences" + orders_by = ["position", ] id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore @@ -112,6 +113,7 @@ class BookSequences(ormar.Model): class Translation(ormar.Model): class Meta(BaseMeta): tablename = "translations" + orders_by = ["position", ] id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore diff --git a/fastapi_book_server/app/serializers/author.py b/fastapi_book_server/app/serializers/author.py index 3035184..fd96856 100644 --- a/fastapi_book_server/app/serializers/author.py +++ b/fastapi_book_server/app/serializers/author.py @@ -47,27 +47,21 @@ class AuthorBook(BaseModel): file_type: str available_types: list[str] uploaded: date + translators: list[Author] annotation_exists: bool class Config(ORJSONConfig): pass -class Translation(BaseModel): - translator: Author - position: int - - class Config(ORJSONConfig): - pass - - class TranslatedBook(BaseModel): id: int title: str lang: str file_type: str + available_types: list[str] authors: list[Author] - translations: list[Translation] + annotation_exists: bool class Config(ORJSONConfig): pass diff --git a/fastapi_book_server/app/serializers/book.py b/fastapi_book_server/app/serializers/book.py index 0b3ca21..4d02155 100644 --- a/fastapi_book_server/app/serializers/book.py +++ b/fastapi_book_server/app/serializers/book.py @@ -19,6 +19,7 @@ class Book(BaseModel): available_types: list[str] uploaded: date authors: list[Author] + translators: list[Author] annotation_exists: bool class Config(ORJSONConfig): diff --git a/fastapi_book_server/app/serializers/sequence.py b/fastapi_book_server/app/serializers/sequence.py index 185acd3..6aa5d1d 100644 --- a/fastapi_book_server/app/serializers/sequence.py +++ b/fastapi_book_server/app/serializers/sequence.py @@ -41,6 +41,7 @@ class Book(BaseModel): available_types: list[str] uploaded: date authors: list[Author] + translators: list[Author] annotation_exists: bool class Config(ORJSONConfig): diff --git a/fastapi_book_server/app/services/author.py b/fastapi_book_server/app/services/author.py index 70a1e00..31ebb4e 100644 --- a/fastapi_book_server/app/services/author.py +++ b/fastapi_book_server/app/services/author.py @@ -39,5 +39,5 @@ SELECT ARRAY( class AuthorTGRMSearchService(TRGMSearchService): MODEL_CLASS = Author - PREFETCH_RELATED = ["source", "annotations"] + PREFETCH_RELATED = ["source", "annotations", "translators"] GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY diff --git a/fastapi_book_server/app/services/book.py b/fastapi_book_server/app/services/book.py index 1100976..3299f36 100644 --- a/fastapi_book_server/app/services/book.py +++ b/fastapi_book_server/app/services/book.py @@ -23,7 +23,7 @@ SELECT ARRAY( class BookTGRMSearchService(TRGMSearchService): MODEL_CLASS = BookDB - PREFETCH_RELATED = ["source", "authors", "annotations"] + PREFETCH_RELATED = ["source", "authors", "translators", "annotations"] GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY diff --git a/fastapi_book_server/app/views/author.py b/fastapi_book_server/app/views/author.py index 7394505..22b3728 100644 --- a/fastapi_book_server/app/views/author.py +++ b/fastapi_book_server/app/views/author.py @@ -18,10 +18,14 @@ author_router = APIRouter( ) + +PREFETCH_RELATED = ["source", "annotations"] + + @author_router.get("/", response_model=CustomPage[Author], dependencies=[Depends(Params)]) async def get_authors(): return await paginate( - AuthorDB.objects.prefetch_related(["source", "annotations"]) + AuthorDB.objects.prefetch_related(PREFETCH_RELATED) ) @@ -31,12 +35,12 @@ async def create_author(data: CreateAuthor): **data.dict() ) - return await AuthorDB.objects.prefetch_related(["source", "annotations"]).get(id=author.id) + return await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get(id=author.id) @author_router.get("/{id}", response_model=Author) async def get_author(id: int): - author = await AuthorDB.objects.prefetch_related(["source", "annotations"]).get_or_none(id=id) + author = await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get_or_none(id=id) if author is None: raise HTTPException(status.HTTP_404_NOT_FOUND) @@ -69,14 +73,14 @@ async def get_author_annotation(id: int): @author_router.get("/{id}/books", response_model=CustomPage[AuthorBook], dependencies=[Depends(Params)]) async def get_author_books(id: int): return await paginate( - BookDB.objects.select_related(["source", "annotations"]).filter(authors__id=id).order_by('title') + BookDB.objects.select_related(["source", "annotations", "translators"]).filter(authors__id=id).order_by('title') ) @author_router.get("/{id}/translated_books", response_model=CustomPage[TranslatedBook]) async def get_translated_books(id: int): return await paginate( - BookDB.objects.select_related(["translations", "translations__translator"]).filter(translations__translator__id=id) + BookDB.objects.select_related(["source", "annotations", "translators"]).filter(translations__translator__id=id) ) diff --git a/fastapi_book_server/app/views/book.py b/fastapi_book_server/app/views/book.py index f491ac5..92f1dcf 100644 --- a/fastapi_book_server/app/views/book.py +++ b/fastapi_book_server/app/views/book.py @@ -21,10 +21,13 @@ book_router = APIRouter( ) +SELECT_RELATED_FIELDS = ["source", "authors", "translators", "annotations"] + + @book_router.get("/", response_model=CustomPage[RemoteBook], dependencies=[Depends(Params)]) async def get_books(book_filter: dict = Depends(get_book_filter)): return await paginate( - BookDB.objects.select_related(["source", "authors", "annotations"]).filter(**book_filter) + BookDB.objects.select_related(SELECT_RELATED_FIELDS).filter(**book_filter) ) @@ -32,12 +35,12 @@ 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(["source", "authors", "annotations"]).get(id=book.id) + return await BookDB.objects.select_related(SELECT_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(["source", "authors", "annotations"]).get_or_none(id=id) + book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id) if book is None: raise HTTPException(status.HTTP_404_NOT_FOUND) @@ -47,7 +50,7 @@ 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(["source", "authors", "annotations"]).get_or_none( + book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none( source=source_id, remote_id=remote_id ) @@ -60,7 +63,7 @@ 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(["source", "authors", "annotations"]).get_or_none(id=id) + book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id) if book is None: raise HTTPException(status.HTTP_404_NOT_FOUND) diff --git a/fastapi_book_server/app/views/sequence.py b/fastapi_book_server/app/views/sequence.py index 9e60b7c..3cd94b7 100644 --- a/fastapi_book_server/app/views/sequence.py +++ b/fastapi_book_server/app/views/sequence.py @@ -32,7 +32,7 @@ async def get_sequence(id: int): @sequence_router.get("/{id}/books", response_model=CustomPage[SequenceBook], dependencies=[Depends(Params)]) async def get_sequence_books(id: int): return await paginate( - BookDB.objects.select_related(["source", "annotations", "authors"]) + BookDB.objects.select_related(["source", "annotations", "authors", "translators"]) .filter(sequences__id=id).order_by("sequences__booksequences__position") ) diff --git a/fastapi_book_server/app/views/translation.py b/fastapi_book_server/app/views/translation.py index 3a3ccc7..3850346 100644 --- a/fastapi_book_server/app/views/translation.py +++ b/fastapi_book_server/app/views/translation.py @@ -22,7 +22,7 @@ translation_router = APIRouter( @translation_router.get("/", response_model=CustomPage[Translation], dependencies=[Depends(Params)]) async def get_translations(): return await paginate( - TranslationDB.objects.prefetch_related(["book", "translator"]) + TranslationDB.objects.prefetch_related(["book", "author"]) ) @@ -30,12 +30,12 @@ async def get_translations(): async def create_translation(data: Union[CreateTranslation, CreateRemoteTranslation]): translation = await TranslationCreator.create(data) - return await TranslationDB.objects.prefetch_related(["book", "translator"]).get(id=translation.id) + return await TranslationDB.objects.prefetch_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(["book", "translator"]).get_or_none(id=id) + translation = await TranslationDB.objects.prefetch_related(["book", "author"]).get_or_none(id=id) if translation is None: raise HTTPException(status.HTTP_404_NOT_FOUND)