ParthCodes commited on
Commit
ed2fa80
·
verified ·
1 Parent(s): 3867016

Update controllers/mindmap.py

Browse files
Files changed (1) hide show
  1. controllers/mindmap.py +35 -2
controllers/mindmap.py CHANGED
@@ -14,6 +14,39 @@ def saveMindmap(data, userId, savedMindmap):
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})
@@ -28,10 +61,10 @@ def getMindmap(userId, savedMindmap):
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:
 
14
  print(e)
15
  return jsonify({"error": "An error occurred"}), 500
16
 
17
+ def saveMindmapById(data, userId, savedMindmap):
18
+ try:
19
+ map_id = data["_id"]
20
+ existing_map = savedMindmap.find_one({"_id": ObjectId(map_id)})
21
+
22
+ if existing_map:
23
+ existing_user_id = existing_map.get("userId")
24
+
25
+ if existing_user_id != userId:
26
+ # If the existing map belongs to a different user, create a new map for the current user
27
+ savedMindmap.insert_one({
28
+ "userId": userId,
29
+ "data": data["data"]
30
+ })
31
+ return jsonify({"message": "Mindmap Saved to your space!"}), 201
32
+ else:
33
+ # If the existing map belongs to the current user, update it with the new data
34
+ savedMindmap.update_one({"_id": ObjectId(map_id)}, {"$set": {"data": data["data"]}})
35
+ return jsonify({"message": "Mindmap Updated!"}), 200
36
+ else:
37
+ # If the map does not exist, create a new one with the provided _id for the current user
38
+ savedMindmap.insert_one({
39
+ "_id": ObjectId(map_id),
40
+ "userId": userId,
41
+ "data": data["data"]
42
+ })
43
+ return jsonify({"message": "Minmdap Saved!"}), 201
44
+ except Exception as e:
45
+ print(e)
46
+ return jsonify({"error": "An error occurred"}), 500
47
+
48
+
49
+
50
  def getMindmap(userId, savedMindmap):
51
  try:
52
  results = savedMindmap.find({"userId": userId})
 
61
  print(e)
62
  return jsonify({"error": "An error occurred"}), 500
63
 
64
+ def getMindmapByid(id, savedMindmap):
65
  try:
66
  object_id = ObjectId(id)
67
+ result = savedMindmap.find_one({"_id": object_id})
68
  if result:
69
  return json_util.dumps({"data": result}), 200
70
  else: