mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 07:05:36 +01:00
Add annotation_exists field to Book and Author
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -12,6 +12,8 @@ class Author(BaseModel):
|
||||
last_name: str
|
||||
middle_name: Optional[str]
|
||||
|
||||
annotation_exists: bool
|
||||
|
||||
class Config(ORJSONConfig):
|
||||
pass
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ class Book(BaseModel):
|
||||
available_types: list[str]
|
||||
uploaded: date
|
||||
authors: list[Author]
|
||||
annotation_exists: bool
|
||||
|
||||
class Config(ORJSONConfig):
|
||||
pass
|
||||
|
||||
@@ -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"]
|
||||
|
||||
@@ -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,
|
||||
]
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user