Update code in endpoints/bookies.post.py
This commit is contained in:
parent
402993b336
commit
64abc19b62
28
endpoints/bookies.post.py
Normal file
28
endpoints/bookies.post.py
Normal file
@ -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.
|
Loading…
x
Reference in New Issue
Block a user