-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestAPI.py
151 lines (134 loc) · 5.33 KB
/
RestAPI.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from flask import Flask, request, jsonify
from flask_pymongo import PyMongo
app = Flask(__name__)
# Configure MongoDB
app.config['MONGO_URI'] = 'mongodb://localhost:27017/institute_db'
mongo = PyMongo(app)
# Department collection in MongoDB
department_collection = mongo.db.department
# Faculty collection in MongoDB
faculty_collection = mongo.db.faculty
# Route for adding a new department
@app.route('/department/add', methods=['POST'])
def add_department():
try:
department_data = request.json
department_collection.insert_one(department_data)
return jsonify({'message': 'Department added successfully'}), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for updating an existing department
@app.route('/department/<department_id>', methods=['PUT'])
def update_department(department_id):
try:
department_data = request.json
result = department_collection.update_one({"department_id": department_id}, {"$set": department_data})
if result.matched_count > 0:
return jsonify({'message': 'Department updated successfully'}), 200
else:
return jsonify({'message': 'Department not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for deleting an existing department
@app.route('/department/<department_id>', methods=['DELETE'])
def delete_department(department_id):
try:
result = department_collection.delete_one({"department_id": department_id})
if result.deleted_count > 0:
return jsonify({'message': 'Department deleted successfully'}), 200
else:
return jsonify({'message': 'Department not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for retrieving all departments
@app.route('/department', methods=['GET'])
def get_all_departments():
try:
department_data = list(department_collection.find({}, {'_id': 0}))
return jsonify(department_data)
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for adding a new faculty
@app.route('/faculty', methods=['POST'])
def add_faculty():
try:
faculty_data = request.json
faculty_collection.insert_one(faculty_data)
return jsonify({'message': 'Faculty added successfully'}), 201
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for updating an existing faculty
@app.route('/faculty/<faculty_id>', methods=['PUT'])
def update_faculty(faculty_id):
try:
faculty_data = request.json
result = faculty_collection.update_one({"faculty_id": faculty_id}, {"$set": faculty_data})
if result.matched_count > 0:
return jsonify({'message': 'Faculty updated successfully'}), 200
else:
return jsonify({'message': 'Faculty not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for deleting an existing faculty
@app.route('/faculty/<faculty_id>', methods=['DELETE'])
def delete_faculty(faculty_id):
try:
result = faculty_collection.delete_one({"faculty_id": faculty_id})
if result.deleted_count > 0:
return jsonify({'message': 'Faculty deleted successfully'}), 200
else:
return jsonify({'message': 'Faculty not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for retrieving all faculty
@app.route('/faculty', methods=['GET'])
def get_all_faculty():
try:
faculty_data = list(faculty_collection.find({}, {'_id': 0}))
return jsonify(faculty_data)
except Exception as e:
return jsonify({'error': str(e)}), 500
# All Campus Routes
# Route for adding a new campus
@app.route('/campus', methods=['POST'])
def add_campus():
try:
campus_data = request.json
mongo.db.campus.insert_one(campus_data)
return jsonify({'message': 'Campus added successfully'}), 201
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for updating an existing campus
@app.route('/campus/<campus_id>', methods=['PUT'])
def update_campus(campus_id):
try:
campus_data = request.json
result = mongo.db.campus.update_one({"campus_id": campus_id}, {"$set": campus_data})
if result.matched_count > 0:
return jsonify({'message': 'Campus updated successfully'}), 200
else:
return jsonify({'message': 'Campus not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for deleting an existing campus
@app.route('/campus/<campus_id>', methods=['DELETE'])
def delete_campus(campus_id):
try:
result = mongo.db.campus.delete_one({"campus_id": campus_id})
if result.deleted_count > 0:
return jsonify({'message': 'Campus deleted successfully'}), 200
else:
return jsonify({'message': 'Campus not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
# Route for retrieving all campuses
@app.route('/campus', methods=['GET'])
def get_all_campuses():
try:
campus_data = list(mongo.db.campus.find({}, {'_id': 0}))
return jsonify(campus_data)
except Exception as e:
return jsonify({'error': str(e)}), 500
# Run the Flask app
if __name__ == '__main__':
app.run(debug=True)