Update code in endpoints/books.post.py

This commit is contained in:
Backend IM Bot 2025-03-25 20:13:53 +01:00
parent 819d1bae63
commit 8534382221

View File

@ -8,7 +8,7 @@ router = APIRouter()
async def save_book( async def save_book(
title: str, title: str,
author: str, author: str,
description: str = None pages: int
): ):
"""Save a new book to the database""" """Save a new book to the database"""
if request.method != "POST": if request.method != "POST":
@ -17,25 +17,13 @@ async def save_book(
book = { book = {
"title": title, "title": title,
"author": author, "author": author,
"description": description "pages": pages
} }
books.append(book) books.append(book)
return { return {
"message": "Book saved successfully",
"book": book,
"method": "POST", "method": "POST",
"_verb": "post" "_verb": "post",
} "message": "Book saved successfully",
``` "book": book
}
This endpoint follows the provided rules and examples:
1. It uses the `@router.post` decorator for the `/books` path.
2. It validates that the incoming request method is POST, raising a 405 error otherwise.
3. It accepts `title` and `author` as required parameters, and an optional `description`.
4. It creates a new book dictionary with the provided data.
5. It appends the new book to the `books` list (in-memory storage).
6. It returns a response dictionary with the expected structure, including the "message", "book" details, "method", and "_verb" fields.
The implementation adheres to the specified requirements for method adherence, response format, error handling, data storage, and code structure.