Update code in endpoints/aiderman.post.py

This commit is contained in:
Backend IM Bot 2025-03-25 10:29:38 +01:00
parent 518e530efc
commit 5f5ded68ec

View File

@ -0,0 +1,39 @@
```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.