aboutsummaryrefslogtreecommitdiff
path: root/db/uobject.cpp
blob: 25b13e699cef968aa7a483770d5152263a93bc73 (plain)
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
#include "uobject.h"

using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;

std::string uobject::retrieveAll(std::string collection, struct json_object* jsonObjects) {

    auto conn = mongo.get_connection();

    auto coll = (*conn)[config["mongo_db"]][collection];

    auto cursor = coll.find({});

    for (auto&& doc : cursor) {
        json_object_array_add(jsonObjects, json_tokener_parse(bsoncxx::to_json(doc).c_str()));
    }

    return json_object_to_json_string_ext(jsonObjects, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
}

std::string uobject::add(std::string collection, struct json_object* jsonObject, const char * body) {
    auto conn = mongo.get_connection();

    auto coll = (*conn)[config["mongo_db"]][collection];

    auto builder = bsoncxx::builder::stream::document{};

    bsoncxx::document::value document = builder
        << "datas" << bsoncxx::from_json(body)
        << bsoncxx::builder::stream::finalize;

    auto result = coll.insert_one(document.view());

    std::string oid = result->inserted_id().get_oid().value.to_string();


    int stringlen = 0;
    stringlen = strlen( body );
    struct json_tokener *tok = json_tokener_new();
    enum json_tokener_error jerr;

    json_object *json_datas = NULL;

    do {
        json_datas = json_tokener_parse_ex(tok, body, stringlen);
    } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);

    if (jerr != json_tokener_success)
    {
        fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
    }

    if (json_object_get_type(json_datas) == json_type_object) {
        /* Create an object with the following template:
            * {
            *  "_id": {
            *      "$oid": "5d484d371ec4865f767d8424"
            *  },
            *  "datas": {
            *      [...]
            *  }
            * }
        */

        auto json_id = json_object_new_object();
        auto json_oid = json_object_new_string(oid.c_str());

        json_object_object_add(json_id, "$oid", json_oid);
        json_object_object_add(jsonObject, "_id", json_id);
        json_object_object_add(jsonObject, "datas", json_datas);
    } else {
        std::cout << typeid(json_datas).name() << std::endl;
        std::cout << "json_datas type: " << json_type_to_name(json_object_get_type(json_datas)) << std::endl;
    }
    json_tokener_free(tok);

    return json_object_to_json_string_ext(jsonObject, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
}

std::string uobject::remove(std::string collection, std::string oid, struct json_object* jsonObject) {
    auto conn = mongo.get_connection();

    auto coll = (*conn)[config["mongo_db"]][collection];

    auto result = coll.delete_one(make_document(kvp("_id", bsoncxx::oid(oid))));


    if (result->deleted_count() > 0) {
        /* Create an object with the following template:
            * {
            *  "_id": {
            *      "$oid": "5d484d371ec4865f767d8424"
            *  },
            *  "datas": {
            *      [...]
            *  }
            * }
        */

        auto json_id = json_object_new_object();
        auto json_oid = json_object_new_string(oid.c_str());

        json_object_object_add(json_id, "$oid", json_oid);
        json_object_object_add(jsonObject, "_id", json_id);
        json_object_object_add(jsonObject, "datas", {});
        return json_object_to_json_string_ext(jsonObject, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
    } else {
        return "0";
    }
}

std::string uobject::searchKeyValue(std::string collection, std::string key, std::string value, struct json_object* jsonArray) {
    auto conn = mongo.get_connection();

    auto coll = (*conn)[config["mongo_db"]][collection];

    // Generate a string with the folowing format:
    // {"datas.key":"value"}
    std::string json_string = "{\"datas.";
    json_string += key;
    json_string += "\":\"";
    json_string += value;
    json_string += "\"}";

    auto cursor = coll.find(bsoncxx::from_json(json_string));

    for (auto&& doc : cursor) {
        json_object_array_add(jsonArray, json_tokener_parse(bsoncxx::to_json(doc).c_str()));
    }

    return json_object_to_json_string_ext(jsonArray, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
}