📖 Scenario: You are building a simple REST API for a bookstore. The store wants to update the prices of multiple books at once to run a sale.Batch updates let you change many items in one request, saving time and effort.
🎯 Goal: Create a batch update endpoint that accepts a list of book IDs and new prices, then updates the prices in the data store.
📋 What You'll Learn
Create a data structure to hold books with their IDs and prices
Add a configuration variable for the discount percentage
Write the batch update logic to apply the discount to selected books
Print the updated books after the batch update
💡 Why This Matters
🌍 Real World
Batch updates are common in online stores to quickly change prices or stock for many products at once.
💼 Career
Understanding batch update patterns helps backend developers build efficient APIs that handle multiple changes in one request.
Progress0 / 4 steps
1
DATA SETUP: Create the initial books data
Create a dictionary called books with these exact entries: 1: {'title': 'Book A', 'price': 100}, 2: {'title': 'Book B', 'price': 150}, 3: {'title': 'Book C', 'price': 200}
Rest API
Hint
Use a dictionary with keys as book IDs and values as dictionaries holding title and price.
2
CONFIGURATION: Set the discount percentage
Create a variable called discount_percent and set it to 10 to represent a 10% discount
Rest API
Hint
Just create a variable with the exact name and value.
3
CORE LOGIC: Apply batch update to selected books
Write a function called batch_update_prices that takes book_ids (a list) and updates each book's price in books by reducing it by discount_percent. Use a for loop with book_id to iterate over book_ids and update the price inside the function.
Rest API
Hint
Use a function with a for loop to update prices by applying the discount.
4
OUTPUT: Run batch update and print updated books
Call batch_update_prices with the list [1, 3] and then print the books dictionary to show updated prices
Rest API
Hint
Call the function with the list [1, 3] and print the books dictionary.
Practice
(1/5)
1. What is the main advantage of using batch update patterns in REST APIs?
easy
A. They only work with GET requests.
B. They allow updating multiple items with a single request, saving time.
C. They require sending separate requests for each item.
D. They increase the number of requests to the server.
Solution
Step 1: Understand batch update purpose
Batch update patterns let you update many items at once instead of one by one.
Step 2: Identify the benefit
This reduces the number of requests and saves time and server resources.
Final Answer:
They allow updating multiple items with a single request, saving time. -> Option B
Quick Check:
Batch update = single request for many items [OK]
Hint: Batch updates reduce requests by combining many updates [OK]
Common Mistakes:
Thinking batch updates increase requests
Confusing batch update with single item update
Assuming batch updates use GET method
2. Which HTTP method is most appropriate for batch updates in REST APIs?
easy
A. GET
B. POST
C. PATCH
D. DELETE
Solution
Step 1: Recall HTTP methods for updates
PATCH is used to partially update resources, suitable for batch updates.
Step 2: Eliminate other methods
GET retrieves data, POST creates new data, DELETE removes data, so they are not best for batch updates.
Final Answer:
PATCH -> Option C
Quick Check:
Batch update uses PATCH method [OK]
Hint: Use PATCH for partial or batch updates [OK]
Common Mistakes:
Using GET for updates
Confusing POST with update method
Using DELETE instead of PATCH
3. Given this JSON payload sent in a PATCH request for batch update: