Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG FIXED #615

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions CRUD Assignment/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def write_csv(data):
except Exception as e:
print("Error writing to CSV:", e)


@app.route('/')
def index():
return render_template('index.html')
Expand All @@ -37,7 +36,7 @@ def get_books():
def add_book():
data = read_csv()
new_book = {
'id': len(data) + 1,
'id': str(len(data) + 1), # UPDATED: Convert id to string for consistency
'title': request.json['title'],
'author': request.json['author'],
'genre': request.json['genre']
Expand All @@ -46,14 +45,12 @@ def add_book():
write_csv(data)
return jsonify(new_book)



@app.route('/books/<int:id>', methods=['POST'])
@app.route('/books/<int:id>', methods=['PUT']) # UPDATED: Changed from POST to PUT for proper RESTful design
def update_book(id):
data = read_csv()
updated_book = None
for book in data:
if book['id'] == id:
if int(book['id']) == id: # UPDATED: Convert book['id'] to int for comparison
book['title'] = request.json['title']
book['author'] = request.json['author']
book['genre'] = request.json['genre']
Expand All @@ -68,7 +65,7 @@ def update_book(id):
@app.route('/books/<int:id>', methods=['DELETE'])
def delete_book(id):
data = read_csv()
data = [book for book in data if book['id'] != id]
data = [book for book in data if int(book['id']) != id] # UPDATED: Convert book['id'] to int for comparison
write_csv(data)
return jsonify({"message": "Book deleted successfully"})

Expand Down
4 changes: 2 additions & 2 deletions CRUD Assignment/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ <h2>Books List</h2>
const genre = prompt("Enter new genre:");
if (title && author && genre) {
fetch(`/books/${id}`, {
method: 'PUT',
method: 'PUT', // UPDATED: Changed from POST to PUT to match the backend
headers: {
'Content-Type': 'application/json'
},
Expand All @@ -133,4 +133,4 @@ <h2>Books List</h2>
fetchBooks();
</script>
</body>
</html>
</html>
4 changes: 4 additions & 0 deletions sample.txt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<<<<<<< HEAD
Hello World!!
=======
Hello World
>>>>>>> 2f1d9c7525c240c87b4350244a8bd5ff49e3ed37