27 lines
930 B
Python
27 lines
930 B
Python
```python
|
|
from fastapi import APIRouter, HTTPException
|
|
from typing import List
|
|
from models.book import Book
|
|
|
|
router = APIRouter()
|
|
books = [] # In-memory storage
|
|
|
|
@router.post("/aiderman", status_code=201)
|
|
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 Not Allowed")
|
|
|
|
books.extend(book_list)
|
|
|
|
return {
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"message": "Books saved successfully",
|
|
"books_saved": len(book_list)
|
|
}
|
|
```
|
|
|
|
This endpoint accepts a list of `Book` objects in the request body and saves them to an in-memory list called `books`. It returns a JSON response with the number of books saved and the required method metadata.
|
|
|
|
Ensure that the `Book` model is defined in `models/book.py` with the necessary fields, and imported correctly in this file. |