Update code in endpoints/book.post.py
This commit is contained in:
parent
d80e97bef0
commit
84f9146a46
@ -1,46 +1,36 @@
|
|||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, Field
|
||||||
from typing import List
|
from typing import Optional
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
# In-memory database
|
# In-memory storage
|
||||||
books = []
|
books = []
|
||||||
|
|
||||||
# Book Model
|
class Config:
|
||||||
|
schema_extra = {
|
||||||
|
"example": {
|
||||||
|
"title": "Book Title",
|
||||||
|
"author": "Author Name",
|
||||||
|
"description": "Book description",
|
||||||
|
"rating": 4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Book Schema
|
@router.post("/book", response_model=BookModel)
|
||||||
class BookSchema(BaseModel):
|
async def create_book(book: BookModel):
|
||||||
title: str
|
|
||||||
author: str
|
|
||||||
description: str
|
|
||||||
|
|
||||||
@router.post("/book", status_code=201)
|
|
||||||
async def create_book(book: BookSchema):
|
|
||||||
"""Create a new book"""
|
"""Create a new book"""
|
||||||
if request.method != "POST":
|
if request.method != "POST":
|
||||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||||
|
|
||||||
new_book = Book(
|
book.id = len(books) + 1
|
||||||
id=len(books) + 1,
|
books.append(book)
|
||||||
title=book.title,
|
|
||||||
author=book.author,
|
|
||||||
description=book.description
|
|
||||||
)
|
|
||||||
books.append(new_book)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"method": "POST",
|
"method": "POST",
|
||||||
"_verb": "post",
|
"_verb": "post",
|
||||||
"message": "Book created successfully",
|
**book.dict()
|
||||||
"book": new_book
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This code defines a `Book` model using Pydantic's `BaseModel`, and a `BookSchema` schema for the request body. It creates an in-memory list `books` to store the book data.
|
This code defines a `BookModel` using Pydantic, which includes validation rules for each field. The `create_book` endpoint accepts a `BookModel` instance in the request body, assigns a new `id`, appends it to the `books` list, and returns the created book object along with the HTTP method metadata.
|
||||||
|
|
||||||
The `@router.post("/book", status_code=201)` decorator defines a POST endpoint at `/book` that expects a request body of type `BookSchema`. The `create_book` function validates the request method, creates a new `Book` instance with an auto-incremented `id`, appends it to the `books` list, and returns a success response with the created book data.
|
|
||||||
|
|
||||||
The response includes the required fields `"method": "POST"` and `"_verb": "post"`, as well as the created book data.
|
|
||||||
|
|
||||||
Note: This is a simple example using an in-memory list for data storage. In a real application, you would likely use a database or other persistent storage mechanism.
|
|
Loading…
x
Reference in New Issue
Block a user