53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from core.database import fake_users_db
|
|
from typing import List, Optional
|
|
|
|
router = APIRouter()
|
|
|
|
# Simulated book database
|
|
fake_books_db = {
|
|
"1": {"id": "1", "title": "Sample Book 1", "author": "Author 1", "year": 2023},
|
|
"2": {"id": "2", "title": "Sample Book 2", "author": "Author 2", "year": 2022},
|
|
}
|
|
|
|
@router.get("/books")
|
|
async def get_books(
|
|
skip: int = 0,
|
|
limit: int = 10,
|
|
search: Optional[str] = None
|
|
):
|
|
"""Get list of books with optional pagination and search"""
|
|
books = list(fake_books_db.values())
|
|
|
|
if search:
|
|
books = [
|
|
book for book in books
|
|
if search.lower() in book["title"].lower() or search.lower() in book["author"].lower()
|
|
]
|
|
|
|
return {
|
|
"message": "Books retrieved successfully",
|
|
"data": books[skip:skip + limit],
|
|
"metadata": {
|
|
"total": len(books),
|
|
"skip": skip,
|
|
"limit": limit
|
|
}
|
|
}
|
|
|
|
@router.get("/books/{book_id}")
|
|
async def get_book(
|
|
book_id: str
|
|
):
|
|
"""Get specific book by ID"""
|
|
if book_id not in fake_books_db:
|
|
raise HTTPException(status_code=404, detail="Book not found")
|
|
|
|
return {
|
|
"message": "Book retrieved successfully",
|
|
"data": fake_books_db[book_id],
|
|
"metadata": {
|
|
"source": "demo_db",
|
|
"result_count": 1
|
|
}
|
|
} |