-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocument.cpp
94 lines (80 loc) · 2.39 KB
/
Document.cpp
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
#include <algorithm>
#include "Document.hpp"
using namespace CCC;
Document::Document(const std::string &uuid, const std::string &db, const std::string &revision, std::shared_ptr<DataBaseConnection> connection) {
this->data.uuid = uuid;
this->data.db = db;
this->data.revision = revision;
this->data.connection = connection;
}
Document::Document(const Arguments &arguments) {
this->data = arguments;
}
std::string Document::uuid() {
return this->data.uuid;
}
json Document::json_content() {
const auto revision = this->data.revision;
return this->get(GetType::Content, revision);
}
std::string Document::content() {
return this->json_content().as_string();
}
void Document::setContent(json json) {
const auto name = this->data.db;
const auto revision = this->revisions().back();
const auto uuid = this->data.uuid;
this->data.connection.get()->updateDocument(name, uuid, revision, json);
}
std::vector<std::string> Document::revisions() {
std::vector<std::string> result;
const auto temp = this->get(GetType::Revisions);
const auto current = temp["_rev"s];
result.push_back(current.as_string());
const auto revs = temp["_revisions"s]["ids"s];
const auto count = temp["_revisions"s]["start"].as_integer<int>();
if (count > 1) {
for (const auto ¤t : revs.array_range()){
const auto tmp = current.as_string();
result.push_back(tmp);
}
}
std::reverse(result.begin(), result.end());
return result;
}
json Document::get(const Document::GetType type, const std::string &revision) {
DBC::GetOptions options;
if (type == GetType::Content) {
if (!revision.empty()) {
options.rev = revision;
}
else {
options.latest = true;
}
}
else {
options.revs = true;
}
const auto name = this->data.db;
const auto uuid = this->data.uuid;
const auto tmp = this->data.connection.get()->dbDoc(name, uuid, options);
this->data.revision = tmp["_rev"s].as_string();
return tmp;;
}
std::string Document::latest_revision() {
return this->revisions().back();
}
std::string Document::revision_of_content() {
return this->data.revision;
}
void Document::setRevision(const std::string &revision) {
this->data.revision = revision;
}
bool Document::purge(std::vector<std::string> revisions){
json temp;
temp[this->data.uuid] = json::array();
for (const auto ¤t : revisions){
temp[this->data.uuid].emplace_back(current);
}
return this->data.connection.get()->purgeDoc(this->data.db, temp);
}