From 5f5ded68ecbdd38598790a95954ea686804c5071 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 10:29:38 +0100 Subject: [PATCH] Update code in endpoints/aiderman.post.py --- endpoints/aiderman.post.py | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 endpoints/aiderman.post.py diff --git a/endpoints/aiderman.post.py b/endpoints/aiderman.post.py new file mode 100644 index 0000000..d34808e --- /dev/null +++ b/endpoints/aiderman.post.py @@ -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. \ No newline at end of file