Add GET endpoint for books
This commit is contained in:
parent
414c076d9b
commit
9bcc11aa0b
37
endpoints/books.get.py
Normal file
37
endpoints/books.get.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user