Open
Conversation
Comment on lines
22
to
33
| items = cursor.fetchall() | ||
| return {"data": items} | ||
|
|
||
| @app.get("/items/{item_id}") | ||
| async def get_item(item_id: int): | ||
| cursor.execute("SELECT * FROM items WHERE id = %s", (item_id,)) | ||
| item = cursor.fetchone() | ||
| return {"data": item} | ||
|
|
||
| @app.post("/items") | ||
| async def add_item(name: str): | ||
| cursor.execute("INSERT INTO items (name) VALUES (%s)", (name,)) |
There was a problem hiding this comment.
The code changes you've made are generally okay, but there are still some improvements that can be done.
-
Usage of SQL statements directly in routes: This can be risky as it might lead to SQL Injection attacks if not properly handled. Also, it's a best practice to separate database operations from your routes for modularity and maintainability.
-
Error handling: There is currently no error handling for database queries within the routes. What happens if the item doesn't exist in the database or the database connection fails?
-
SQL execute parameters: Use a tuple with a trailing comma for single parameters as it is safer against SQL-injection.
Here are the suggestions to improve your code:
@app.get("/items/{item_id}")
async def get_item(item_id: int):
try:
cursor.execute("SELECT * FROM items WHERE id = %s", (item_id,))
item = cursor.fetchone()
if item is None:
return {"error": "Item not found"}
return {"data": item}
except Exception as e:
# Ideally log the error and return a user-friendly message
return {"error": "An error occurred"}
@app.post("/items")
async def add_item(name: str):
try:
cursor.execute("INSERT INTO items (name) VALUES (%s)", (name,))
except Exception as e:
# Similarly, log errors and return a user-friendly message
return {"error": "An error occurred"}- Finally, be aware that this simplified adjustment assumes you have the cursor object initiated and connected to a database, which is not shown in the provided snippet. If not, further changes would be needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We'd like the ability to get a singular item by id from the API instead of returning a full list.