memgraph/api/resources/node.hpp

130 lines
3.5 KiB
C++
Raw Normal View History

#pragma once
2015-10-09 07:24:12 +08:00
#include <random>
2015-10-09 07:24:12 +08:00
#include "api/restful/resource.hpp"
#include "mvcc/version_list.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]() {
// create transaction
auto& transaction = db->tx_engine.begin();
// insert a new vertex
auto vertex = db->graph.vertices.insert(transaction);
// 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) {
return res.send(properties_to_string(node));
});
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
{
task->run([this, &req]() {
// 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.find(transaction, id);
// commit the transaction
transaction.commit();
return vertex;
},
[&req, &res](const 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
}
2015-10-09 07:24:12 +08:00
void put(sp::Request& req, sp::Response& res)
{
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)
{
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
}
};