From 64abc19b625d04e594c7d5e71e7e889b4e75250e Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 00:17:01 +0100 Subject: [PATCH] Update code in endpoints/bookies.post.py --- endpoints/bookies.post.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 endpoints/bookies.post.py diff --git a/endpoints/bookies.post.py b/endpoints/bookies.post.py new file mode 100644 index 0000000..8d7f784 --- /dev/null +++ b/endpoints/bookies.post.py @@ -0,0 +1,28 @@ +from typing import Optional +from pydantic import BaseModel +from fastapi import APIRouter, HTTPException + +router = APIRouter() + +# In-memory storage +books = [] + +@router.post("/bookies", status_code=201) +async def create_book(book: BookSchema): + """Create a new book""" + if request.method != "POST": + raise HTTPException(status_code=405, detail="Method Not Allowed") + + book_data = book.dict() + book_data["id"] = len(books) + 1 + books.append(book_data) + + return { + "method": "POST", + "_verb": "post", + "message": "Book created successfully", + "book": book_data + } +``` + +This code defines a POST endpoint `/bookies` that creates a new book in the in-memory `books` list. It uses the `BookSchema` model from Pydantic to validate the request body data. The response includes the created book data along with the HTTP method information. \ No newline at end of file