39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
```python
|
|
from fastapi import APIRouter, HTTPException
|
|
from typing import List
|
|
from pydantic import BaseModel
|
|
|
|
books = [] # In-memory storage
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
@router.post("/aiderman")
|
|
async def save_books(book_list: List[Book]):
|
|
"""Save a list of books to the database"""
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail={
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"error": "Method Not Allowed"
|
|
})
|
|
|
|
for book in book_list:
|
|
books.append({
|
|
"title": book.title,
|
|
"author": book.author,
|
|
"year": book.year
|
|
})
|
|
|
|
return {
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"message": "Books saved successfully",
|
|
"count": len(book_list)
|
|
}
|
|
```
|
|
|
|
This endpoint accepts a list of Book objects in the request body, validates the HTTP method, and saves the books to an in-memory list called `books`. It returns a success message with the method details and the count of saved books.
|
|
|
|
Note: This is an example implementation using an in-memory list for storage. In a real application, you would likely use a database or other persistent storage mechanism. |