Update code in endpoints/aiderman.post.py
This commit is contained in:
parent
518e530efc
commit
5f5ded68ec
39
endpoints/aiderman.post.py
Normal file
39
endpoints/aiderman.post.py
Normal 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.
|
Loading…
x
Reference in New Issue
Block a user