diff --git a/fastapi_book_server/app/models.py b/fastapi_book_server/app/models.py index b87c5c0..83e5023 100644 --- a/fastapi_book_server/app/models.py +++ b/fastapi_book_server/app/models.py @@ -53,6 +53,10 @@ class Author(ormar.Model): last_name: str = ormar.String(max_length=256, nullable=False) # type: ignore middle_name: str = ormar.String(max_length=256, nullable=True) # type: ignore + @ormar.property_field + def annotation_exists(self) -> bool: + return len(self.annotations) != 0 + class AuthorAnnotation(ormar.Model): class Meta(BaseMeta): @@ -60,7 +64,7 @@ class AuthorAnnotation(ormar.Model): id = ormar.Integer(primary_key=True, nullable=False) - author: Author = ormar.ForeignKey(Author, nullable=False, unique=True) + author: Author = ormar.ForeignKey(Author, nullable=False, unique=True, related_name="annotations") title: str = ormar.String(max_length=256, nullable=False, default="") # type: ignore text: str = ormar.Text(nullable=False, default="") # type: ignore @@ -147,6 +151,10 @@ class Book(ormar.Model): return [self.file_type] + @ormar.property_field + def annotation_exists(self) -> bool: + return len(self.annotations) != 0 + class BookAnnotation(ormar.Model): class Meta(BaseMeta): @@ -154,7 +162,7 @@ class BookAnnotation(ormar.Model): id = ormar.Integer(primary_key=True, nullable=False) - book: Book = ormar.ForeignKey(Book, nullable=False, unique=True) + book: Book = ormar.ForeignKey(Book, nullable=False, unique=True, related_name="annotations") title: str = ormar.String(max_length=256, nullable=False, default="") # type: ignore text: str = ormar.Text(nullable=False, default="") # type: ignore diff --git a/fastapi_book_server/app/serializers/author.py b/fastapi_book_server/app/serializers/author.py index 8112ce0..1e08d87 100644 --- a/fastapi_book_server/app/serializers/author.py +++ b/fastapi_book_server/app/serializers/author.py @@ -12,6 +12,8 @@ class Author(BaseModel): last_name: str middle_name: Optional[str] + 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 69b2646..0b3ca21 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] + annotation_exists: bool class Config(ORJSONConfig): pass diff --git a/fastapi_book_server/app/services/author.py b/fastapi_book_server/app/services/author.py index be3c6e8..82cbdb6 100644 --- a/fastapi_book_server/app/services/author.py +++ b/fastapi_book_server/app/services/author.py @@ -10,4 +10,4 @@ class AuthorTGRMSearchService(TRGMSearchService): Author.Meta.table.c.first_name, Author.Meta.table.c.middle_name ] - PREFETCH_RELATED = ["source"] + PREFETCH_RELATED = ["source", "annotations"] diff --git a/fastapi_book_server/app/services/book.py b/fastapi_book_server/app/services/book.py index cae5759..edc5d63 100644 --- a/fastapi_book_server/app/services/book.py +++ b/fastapi_book_server/app/services/book.py @@ -13,7 +13,7 @@ class BookTGRMSearchService(TRGMSearchService): FIELDS = [ BookDB.Meta.table.c.title ] - PREFETCH_RELATED = ["source", "authors"] + PREFETCH_RELATED = ["source", "authors", "annotations"] FILTERS = [ BookDB.Meta.table.c.is_deleted == False, ] diff --git a/fastapi_book_server/app/views/author.py b/fastapi_book_server/app/views/author.py index e886e87..31b54c9 100644 --- a/fastapi_book_server/app/views/author.py +++ b/fastapi_book_server/app/views/author.py @@ -21,7 +21,7 @@ author_router = APIRouter( @author_router.get("/", response_model=CustomPage[Author], dependencies=[Depends(Params)]) async def get_authors(): return await paginate( - AuthorDB.objects.prefetch_related("source") + AuthorDB.objects.prefetch_related(["source", "annotations"]) ) @@ -31,12 +31,12 @@ async def create_author(data: CreateAuthor): **data.dict() ) - return await AuthorDB.objects.prefetch_related("source").get(id=author.id) + return await AuthorDB.objects.prefetch_related(["source", "annotations"]).get(id=author.id) @author_router.get("/{id}", response_model=Author) async def get_author(id: int): - author = await AuthorDB.objects.prefetch_related("source").get_or_none(id=id) + author = await AuthorDB.objects.prefetch_related(["source", "annotations"]).get_or_none(id=id) if author is None: raise HTTPException(status.HTTP_404_NOT_FOUND) diff --git a/fastapi_book_server/app/views/book.py b/fastapi_book_server/app/views/book.py index f7d781f..128c7be 100644 --- a/fastapi_book_server/app/views/book.py +++ b/fastapi_book_server/app/views/book.py @@ -23,7 +23,7 @@ book_router = APIRouter( @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"]).filter(**book_filter) + BookDB.objects.select_related(["source", "authors", "annotations"]).filter(**book_filter) ) @@ -31,12 +31,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"]).get(id=book.id) + return await BookDB.objects.select_related(["source", "authors", "annotations"]).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"]).get_or_none(id=id) + book = await BookDB.objects.select_related(["source", "authors", "annotations"]).get_or_none(id=id) if book is None: raise HTTPException(status.HTTP_404_NOT_FOUND) @@ -46,7 +46,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"]).get_or_none( + book = await BookDB.objects.select_related(["source", "authors", "annotations"]).get_or_none( source=source_id, remote_id=remote_id ) @@ -59,7 +59,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"]).get_or_none(id=id) + book = await BookDB.objects.select_related(["source", "authors", "annotations"]).get_or_none(id=id) if book is None: raise HTTPException(status.HTTP_404_NOT_FOUND)