From 26b426e1fa9def3613c2de7deb182d0a78a5aa05 Mon Sep 17 00:00:00 2001
From: Marko Budiselic <mbudiselicbuda@gmail.com>
Date: Thu, 5 Nov 2015 00:46:13 +0100
Subject: [PATCH] A node could now be found by id. GET /db/data/node/{id}
 returns the node with specified id.

---
 api/resources/node.hpp                 | 43 ++++++++++++++++++--------
 data_structures/list/lockfree_list.hpp |  7 +++++
 speedy/r3.hpp                          |  1 +
 storage/vertex.hpp                     | 15 +++++++++
 4 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/api/resources/node.hpp b/api/resources/node.hpp
index 8156c776b..9af7e155e 100644
--- a/api/resources/node.hpp
+++ b/api/resources/node.hpp
@@ -54,16 +54,7 @@ public:
             return node;
         }, 
         [&req, &res](Vertex* node) {
-            // make a string buffer
-            StringBuffer buffer;
-            JsonWriter<StringBuffer> writer(buffer);
-
-            // dump properties in this buffer
-            node->properties.accept(writer);
-            writer.finish();
-            
-            // respond to the use with the buffer
-            return res.send(buffer.str());
+            return res.send(properties_to_string(node));
         });
     }
 };
@@ -76,17 +67,43 @@ public:
         
     void get(sp::Request& req, sp::Response& res)
     {
-        return res.send(req.url);
+        task->run([this, &req]() -> Vertex* {
+            // read id param
+            uint64_t id = std::stoull(req.params[0]); 
+
+            // get atom iterator
+            auto atom_it = db->graph.vertices.begin();
+            
+            // find the right atom
+            // TODO: better implementation
+            while (true) {
+                if (id == atom_it->id) {
+                    // TODO: return latest version
+                    return atom_it->first();
+                }
+                if (!atom_it.has_next())
+                    break;
+                ++atom_it;
+            }
+
+            return nullptr;
+        },
+        [&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));
+        });
     }
 
     void put(sp::Request& req, sp::Response& res)
     {
-        return res.send(req.url);
+        return res.send("TODO");
     }
 
     void del(sp::Request& req, sp::Response& res)
     {
-        return res.send(req.url);
+        return res.send("TODO");
     }
 };
 
diff --git a/data_structures/list/lockfree_list.hpp b/data_structures/list/lockfree_list.hpp
index 96d8d07e9..07a3b2af9 100644
--- a/data_structures/list/lockfree_list.hpp
+++ b/data_structures/list/lockfree_list.hpp
@@ -55,6 +55,13 @@ public:
         {
             return operator++();
         }
+
+        bool has_next()
+        {
+            if (curr->next == nullptr)
+                return false;
+            return true;
+        }
   
     private:
         T* curr;
diff --git a/speedy/r3.hpp b/speedy/r3.hpp
index a93d47f19..f813ba6ec 100644
--- a/speedy/r3.hpp
+++ b/speedy/r3.hpp
@@ -75,6 +75,7 @@ public:
 
         void populate(sp::Request& req)
         {
+            req.params.clear();
             for(int i = 0; i < entry->vars->len; ++i)
                 req.params.emplace_back(entry->vars->tokens[i]);
         }
diff --git a/storage/vertex.hpp b/storage/vertex.hpp
index 56dee9fd6..bcf3ad7b3 100644
--- a/storage/vertex.hpp
+++ b/storage/vertex.hpp
@@ -27,4 +27,19 @@ inline std::ostream& operator<<(std::ostream& stream, Vertex& record)
                   << ", xmax = " << record.tx.max()
                   << "): " << buffer.str();
 }
+
+// TODO: find more appropriate place for this
+inline std::string properties_to_string(Vertex* vertex)
+{
+    // make a string buffer
+    StringBuffer buffer;
+    JsonWriter<StringBuffer> writer(buffer);
+
+    // dump properties in this buffer
+    vertex->properties.accept(writer);
+    writer.finish();
+
+    // respond to the use with the buffer
+    return std::move(buffer.str());
+}
 #endif