Spaces:
Sleeping
Sleeping
File size: 3,746 Bytes
d5c07b5 ed2fa80 d5c07b5 018b21a d5c07b5 ed2fa80 d5c07b5 ed2fa80 d5c07b5 |
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 |
from flask import jsonify
from bson import json_util, ObjectId
def saveMindmap(data, userId, savedMindmap):
try:
result = savedMindmap.insert_one({
"userId": userId,
"data": data,
})
print(result.inserted_id)
return jsonify({"result": str(result.inserted_id)}), 201
except Exception as e:
print(e)
return jsonify({"error": "An error occurred"}), 500
def saveMindmapById(data, userId, savedMindmap):
try:
map_id = data["_id"]
existing_map = savedMindmap.find_one({"_id": ObjectId(map_id)})
if existing_map:
existing_user_id = existing_map.get("userId")
if existing_user_id != userId:
# If the existing map belongs to a different user, create a new map for the current user
savedMindmap.insert_one({
"userId": userId,
"data": data["data"]
})
return jsonify({"message": "Mindmap Saved to your space!"}), 201
else:
# If the existing map belongs to the current user, update it with the new data
savedMindmap.update_one({"_id": ObjectId(map_id)}, {"$set": {"data": data["data"]}})
return jsonify({"message": "Mindmap Updated!"}), 200
else:
# If the map does not exist, create a new one with the provided _id for the current user
savedMindmap.insert_one({
"_id": ObjectId(map_id),
"userId": userId,
"data": data["data"]
})
return jsonify({"message": "Minmdap Saved!"}), 201
except Exception as e:
print(e)
return jsonify({"error": "An error occurred"}), 500
def getMindmap(userId, savedMindmap):
try:
results = savedMindmap.find({"userId": userId})
mindmaps = [{"_id": str(result["_id"]), "data": result["data"]} for result in results]
if mindmaps:
# Convert ObjectId to string for JSON serialization
return json_util.dumps({"data": mindmaps}), 200
else:
return jsonify({"msg": "No Mindmap stored"}), 404
except Exception as e:
print(e)
return jsonify({"error": "An error occurred"}), 500
def getMindmapHistory(userId, savedMindmap):
try:
results = savedMindmap.find({"userId": userId}).limit(3)
mindmaps = [{"_id": str(result["_id"]), "data": result["data"]} for result in results]
if mindmaps:
# Convert ObjectId to string for JSON serialization
return json_util.dumps({"data": mindmaps}), 200
else:
return jsonify({"msg": "No Mindmap stored"}), 404
except Exception as e:
print(e)
return jsonify({"error": "An error occurred"}), 500
def getMindmapByid(id, savedMindmap):
try:
object_id = ObjectId(id)
result = savedMindmap.find_one({"_id": object_id})
if result:
return json_util.dumps({"data": result}), 200
else:
return jsonify({"msg": "Mindmap not found"}), 404
except Exception as e:
print(e)
return jsonify({"error": "An error occurred"}), 500
def deleteMindmap(userId, data, savedMindmap):
try:
object_id = ObjectId(data["_id"])
result = savedMindmap.delete_one({"userId": userId, "_id": object_id})
if result.deleted_count == 1:
return jsonify({"result": True}), 200
else:
return jsonify({"result": False, "message": "Mindmap not found"}), 404
except Exception as e:
print(e)
return jsonify({"message": "Something went wrong"}), 500 |