from typing import Optional from uuid import UUID from sqlalchemy.orm import Session from models.book import Book from schemas.book import BookCreate def create_book(db: Session, book_data: BookCreate) -> Book: """ Creates a new book in the database. Args: db (Session): The database session. book_data (BookCreate): The data for the book to create. Returns: Book: The newly created book object. """ db_book = Book(**book_data.dict()) db.add(db_book) db.commit() db.refresh(db_book) return db_book def get_book_by_id(db: Session, book_id: UUID) -> Optional[Book]: """ Retrieves a single book by its ID. Args: db (Session): The database session. book_id (UUID): The ID of the book to retrieve. Returns: Optional[Book]: The book object if found, otherwise None. """ return db.query(Book).filter(Book.id == book_id).first()