From 6cd15d3333419e9b5ac09f2e7e68e33737534733 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Tue, 27 Jul 2021 18:15:54 +0200
Subject: [PATCH] WIP: insert delete

---
 include/caosdb/entity.h | 52 ++++++++++++++++++++++++++++++++-----
 src/CMakeLists.txt      |  1 +
 src/caosdb/entity.cpp   | 57 +++++++++++++++++++++++++++++++++++++++++
 test/CMakeLists.txt     |  1 +
 test/test_entity.cpp    | 46 +++++++++++++++++++++++++++++++++
 5 files changed, 150 insertions(+), 7 deletions(-)
 create mode 100644 src/caosdb/entity.cpp
 create mode 100644 test/test_entity.cpp

diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h
index 4803ae7..9141e32 100644
--- a/include/caosdb/entity.h
+++ b/include/caosdb/entity.h
@@ -35,6 +35,10 @@
 #include <string> // for string
 
 namespace caosdb::entity {
+using ProtoParent = caosdb::entity::v1alpha1::Parent;
+using ProtoEntity = caosdb::entity::v1alpha1::Entity;
+
+class Parents;
 
 /**
  * Parent of an Entity.
@@ -43,12 +47,20 @@ class Parent {
 public:
   explicit inline Parent(caosdb::entity::v1alpha1::Parent *wrapped)
     : wrapped(wrapped){};
+  inline Parent() : wrapped(new ProtoParent){};
   [[nodiscard]] auto GetId() const -> const std::string &;
   [[nodiscard]] auto GetName() const -> const std::string &;
   [[nodiscard]] auto GetDescription() const -> const std::string &;
+  auto SetId(const std::string &id) -> void;
+  auto SetName(const std::string &name) -> void;
+
+  friend class Parents;
 
 private:
-  caosdb::entity::v1alpha1::Parent *wrapped;
+  auto AppendTo(
+    ::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
+      *parents) const -> void;
+  std::shared_ptr<caosdb::entity::v1alpha1::Parent> wrapped;
 };
 
 /**
@@ -61,7 +73,11 @@ public:
     ::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
       *wrapped)
     : wrapped(wrapped){};
-  [[nodiscard]] auto At(int index) const -> const Parent &;
+  auto Append(const Parent &parent) -> void;
+  [[nodiscard]] inline auto Size() const -> int { return wrapped->size(); }
+  [[nodiscard]] inline auto At(int index) const -> const Parent {
+    return Parent(&(wrapped->at(index)));
+  }
 
 private:
   ::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
@@ -72,8 +88,8 @@ private:
 /**
  * Property of an Entity.
  *
- * This is a property which belongs to another entity. Don't confuse it with an
- * Entity with the "Property" role.
+ * This is a property which belongs to another entity. Don't confuse it with
+ * an Entity with the "Property" role.
  *
  * @brief Property of an Entity.
  */
@@ -88,6 +104,12 @@ public:
   [[nodiscard]] auto GetValue() const -> const std::string &;
   [[nodiscard]] auto GetUnit() const -> const std::string &;
   [[nodiscard]] auto GetDatatype() const -> const std::string &;
+  auto SetId(const std::string &id) -> void;
+  auto SetName(const std::string &name) -> void;
+  auto SetImportance(const std::string &importance) -> void;
+  auto SetValue(const std::string &value) -> void;
+  auto SetUnit(const std::string &unit) -> void;
+  auto SetDatatype(const std::string &datatype) -> void;
 
 private:
   caosdb::entity::v1alpha1::Property *wrapped;
@@ -104,6 +126,7 @@ public:
       *wrapped)
     : wrapped(wrapped){};
   [[nodiscard]] auto At(int index) const -> const Property &;
+  auto Append(const Property &property) -> void;
 
 private:
   ::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Property>
@@ -152,9 +175,15 @@ private:
  */
 class Entity {
 public:
-  explicit inline Entity(caosdb::entity::v1alpha1::Entity *wrapped)
-    : wrapped(wrapped) {
+  inline Entity() : wrapped(new ProtoEntity) {
+    properties.wrapped = this->wrapped->mutable_properties();
+    parents.wrapped = this->wrapped->mutable_parents();
+    errors.wrapped = this->wrapped->mutable_errors();
+  };
+  explicit inline Entity(ProtoEntity *wrapped) : wrapped(wrapped) {
     errors.wrapped = this->wrapped->mutable_errors();
+    properties.wrapped = this->wrapped->mutable_properties();
+    parents.wrapped = this->wrapped->mutable_parents();
   };
 
   [[nodiscard]] inline auto GetId() const -> const std::string & {
@@ -198,8 +227,17 @@ public:
     return out;
   }
 
+  auto SetId(const std::string &id) -> void;
+  auto SetName(const std::string &name) -> void;
+  auto SetImportance(const std::string &importance) -> void;
+  auto SetValue(const std::string &value) -> void;
+  auto SetUnit(const std::string &unit) -> void;
+  auto SetDatatype(const std::string &datatype) -> void;
+  auto AppendProperty(const Property &property) -> void;
+  auto AppendParent(const Parent &parent) -> void;
+
 private:
-  std::unique_ptr<caosdb::entity::v1alpha1::Entity> wrapped;
+  std::unique_ptr<ProtoEntity> wrapped;
   Properties properties;
   Parents parents;
   Messages errors;
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ce19975..67074f7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -22,6 +22,7 @@
 # add all source files to this list
 set(libcaosdb_SRC
     ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/entity.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/logging.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/configuration.cpp
diff --git a/src/caosdb/entity.cpp b/src/caosdb/entity.cpp
new file mode 100644
index 0000000..65a641e
--- /dev/null
+++ b/src/caosdb/entity.cpp
@@ -0,0 +1,57 @@
+/*
+ * This file is a part of the CaosDB Project.
+ * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
+ * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+#include "caosdb/entity.h"
+#include "caosdb/entity/v1alpha1/main.grpc.pb.h" // for EntityTransactionS...
+#include "caosdb/entity/v1alpha1/main.pb.h"      // for SingleRetrieveRequest
+#include <cassert>                               // for assert
+#include <map>                                   // for map
+#include <memory>                                // for allocator, unique_ptr
+#include <stdexcept>                             // for out_of_range
+#include <utility>                               // for move
+
+namespace caosdb::entity {
+auto Parent::SetName(const std::string &name) -> void {
+  this->wrapped->set_name(name);
+}
+
+auto Parent::SetId(const std::string &id) -> void { this->wrapped->set_id(id); }
+
+auto Parent::AppendTo(
+  ::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
+    *parents) const -> void {
+  auto *destination = parents->Add();
+  destination = std::move(this->wrapped.get());
+}
+
+auto Parents::Append(const Parent &parent) -> void {
+  auto *destination = this->wrapped->Add();
+  destination = std::move(parent.wrapped.get());
+
+  // parent.AppendTo(this->wrapped);
+}
+
+[[nodiscard]] auto Entity::GetParents() const -> const Parents & {
+  return parents;
+}
+
+auto Entity::AppendParent(const Parent &parent) -> void {
+  this->parents.Append(parent);
+}
+} // namespace caosdb::entity
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index aab1f0a..4eb876b 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -22,6 +22,7 @@
 set(test_cases
     test_configuration
     test_connection
+    test_entity
     test_info
     test_transaction
     test_utility
diff --git a/test/test_entity.cpp b/test/test_entity.cpp
new file mode 100644
index 0000000..ff0fa68
--- /dev/null
+++ b/test/test_entity.cpp
@@ -0,0 +1,46 @@
+/*
+ *
+ * This file is a part of the CaosDB Project.
+ *
+ * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
+ * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+#include "caosdb/entity.h"                  // for Entity
+#include "caosdb/entity/v1alpha1/main.pb.h" // for Entity
+#include "caosdb/exceptions.h"              // for ConnectionError
+#include "caosdb/transaction.h"             // for Transaction, UniqueResult
+#include "caosdb/transaction_status.h"      // for ConnectionError
+#include "caosdb_test_utility.h"            // for EXPECT_THROW_MESSAGE
+#include "gtest/gtest-message.h"            // for Message
+#include "gtest/gtest-test-part.h"          // for SuiteApiResolver, TestPa...
+#include "gtest/gtest_pred_impl.h"          // for Test, TestInfo, TEST
+#include <memory>                           // for allocator, unique_ptr
+
+namespace caosdb::entity {
+
+TEST(test_entity, test_append_parent) {
+  auto parent = Parent();
+  parent.SetName("RT1");
+  parent.SetId("some-id");
+
+  auto entity = Entity();
+  EXPECT_EQ(entity.GetParents().Size(), 0);
+  entity.AppendParent(parent);
+  EXPECT_EQ(entity.GetParents().Size(), 1);
+}
+
+} // namespace caosdb::entity
-- 
GitLab