Update code in endpoints/api/v1/endpoint.post.py
This commit is contained in:
parent
3497054382
commit
dc487de8df
49
endpoints/api/v1/endpoint.post.py
Normal file
49
endpoints/api/v1/endpoint.post.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user