sh20raj commited on
Commit
007f0f1
·
verified ·
1 Parent(s): 2cfce93

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify, request
2
+
3
+ app = Flask(__name__)
4
+
5
+ # Sample data - you can replace this with your actual data source
6
+ books = [
7
+ {"id": 1, "title": "Python Programming", "author": "Guido van Rossum"},
8
+ {"id": 2, "title": "Flask Basics", "author": "Miguel Grinberg"},
9
+ {"id": 3, "title": "Data Science Handbook", "author": "Jake VanderPlas"}
10
+ ]
11
+
12
+ # Route to get all books
13
+
14
+
15
+ @app.route('/', methods=['GET'])
16
+ def get_books_list():
17
+ return jsonify(books)
18
+
19
+
20
+ @app.route('/books', methods=['GET'])
21
+ def get_books():
22
+ return jsonify(books)
23
+
24
+ # Route to get a specific book by ID
25
+
26
+
27
+ @app.route('/books/<int:book_id>', methods=['GET'])
28
+ def get_book(book_id):
29
+ book = next((book for book in books if book['id'] == book_id), None)
30
+ if book:
31
+ return jsonify(book)
32
+ else:
33
+ return jsonify({'message': 'Book not found'}), 404
34
+
35
+ # Route to create a new book
36
+
37
+
38
+ @app.route('/books', methods=['POST'])
39
+ def create_book():
40
+ new_book = request.get_json()
41
+ books.append(new_book)
42
+ return jsonify({'message': 'Book created successfully'}), 201
43
+
44
+ # Route to update a book
45
+
46
+
47
+ @app.route('/books/<int:book_id>', methods=['PUT'])
48
+ def update_book(book_id):
49
+ book = next((book for book in books if book['id'] == book_id), None)
50
+ if not book:
51
+ return jsonify({'message': 'Book not found'}), 404
52
+ else:
53
+ data = request.get_json()
54
+ book.update(data)
55
+ return jsonify({'message': 'Book updated successfully'})
56
+
57
+ # Route to delete a book
58
+
59
+
60
+ @app.route('/books/<int:book_id>', methods=['DELETE'])
61
+ def delete_book(book_id):
62
+ global books
63
+ books = [book for book in books if book['id'] != book_id]
64
+ return jsonify({'message': 'Book deleted successfully'})
65
+
66
+
67
+ if __name__ == '__main__':
68
+ app.run(debug=True)