forked from keploy/samples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
43 lines (34 loc) · 1.43 KB
/
Copy pathapp.py
File metadata and controls
43 lines (34 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from flask import Flask, request, jsonify
from pymongo import MongoClient
from flask_cors import CORS
import collections.abc
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
# Connect to MongoDB
client = MongoClient('mongodb://mongo:27017/')
db = client['studentsdb']
students_collection = db['students']
@app.route('/students', methods=['GET'])
def get_students():
students = list(students_collection.find({}, {'_id': 0}))
return jsonify(students)
@app.route('/students/<student_id>', methods=['GET'])
def get_student(student_id):
student = students_collection.find_one({'student_id': student_id}, {'_id': 0})
return jsonify(student)
@app.route('/students', methods=['POST'])
def create_student():
new_student = request.json
students_collection.insert_one(new_student)
return jsonify({'message': 'Student created successfully'})
@app.route('/students/<student_id>', methods=['PUT'])
def update_student(student_id):
updated_student = request.json
students_collection.update_one({'student_id': student_id}, {'$set': updated_student})
return jsonify({'message': 'Student updated successfully'})
@app.route('/students/<student_id>', methods=['DELETE'])
def delete_student(student_id):
students_collection.delete_one({'student_id': student_id})
return jsonify({'message': 'Student deleted successfully'})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=6000, debug=True)