mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Code clean up
This commit is contained in:
@@ -1,28 +1,17 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import APIRouter, Depends, Request, HTTPException, status
|
||||
|
||||
from fastapi_pagination import Params
|
||||
|
||||
from app.depends import check_token, get_allowed_langs
|
||||
from app.filters.book import get_book_filter
|
||||
from app.models import Author as AuthorDB
|
||||
from app.models import Book as BookDB
|
||||
from app.models import BookAnnotation as BookAnnotationDB
|
||||
from app.serializers.book import (
|
||||
Book,
|
||||
RemoteBook,
|
||||
BookDetail,
|
||||
CreateBook,
|
||||
UpdateBook,
|
||||
CreateRemoteBook,
|
||||
)
|
||||
from app.serializers.book import Book, RemoteBook, BookDetail
|
||||
from app.serializers.book_annotation import BookAnnotation
|
||||
from app.services.book import (
|
||||
BookMeiliSearchService,
|
||||
BookFilterService,
|
||||
GetRandomBookService,
|
||||
BookCreator,
|
||||
)
|
||||
from app.utils.pagination import CustomPage
|
||||
|
||||
@@ -49,17 +38,6 @@ async def get_books(
|
||||
return await BookFilterService.get(book_filter, request.app.state.redis)
|
||||
|
||||
|
||||
@book_router.post("/", response_model=Book)
|
||||
async def create_book(data: Union[CreateBook, CreateRemoteBook]):
|
||||
book = await BookCreator.create(data)
|
||||
|
||||
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(
|
||||
request: Request,
|
||||
@@ -110,33 +88,6 @@ async def get_remote_book(source_id: int, remote_id: int):
|
||||
return book
|
||||
|
||||
|
||||
@book_router.put("/{id}", response_model=Book)
|
||||
async def update_book(id: int, data: UpdateBook):
|
||||
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)
|
||||
|
||||
for author in list(book.authors):
|
||||
await book.authors.remove(author)
|
||||
|
||||
data_dict = data.dict()
|
||||
|
||||
author_ids = data_dict.pop("authors", [])
|
||||
authors = await AuthorDB.objects.filter(id__in=author_ids).all()
|
||||
|
||||
book = await BookDB.objects.create(**data_dict)
|
||||
|
||||
for author in authors:
|
||||
await book.authors.add(author)
|
||||
|
||||
return book
|
||||
|
||||
|
||||
@book_router.get("/{id}/annotation", response_model=BookAnnotation)
|
||||
async def get_book_annotation(id: int):
|
||||
annotation = await BookAnnotationDB.objects.get(book__id=id)
|
||||
|
||||
Reference in New Issue
Block a user