Add genres to detail book

This commit is contained in:
2022-04-08 12:34:44 +03:00
parent 531211ae03
commit 21de16ede7
2 changed files with 15 additions and 3 deletions

View File

@@ -3,8 +3,8 @@ from datetime import date
from pydantic import BaseModel from pydantic import BaseModel
from app.serializers.author import Author from app.serializers.author import Author
from app.serializers.sequence import Sequence
from app.serializers.orjson_config import ORJSONConfig from app.serializers.orjson_config import ORJSONConfig
from app.serializers.sequence import Sequence
class BookSource(BaseModel): class BookSource(BaseModel):
@@ -12,6 +12,11 @@ class BookSource(BaseModel):
name: str name: str
class BookGenre(BaseModel):
id: int
description: str
class Book(BaseModel): class Book(BaseModel):
id: int id: int
title: str title: str
@@ -34,6 +39,7 @@ class RemoteBook(Book):
class BookDetail(RemoteBook): class BookDetail(RemoteBook):
sequences: list[Sequence] sequences: list[Sequence]
genres: list[BookGenre]
is_deleted: bool is_deleted: bool

View File

@@ -36,6 +36,8 @@ book_router = APIRouter(
PREFETCH_RELATED_FIELDS = ["source"] PREFETCH_RELATED_FIELDS = ["source"]
SELECT_RELATED_FIELDS = ["authors", "translators", "annotations"] SELECT_RELATED_FIELDS = ["authors", "translators", "annotations"]
DETAIL_SELECT_RELATED_FIELDS = ["sequences", "genres"]
@book_router.get( @book_router.get(
"/", response_model=CustomPage[RemoteBook], dependencies=[Depends(Params)] "/", response_model=CustomPage[RemoteBook], dependencies=[Depends(Params)]
@@ -68,7 +70,9 @@ async def get_random_book(
) )
book = ( book = (
await BookDB.objects.select_related(SELECT_RELATED_FIELDS + ["sequences"]) await BookDB.objects.select_related(
SELECT_RELATED_FIELDS + DETAIL_SELECT_RELATED_FIELDS
)
.prefetch_related(PREFETCH_RELATED_FIELDS) .prefetch_related(PREFETCH_RELATED_FIELDS)
.get(id=book_id) .get(id=book_id)
) )
@@ -79,7 +83,9 @@ async def get_random_book(
@book_router.get("/{id}", response_model=BookDetail) @book_router.get("/{id}", response_model=BookDetail)
async def get_book(id: int): async def get_book(id: int):
book = ( book = (
await BookDB.objects.select_related(SELECT_RELATED_FIELDS + ["sequences"]) await BookDB.objects.select_related(
SELECT_RELATED_FIELDS + DETAIL_SELECT_RELATED_FIELDS
)
.prefetch_related(PREFETCH_RELATED_FIELDS) .prefetch_related(PREFETCH_RELATED_FIELDS)
.get_or_none(id=id) .get_or_none(id=id)
) )