49 lines
1.4 KiB
Python
49 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()
|
|
|
|
fake_books_db = {
|
|
"1": {"id": "1", "title": "Book 1", "author": "Author 1", "year": 2021},
|
|
"2": {"id": "2", "title": "Book 2", "author": "Author 2", "year": 2022},
|
|
"3": {"id": "3", "title": "Book 3", "author": "Author 3", "year": 2023}
|
|
}
|
|
|
|
@router.post("/api/v1/endpoint")
|
|
async def get_books(
|
|
author: Optional[str] = None,
|
|
year: Optional[int] = None
|
|
):
|
|
"""Get books from database with optional filters"""
|
|
filtered_books = fake_books_db.copy()
|
|
|
|
if author:
|
|
filtered_books = {
|
|
k: v for k, v in filtered_books.items()
|
|
if v["author"].lower() == author.lower()
|
|
}
|
|
|
|
if year:
|
|
filtered_books = {
|
|
k: v for k, v in filtered_books.items()
|
|
if v["year"] == year
|
|
}
|
|
|
|
if not filtered_books:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="No books found matching the criteria"
|
|
)
|
|
|
|
return {
|
|
"message": "Books retrieved successfully",
|
|
"data": list(filtered_books.values()),
|
|
"metadata": {
|
|
"total_count": len(filtered_books),
|
|
"filters_applied": {
|
|
"author": author,
|
|
"year": year
|
|
}
|
|
}
|
|
} |