From 9bcc11aa0b121b91439c7b9aed20798483f497b7 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 17:28:00 +0100 Subject: [PATCH] Add GET endpoint for books --- endpoints/books.get.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 endpoints/books.get.py diff --git a/endpoints/books.get.py b/endpoints/books.get.py new file mode 100644 index 0000000..c0b5cf3 --- /dev/null +++ b/endpoints/books.get.py @@ -0,0 +1,37 @@ +# 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 \ No newline at end of file