2015-12-09 03:49:45 +08:00
|
|
|
#pragma once
|
2015-10-09 07:24:12 +08:00
|
|
|
|
2015-10-12 02:59:27 +08:00
|
|
|
#include <random>
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
#include "api/restful/resource.hpp"
|
2015-12-08 04:51:55 +08:00
|
|
|
#include "mvcc/version_list.hpp"
|
2015-10-12 02:59:27 +08:00
|
|
|
#include "debug/log.hpp"
|
2015-10-09 07:24:12 +08:00
|
|
|
|
2015-10-12 02:59:27 +08:00
|
|
|
#pragma url /node
|
|
|
|
class Nodes : public Resource<Nodes, POST>
|
2015-10-09 07:24:12 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-10-12 02:59:27 +08:00
|
|
|
using Resource::Resource;
|
|
|
|
|
|
|
|
void post(sp::Request& req, sp::Response& res)
|
2015-10-09 07:24:12 +08:00
|
|
|
{
|
2015-10-12 02:59:27 +08:00
|
|
|
task->run([this, &req]() {
|
2015-12-09 03:49:45 +08:00
|
|
|
// create transaction
|
2015-12-08 05:43:54 +08:00
|
|
|
auto& transaction = db->tx_engine.begin();
|
2015-10-12 02:59:27 +08:00
|
|
|
|
2015-12-09 03:49:45 +08:00
|
|
|
// insert a new vertex
|
2015-12-13 00:57:07 +08:00
|
|
|
auto vertex = db->graph.vertices.insert(transaction);
|
2015-12-09 03:49:45 +08:00
|
|
|
|
|
|
|
// map fields
|
2015-12-08 05:43:54 +08:00
|
|
|
for(auto it = req.json.MemberBegin(); it != req.json.MemberEnd(); ++it)
|
|
|
|
{
|
|
|
|
vertex->data.props.set<String>(it->name.GetString(), it->value.GetString());
|
|
|
|
}
|
2015-12-13 00:57:07 +08:00
|
|
|
|
|
|
|
// commit the transaction
|
2015-12-08 05:43:54 +08:00
|
|
|
transaction.commit();
|
2015-10-12 02:59:27 +08:00
|
|
|
|
2015-12-09 03:49:45 +08:00
|
|
|
return vertex;
|
2015-10-12 02:59:27 +08:00
|
|
|
},
|
|
|
|
[&req, &res](Vertex* node) {
|
2015-12-09 03:49:45 +08:00
|
|
|
return res.send(properties_to_string(node));
|
2015-10-12 02:59:27 +08:00
|
|
|
});
|
2015-10-09 07:24:12 +08:00
|
|
|
}
|
2015-10-12 02:59:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#pragma url /node/{id:\\d+}
|
|
|
|
class Node : public Resource<Node, GET, PUT, DELETE>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Resource::Resource;
|
2015-10-09 07:24:12 +08:00
|
|
|
|
2015-10-12 02:59:27 +08:00
|
|
|
void get(sp::Request& req, sp::Response& res)
|
2015-10-09 07:24:12 +08:00
|
|
|
{
|
2015-12-09 03:49:45 +08:00
|
|
|
task->run([this, &req]() {
|
|
|
|
// create transaction
|
|
|
|
auto& transaction = db->tx_engine.begin();
|
|
|
|
|
|
|
|
// read id param
|
|
|
|
Id id(std::stoull(req.params[0]));
|
|
|
|
|
|
|
|
// find node
|
2015-12-13 00:57:07 +08:00
|
|
|
auto vertex = db->graph.vertices.find(transaction, id);
|
2015-12-09 03:49:45 +08:00
|
|
|
|
2015-12-13 00:57:07 +08:00
|
|
|
// commit the transaction
|
2015-12-09 03:49:45 +08:00
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
return vertex;
|
2015-11-05 07:46:13 +08:00
|
|
|
},
|
2015-12-09 03:49:45 +08:00
|
|
|
[&req, &res](const Vertex* node) {
|
2015-11-05 07:46:13 +08:00
|
|
|
if (node == nullptr) {
|
|
|
|
return res.send(http::Status::NotFound, "The node was not found");
|
|
|
|
}
|
|
|
|
return res.send(properties_to_string(node));
|
|
|
|
});
|
2015-10-09 07:24:12 +08:00
|
|
|
}
|
2015-10-12 02:59:27 +08:00
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
void put(sp::Request& req, sp::Response& res)
|
|
|
|
{
|
2015-12-13 00:57:07 +08:00
|
|
|
task->run([this, &req]() -> Vertex* {
|
|
|
|
// create transaction
|
|
|
|
auto& transaction = db->tx_engine.begin();
|
|
|
|
|
|
|
|
// read id param
|
|
|
|
Id id(std::stoull(req.params[0]));
|
|
|
|
|
|
|
|
// find node
|
|
|
|
auto vertex = db->graph.vertices.update(transaction, id);
|
|
|
|
|
|
|
|
if (vertex == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// map fields
|
|
|
|
for(auto it = req.json.MemberBegin(); it != req.json.MemberEnd(); ++it)
|
|
|
|
{
|
|
|
|
vertex->data.props.set<String>(it->name.GetString(), it->value.GetString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// commit the transaction
|
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
return vertex;
|
|
|
|
},
|
|
|
|
[&req, &res](Vertex* node) {
|
|
|
|
if (node == nullptr) {
|
|
|
|
return res.send(http::Status::NotFound, "The node was not found");
|
|
|
|
}
|
|
|
|
return res.send(properties_to_string(node));
|
|
|
|
});
|
2015-10-09 07:24:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void del(sp::Request& req, sp::Response& res)
|
|
|
|
{
|
2015-12-13 00:57:07 +08:00
|
|
|
task->run([this, &req]() -> bool {
|
|
|
|
// create transaction
|
|
|
|
auto& transaction = db->tx_engine.begin();
|
|
|
|
|
|
|
|
// read id param
|
|
|
|
Id id(std::stoull(req.params[0]));
|
|
|
|
|
|
|
|
auto is_deleted = db->graph.vertices.remove(transaction, id);
|
|
|
|
|
|
|
|
// commit the transaction
|
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
return is_deleted;
|
|
|
|
},
|
|
|
|
[&req, &res](bool is_deleted) {
|
|
|
|
if (is_deleted)
|
|
|
|
return res.send(http::Status::Ok, "The node was deleted");
|
|
|
|
|
|
|
|
return res.send(http::Status::NotFound, "The node was not found");
|
|
|
|
});
|
2015-10-09 07:24:12 +08:00
|
|
|
}
|
|
|
|
};
|