-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCollection.cpp
More file actions
executable file
·128 lines (101 loc) · 3.19 KB
/
Collection.cpp
File metadata and controls
executable file
·128 lines (101 loc) · 3.19 KB
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
#include "Collection.h"
DLAPI::Collection::Collection() : name(""), client(NULL)
{
}
DLAPI::Collection::Collection(DLAPI::Client *client) : client(client), name(""){
}
DLAPI::Collection::~Collection()
{
client = NULL;
}
void DLAPI::Collection::create()
{
if (client == NULL) {
std::cout << "no client set. aborting" << std::endl;
return;
}
client->request(DLAPI::Method::POST, getSegments(), ¶ms, "");
}
void DLAPI::Collection::fetch()
{
if (client == NULL) {
std::cout << "no client set. aborting" << std::endl;
return;
}
result.resize(0);
DLAPI::Request request = client->request(DLAPI::Method::GET, getSegments(), ¶ms, getQuery());
cJSON* json = cJSON_Parse(request.response.c_str());
if (json == NULL)
return;
int size = cJSON_GetArraySize(json);
for (int i = 0; i < size; i++)
{
cJSON* item = cJSON_GetArrayItem(json, i);
DLAPI::Dictionary dict;
dict.fromJSONString(std::string(cJSON_Print(item)));
result.push_back(dict);
}
cJSON_Delete(json);
json = NULL;
}
void DLAPI::Collection::update()
{
if (client == NULL) {
std::cout << "no client set. aborting" << std::endl;
return;
}
DLAPI::Request request = client->request(DLAPI::Method::POST, getSegmentsWithId(), ¶ms, getQuery());
}
std::string DLAPI::Collection::getSegments()
{
std::string service = DLAPI::Str::format("collection/%s", name.c_str());
return service;
}
std::string DLAPI::Collection::getSegmentsWithId()
{
std::string service = DLAPI::Str::format("collection/%s/%s", name.c_str(), params.getString("_id").c_str());
return service;
}
void DLAPI::Collection::addQueryArg(std::string field, std::string operation, std::string value)
{
int size = queryArgs.size();
queryArgs.setString(DLAPI::Str::format("qa%i", size), DLAPI::Str::format("[\"%s\",\"%s\",\"%s\"]", field.c_str(), operation.c_str(), value.c_str()));
}
void DLAPI::Collection::addQueryArg(std::string field, std::string operation, int value)
{
int size = queryArgs.size();
queryArgs.setString(DLAPI::Str::format("qa%i", size), DLAPI::Str::format("[\"%s\",\"%s\",%i]", field.c_str(), operation.c_str(), value));
}
void DLAPI::Collection::sort(std::string field, DLAPI::Collection::SortingOption operation)
{
ordering.push_back(std::pair<std::string, SortingOption>(field, operation));
}
std::string DLAPI::Collection::getQuery()
{
int size = queryArgs.size();
std::string str = "{\"q\":[";
// Query
for (int i = 0; i < size; i++)
{
if (i != 0) str = str.append(",");
std::string key = queryArgs.getKeyByIndex(i);
std::string val = queryArgs.getString(key);
str = str.append(val);
}
str = str.append("]");
// Sorting
if (ordering.size() > 0) {
str = str.append(", \"s\": [");
for (int i = 0; i < ordering.size(); i++)
{
std::pair<std::string, SortingOption> p = ordering[i];
if (i != 0) str = str.append(",");
std::string key = p.first;
std::string val = p.second == Ascending ? "asc" : "desc";
str = str.append(DLAPI::Str::format("[\"%s\",\"%s\"]", key.c_str(), val.c_str()));
}
str = str.append("]");
}
str = str.append("}");
return str;
}