memgraph/api/resources/node.hpp

94 lines
2.5 KiB
C++
Raw Normal View History

2015-10-09 07:24:12 +08:00
#ifndef MEMGRAPH_API_RESOURCES_NODE_HPP
#define MEMGRAPH_API_RESOURCES_NODE_HPP
#include <random>
2015-10-09 07:24:12 +08:00
#include "api/restful/resource.hpp"
#include "debug/log.hpp"
2015-10-09 07:24:12 +08:00
#pragma url /node
class Nodes : public Resource<Nodes, POST>
2015-10-09 07:24:12 +08:00
{
public:
using Resource::Resource;
void post(sp::Request& req, sp::Response& res)
2015-10-09 07:24:12 +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();
// 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
// first version
//
// for(key, value in body)
// node->properties[key] = value;
for(auto it = req.json.MemberBegin(); it != req.json.MemberEnd(); ++it)
{
node->properties.emplace<String>(it->name.GetString(), it->value.GetString());
}
// 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);
// 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();
// respond to the use with the buffer
2015-10-14 02:32:54 +08:00
return res.send(buffer.str());
});
2015-10-09 07:24:12 +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
void get(sp::Request& req, sp::Response& res)
2015-10-09 07:24:12 +08:00
{
return res.send(req.url);
2015-10-09 07:24:12 +08:00
}
2015-10-09 07:24:12 +08:00
void put(sp::Request& req, sp::Response& res)
{
return res.send(req.url);
2015-10-09 07:24:12 +08:00
}
void del(sp::Request& req, sp::Response& res)
{
return res.send(req.url);
2015-10-09 07:24:12 +08:00
}
};
#endif