47 lines
1.2 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from uuid import UUID
from core.database import get_db
from schemas.book import BookSchema, BookUpdate
from helpers.book_helpers import update_book, get_book_by_id
router = APIRouter()
@router.put("/books", response_model=BookSchema)
async def update_book_endpoint(
book_id: UUID,
book_data: BookUpdate,
db: Session = Depends(get_db)
):
"""
Update an existing book in the database.
Args:
book_id: UUID of the book to update
book_data: Updated book data
db: Database session
Returns:
The updated book
Raises:
HTTPException: If the book is not found
"""
# First check if the book exists
book = get_book_by_id(db, book_id)
if not book:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Book with ID {book_id} not found"
)
# Update the book
updated_book = update_book(db, book_id, book_data)
if not updated_book:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Failed to update book"
)
return updated_book