File size: 1,862 Bytes
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
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 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 getMindmapByid(userId, id, savedMindmap):
    try:
        object_id = ObjectId(id)
        result = savedMindmap.find_one({"userId": userId, "_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