Spaces:
Sleeping
Sleeping
Upload mindmap.py
Browse files- controllers/mindmap.py +53 -0
controllers/mindmap.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import jsonify
|
| 2 |
+
from bson import json_util, ObjectId
|
| 3 |
+
|
| 4 |
+
def saveMindmap(data, userId, savedMindmap):
|
| 5 |
+
try:
|
| 6 |
+
result = savedMindmap.insert_one({
|
| 7 |
+
"userId": userId,
|
| 8 |
+
"data": data,
|
| 9 |
+
})
|
| 10 |
+
|
| 11 |
+
print(result.inserted_id)
|
| 12 |
+
return jsonify({"result": str(result.inserted_id)}), 201
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(e)
|
| 15 |
+
return jsonify({"error": "An error occurred"}), 500
|
| 16 |
+
|
| 17 |
+
def getMindmap(userId, savedMindmap):
|
| 18 |
+
try:
|
| 19 |
+
results = savedMindmap.find({"userId": userId})
|
| 20 |
+
mindmaps = [{"_id": str(result["_id"]), "data": result["data"]} for result in results]
|
| 21 |
+
|
| 22 |
+
if mindmaps:
|
| 23 |
+
# Convert ObjectId to string for JSON serialization
|
| 24 |
+
return json_util.dumps({"data": mindmaps}), 200
|
| 25 |
+
else:
|
| 26 |
+
return jsonify({"msg": "No Mindmap stored"}), 404
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(e)
|
| 29 |
+
return jsonify({"error": "An error occurred"}), 500
|
| 30 |
+
|
| 31 |
+
def getMindmapByid(userId, id, savedMindmap):
|
| 32 |
+
try:
|
| 33 |
+
object_id = ObjectId(id)
|
| 34 |
+
result = savedMindmap.find_one({"userId": userId, "_id": object_id})
|
| 35 |
+
if result:
|
| 36 |
+
return json_util.dumps({"data": result}), 200
|
| 37 |
+
else:
|
| 38 |
+
return jsonify({"msg": "Mindmap not found"}), 404
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(e)
|
| 41 |
+
return jsonify({"error": "An error occurred"}), 500
|
| 42 |
+
|
| 43 |
+
def deleteMindmap(userId, data, savedMindmap):
|
| 44 |
+
try:
|
| 45 |
+
object_id = ObjectId(data["_id"])
|
| 46 |
+
result = savedMindmap.delete_one({"userId": userId, "_id": object_id})
|
| 47 |
+
if result.deleted_count == 1:
|
| 48 |
+
return jsonify({"result": True}), 200
|
| 49 |
+
else:
|
| 50 |
+
return jsonify({"result": False, "message": "Mindmap not found"}), 404
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(e)
|
| 53 |
+
return jsonify({"message": "Something went wrong"}), 500
|