2015-10-09 07:24:12 +08:00
|
|
|
#ifndef MEMGRAPH_API_RESOURCES_NODE_HPP
|
|
|
|
#define MEMGRAPH_API_RESOURCES_NODE_HPP
|
|
|
|
|
2015-10-12 02:59:27 +08:00
|
|
|
#include <random>
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
#include "api/restful/resource.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]() {
|
|
|
|
// start a new transaction and obtain a reference to it
|
|
|
|
auto t = db->tx_engine.begin();
|
|
|
|
|
|
|
|
// insert a new vertex in the graph
|
|
|
|
auto atom = db->graph.vertices.insert(t);
|
|
|
|
|
|
|
|
// a new version was created and we got an atom along with the
|
|
|
|
// first version. obtain a pointer to the first version
|
|
|
|
//
|
|
|
|
// nullptr
|
|
|
|
// ^
|
|
|
|
// |
|
|
|
|
// [Vertex v1]
|
|
|
|
// ^
|
|
|
|
// |
|
|
|
|
// [Atom id=k] k {1, 2, ...}
|
|
|
|
//
|
|
|
|
auto node = atom->first();
|
|
|
|
|
2015-10-18 01:34:00 +08:00
|
|
|
// TODO: req.json can be empty
|
|
|
|
// probably there is some other place to handle
|
|
|
|
// emptiness of req.json
|
2015-10-17 19:26:07 +08:00
|
|
|
|
2015-10-12 02:59:27 +08:00
|
|
|
// first version
|
|
|
|
//
|
|
|
|
// for(key, value in body)
|
|
|
|
// node->properties[key] = value;
|
2015-10-18 01:34:00 +08:00
|
|
|
for(auto it = req.json.MemberBegin(); it != req.json.MemberEnd(); ++it)
|
|
|
|
{
|
|
|
|
node->properties.emplace<String>(it->name.GetString(), it->value.GetString());
|
|
|
|
}
|
2015-10-14 05:01:20 +08:00
|
|
|
|
2015-10-12 02:59:27 +08:00
|
|
|
// commit the transaction
|
|
|
|
db->tx_engine.commit(t);
|
|
|
|
|
|
|
|
// return the node we created so we can send it as a response body
|
|
|
|
return node;
|
|
|
|
},
|
|
|
|
[&req, &res](Vertex* node) {
|
|
|
|
// make a string buffer
|
2015-10-14 02:32:54 +08:00
|
|
|
StringBuffer buffer;
|
|
|
|
JsonWriter<StringBuffer> writer(buffer);
|
2015-10-12 02:59:27 +08:00
|
|
|
|
|
|
|
// dump properties in this buffer
|
2015-10-14 02:32:54 +08:00
|
|
|
node->properties.accept(writer);
|
2015-10-14 02:44:14 +08:00
|
|
|
writer.finish();
|
2015-10-12 02:59:27 +08:00
|
|
|
|
|
|
|
// respond to the use with the buffer
|
2015-10-14 02:32:54 +08:00
|
|
|
return res.send(buffer.str());
|
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-10-12 02:59:27 +08:00
|
|
|
return res.send(req.url);
|
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-10-12 02:59:27 +08:00
|
|
|
return res.send(req.url);
|
2015-10-09 07:24:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void del(sp::Request& req, sp::Response& res)
|
|
|
|
{
|
2015-10-12 02:59:27 +08:00
|
|
|
return res.send(req.url);
|
2015-10-09 07:24:12 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|