beans-aqdspd/endpoints/bookies.post.py
2025-03-25 00:17:01 +01:00

28 lines
884 B
Python

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.