Activity: Writing your first prompts
- Add features, add features to a basic codebase using comments (e.g., add user input, simple calculations).
- Fix bugs, let's fix a bug in the codebase using Copilot's suggestions.
- Refactor, let's improve a codebase using Copilot's suggestions.
Add features
Let's take this REST API and add more features to it. The API manages your product catalog:
from flask import Flask, jsonify, request
products = [
{'id': 1, 'name': 'Product 1', 'price': 10.0},
{'id': 2, 'name': 'Product 2', 'price': 20.0},
{'id': 3, 'name': 'Product 3', 'price': 30.0},
{'id': 4, 'name': 'Product 4', 'price': 40.0},
{'id': 5, 'name': 'Product 5', 'price': 50.0},
{'id': 6, 'name': 'Product 6', 'price': 60.0},
{'id': 7, 'name': 'Product 7', 'price': 70.0},
{'id': 8, 'name': 'Product 8', 'price': 80.0},
{'id': 9, 'name': 'Product 9', 'price': 90.0},
{'id': 10, 'name': 'Product 10', 'price': 100.0}
]
## add routes
@app.route('/products', methods=['GET'])
def get_products():
return jsonify(products)
@app.route('/products/<int:product_id>', methods=['GET'])
def get_product(product_id):
product = next((p for p in products if p['id'] == product_id), None)
return jsonify(product) if product else ('', 404)
## create a Flask app
app = Flask(__name__)
Task: Write a prompt to support pagination, client should be able to specify the page and the number of items per page. The API should return a paginated list of products.
- Try typing it as a comment directly in a code file. For example:
# Add pagination to the /products endpoint. The client should be able to specify the page and the number of items per page. The API should return a paginated list of products.
- Try the same comment in the chat interface.
Refactor
Refactor the code to something you want to maintain, i.e a class-based approach where responsibilities are separated. For example, you can create a Product
class and a ProductService
class to handle the business logic. You can also create a ProductController
class to handle the API endpoints.
- Use the chat interface for this.
Suggested prompts:
- Refactor the code to use a class-based approach and separating concerns.
Fix a bug
# write a function below that takes a cart of products and applies a discount of 3 for 2, i.e buy 3 and pay for 2, choose the cheapest one to be free. However, there should be a bug that takes away the last product from the cart total.
def apply_discount(cart):
cart.sort(key=lambda x: x['price'])
total = 0
for i in range(len(cart)):
if i % 3 == 2:
continue
total += cart[i]['price']
return total
Here's the test file:
import unittest
from app import apply_discount
class TestApplyDiscount(unittest.TestCase):
def test_apply_discount(self):
cart = [
{'id': 1, 'name': 'Product 1', 'price': 10.0},
{'id': 2, 'name': 'Product 2', 'price': 20.0},
{'id': 3, 'name': 'Product 3', 'price': 30.0},
{'id': 4, 'name': 'Product 4', 'price': 40.0},
{'id': 5, 'name': 'Product 5', 'price': 50.0}
]
self.assertEqual(apply_discount(cart), 140.0) # i.e the cheapest one, 10.0 should be free, total without discount is 150.0
Suggested prompts:
- What does this code do?
- Change it to "behavior"
Optional: Improve security
The idea here is to make the API more secure. Ask Copilot to consider how to make the code more secure.
Suggested prompts:
- How would you make this code more secure?
- Add authentication to the API.
- Add authorization to the API.