Update Operation

 Update operation | HTTP PUT Request Method

Here we will try to update the existing book with new book and if that book don't exist then we will provide a message book not found.

Code:
from fastapi import FastAPI, Body
from books3 import Book
from datavalidatorhelper import BookRequest

app = FastAPI()

BOOKS = [
    Book(id=1, title="DSA with Java", author="Puneet Kumar Singh",
description="A comprehensive guide to Data Structures and Algorithms using Java.",
rating=5),
    Book(id=2, title="Python Programming", author="Puneet Kumar Singh",
description="An introduction to Python programming for beginners.", rating=4),
    Book(id=3, title="Machine Learning Basics", author="Puneet Kumar Singh",
description="Understanding the fundamentals of Machine Learning.", rating=4),
    Book(id=4, title="Web Development with FastAPI", author="Ramesh Jha",
description="Building web applications using FastAPI.", rating=3),
    Book(id=5, title="Data Science with Python", author="Kalidas",
description="A guide to Data Science using Python.", rating=2),
    Book(id=6, title="Advanced Java Programming", author="Kalpana Gupta",
description="Deep dive into advanced concepts of Java programming.", rating=1)
]

@app.get("/books") # static path to get all books
async def read_all_books():
    """
    This function returns the list of all books.
    """
    return BOOKS

""" post request without any input validation
@app.post("/create_book")
async def create_book(book_request=Body()):
    BOOKS.append(book_request)
"""

# API endpoint to fetch book by using id
@app.get("/books/{book_id}")
async def read_book_by_id(book_id: int):
    for book in BOOKS:
        if book.id == book_id:
            return book
    return {"message": "book not found"}

# API endpoint to fetch books by rating
@app.get("/books/")
async def read_book_by_rating(rating: int):
    book_to_return = []
    for book in BOOKS:
        if book.rating == rating:
            book_to_return.append(book)
    return book_to_return

#post request with input validator
@app.post("/creat_book")
async def create_book(book_request: BookRequest):
    new_book = Book(**book_request.model_dump())
# new_book = Book(**book_request.dict()) -> if you are using
pydantic version 1 else the model_dump() for pydentic version 2.
    # BOOKS.append(new_book) -> it will store
the valid input dump but you can have duplicate ids.
    BOOKS.append(find_book_id(new_book))
# It will add latest id sequence number to id automatically and will overwrite the provided input so you don't need to bother about the id value sequence.

def find_book_id(book: Book):
    if len(BOOKS) > 0:
        book.id = BOOKS[-1].id + 1
    else:
        book.id = 1
    return book


# API endpoint to update existing book entry
@app.put("/books/update_book")
async def update_book_by_id(book_request: BookRequest):
    new_book=Book(**book_request.model_dump())
    for i in range(len(BOOKS)):
        if BOOKS[i].id == new_book.id:
            BOOKS[i]=new_book
            return {"message": "Book is updated", "detail": new_book}
    return {"message": "Book id not found.
Hence can't add new book to library.
Do use create book API endpoint to add new book into library."}

Result:


Comments

Popular posts from this blog

Post Request with Pydantic usage (input validator)

Code Scalability and Routing

CRUD Assignment