29 lines
599 B
Python
29 lines
599 B
Python
from fastapi import APIRouter, HTTPException
|
|
|
|
books = [] # In-memory storage
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/books")
|
|
async def save_book(
|
|
title: str,
|
|
author: str,
|
|
pages: int
|
|
):
|
|
"""Save a new book to the database"""
|
|
if request.method != "POST":
|
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
|
|
|
book = {
|
|
"title": title,
|
|
"author": author,
|
|
"pages": pages
|
|
}
|
|
books.append(book)
|
|
|
|
return {
|
|
"method": "POST",
|
|
"_verb": "post",
|
|
"message": "Book saved successfully",
|
|
"book": book
|
|
} |