From 3896a0cc2ae9c3235b6b23d6fc039860ef63924d Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 08:40:29 +0100 Subject: [PATCH] Update code in endpoints\books.post.py --- endpoints/books.post.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 endpoints/books.post.py diff --git a/endpoints/books.post.py b/endpoints/books.post.py new file mode 100644 index 0000000..74257f2 --- /dev/null +++ b/endpoints/books.post.py @@ -0,0 +1,34 @@ +from fastapi import APIRouter, HTTPException +import uuid + +books = [] # In-memory storage + +router = APIRouter() + +@router.post("/books") +async def create_book( + title: str, + author: str, + description: str = None +): + """Create a new book""" + if request.method != "POST": + raise HTTPException(status_code=405, detail="Method Not Allowed") + + book_id = str(uuid.uuid4()) + books.append({ + "id": book_id, + "title": title, + "author": author, + "description": description + }) + + return { + "message": "Book created successfully", + "book_id": book_id, + "method": "POST", + "_verb": "post" + } +``` + +This code defines a POST endpoint at `/books` that creates a new book entry in an in-memory list called `books`. It takes the `title` and `author` as required parameters, and an optional `description`. It generates a unique `book_id` using `uuid.uuid4()` and appends a new dictionary to the `books` list with the provided data. The response includes a success message, the generated `book_id`, and the expected `method` and `_verb` fields. \ No newline at end of file