37 lines
969 B
Python
37 lines
969 B
Python
# Entity: Book
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from app.api.db.database import get_db
|
|
# You'll need to add correct model and schema imports
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/books")
|
|
async def get_books(
|
|
title: str | None = None,
|
|
author: str | None = None,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Get all books with optional filtering by title and author.
|
|
|
|
Args:
|
|
title (str, optional): Filter books by title.
|
|
author (str, optional): Filter books by author.
|
|
db (Session, Depends): Database session object.
|
|
|
|
Returns:
|
|
List[BookSchema]: List of book objects matching the filters.
|
|
"""
|
|
query = db.query(Book)
|
|
|
|
if title:
|
|
query = query.filter(Book.title.ilike(f"%{title}%"))
|
|
|
|
if author:
|
|
query = query.filter(Book.author.ilike(f"%{author}%"))
|
|
|
|
books = query.all()
|
|
|
|
return books |