mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Init
This commit is contained in:
49
fastapi_book_server/app/views/book_annotation.py
Normal file
49
fastapi_book_server/app/views/book_annotation.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
|
||||
from fastapi_pagination import Params, Page
|
||||
from fastapi_pagination.ext.ormar import paginate
|
||||
|
||||
from app.models import BookAnnotation as BookAnnotationDB
|
||||
from app.serializers.book_annotation import BookAnnotation, CreateBookAnnotation, UpdateBookAnnotation
|
||||
|
||||
|
||||
book_annotation_router = APIRouter(
|
||||
prefix="/api/v1/book_annotations",
|
||||
tags=["book_annotation"]
|
||||
)
|
||||
|
||||
|
||||
@book_annotation_router.get("/", response_model=Page[BookAnnotation], dependencies=[Depends(Params)])
|
||||
async def get_book_annotations():
|
||||
return await paginate(
|
||||
BookAnnotationDB.objects
|
||||
)
|
||||
|
||||
|
||||
@book_annotation_router.post("/", response_model=BookAnnotation)
|
||||
async def create_book_annotation(data: CreateBookAnnotation):
|
||||
return await BookAnnotationDB.objects.create(
|
||||
**data.dict()
|
||||
)
|
||||
|
||||
|
||||
@book_annotation_router.get("/{id}", response_model=BookAnnotation)
|
||||
async def get_book_annotation(id: int):
|
||||
annotation = await BookAnnotationDB.objects.get_or_none(id=id)
|
||||
|
||||
if annotation is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||
|
||||
return annotation
|
||||
|
||||
|
||||
@book_annotation_router.put("/{id}", response_model=BookAnnotation)
|
||||
async def update_book_annotation(id: int, data: UpdateBookAnnotation):
|
||||
annotation = await BookAnnotationDB.objects.get_or_none(id=id)
|
||||
|
||||
if annotation is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||
|
||||
annotation.update_from_dict(data.dict())
|
||||
|
||||
return annotation.save()
|
||||
Reference in New Issue
Block a user