From 38c8f8554ab883cd3f6ea8462bb574d0e93872a2 Mon Sep 17 00:00:00 2001
From: isah <isabelle.herbauts@univie.ac.at>
Date: Thu, 30 Jul 2020 11:46:01 +0200
Subject: [PATCH] api post to mongodb with disease

---
 src/App.vue                       |  2 +
 src/components/input/PhSubmit.vue | 75 +++++++++++++++++++++++++++++--
 src/mixins/filterjsonfields.js    | 20 +++++++++
 src/services/Api.js               |  8 ++++
 src/services/PostsService.js      | 12 +++++
 src/store/index.js                |  6 ++-
 6 files changed, 119 insertions(+), 4 deletions(-)
 create mode 100644 src/mixins/filterjsonfields.js
 create mode 100644 src/services/Api.js
 create mode 100644 src/services/PostsService.js

diff --git a/src/App.vue b/src/App.vue
index e918911..625f7a7 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -261,6 +261,7 @@ export default {
       collection: "",
       sampleCollection: "",
       solrbaseurl: "https://app01.cc.univie.ac.at:8983/solr/phaidra_sandbox",
+      mongobaseurl: "http://localhost:8081",
       phaidrabaseurl: "phaidra-sandbox.univie.ac.at",
       trackerbaseurl: "www.univie.ac.at/phaidra-stat/piwik",
       siteid: 19,
@@ -1193,6 +1194,7 @@ export default {
       suggester: "ols",
       url: "ols: 'http://www.ebi.ac.uk/ols/api/"
     });
+    this.$store.commit("setInstanceMongo", this.mongobaseurl);
     this.$store.commit("initStore"); // this commits initStore in every store module which has it
 
     this.createContainerForm();
diff --git a/src/components/input/PhSubmit.vue b/src/components/input/PhSubmit.vue
index 8cfce64..08ba354 100644
--- a/src/components/input/PhSubmit.vue
+++ b/src/components/input/PhSubmit.vue
@@ -10,6 +10,7 @@
         </template>
       </v-tab>
     <v-tab ripple @click="updatePrettyPrint()">Metadata preview</v-tab>
+      <v-tab ripple @click="addPost()">Mongo</v-tab>
     <!--  <v-tab v-if="templating" ripple @click="loadTemplates()">Templates</v-tab>-->
     </v-tabs>
 
@@ -46,14 +47,16 @@ import VueJsonPretty from "vue-json-pretty";
 import arrays from "../../utils/arrays";
 import jsonLd from "../../utils/json-ld";
 import fields from "../../utils/fields";
-
+import PostsService from "@/services/PostsService";
+import axios from "axios";
 import PhInputForm from "./pharma-input-fields/PhInputForm";
+import { filterjsonfields } from "@/mixins/filterjsonfields";
 
 export default {
   name: "ph-submit",
+  mixins: [filterjsonfields],
   components: {
     PhInputForm,
-
     VueJsonPretty
   },
   props: {
@@ -128,7 +131,9 @@ export default {
       previewMember: "",
       searchfieldsinput: "",
       metadatapreview: {},
-      ticked: []
+      ticked: [],
+      title: "ICI",
+      description: "voila"
     };
   },
   methods: {
@@ -239,6 +244,41 @@ export default {
           return "unknown";
       }
     },
+    submitMongo: async function() {
+      var self = this;
+
+      //var httpFormData = new FormData();
+      var myjson = {
+        title: "poupinette",
+        description: "mon poupie"
+      };
+      //httpFormData.append(JSON.stringify(myjson));
+
+      //console.log("httpFormData", self.getMetadata());
+
+      axios.post(
+        self.$store.state.settings.instance.mongo + "/posts",
+        {
+          title: "NOW",
+          description: "NOWWWWWWWWWWWWWWWWWWWWWWW"
+        },
+        {
+          headers: {
+            "Content-Type": "application/json"
+          }
+        }
+      );
+      //  fetch(self.$store.state.settings.instance.mongo, {
+      //    method: "POST",
+      //   headers: {
+      //     "Content-Type": "application/json"
+      //   },
+      //   body: JSON.stringify(myjson)
+      // })
+      // .then(response => response.json())
+      // .then(function(json) {})
+      // .catch(function(error) {});
+    },
     submit: async function() {
       var self = this;
       this.loading = true;
@@ -408,7 +448,36 @@ export default {
         self.loading = false;
         self.$store.commit("setAlerts", []);
         this.submit();
+        //need to launch the script to mongodb
+      }
+    },
+    async addPost() {
+      var self = this;
+      if (self.getMetadata().metadata["json-ld"].container) {
+        var myJson = self.getMetadata().metadata["json-ld"].container;
+        console.log(self.getField(myJson, "pharmaWien:disease"));
+        /*if (
+          self.getMetadata().metadata["json-ld"].container["dcterms:subject"]
+        ) {
+          var pharmaMetada = self.getMetadata().metadata["json-ld"].container[
+            "dcterms:subject"
+          ];
+          //filter for all the @type which are relevant
+          var result = pharmaMetada.filter(obj => {
+            return obj["@type"] === "pharmaWien:disease";
+          });
+          console.log("results", result);
+        }*/
       }
+
+      var httpFormData = new FormData();
+      //console.log("lalalal", self.getMetadata(), pharmaMetada);
+
+      await PostsService.addPost({
+        phaidraId: "objectID",
+        disease: this.description
+      });
+      //this.$router.push({ name: 'Posts' })
     }
   },
   mounted: function() {
diff --git a/src/mixins/filterjsonfields.js b/src/mixins/filterjsonfields.js
new file mode 100644
index 0000000..c6dbb74
--- /dev/null
+++ b/src/mixins/filterjsonfields.js
@@ -0,0 +1,20 @@
+export const filterjsonfields = {
+  methods: {
+    getField: function (jsonObject, type) {
+      console.log("in getfields", jsonObject)
+      if (jsonObject) {
+        if (jsonObject["dcterms:subject"]) {
+          var pharmaMetada = jsonObject["dcterms:subject"];
+          //filter for all the @type which are relevant
+          var result = pharmaMetada.filter(obj => {
+            return obj["@type"] === type;
+          });
+          console.log("results", result);
+          return result;
+        }
+      }
+    },
+
+
+  }
+};
diff --git a/src/services/Api.js b/src/services/Api.js
new file mode 100644
index 0000000..d2722d0
--- /dev/null
+++ b/src/services/Api.js
@@ -0,0 +1,8 @@
+import axios from "axios";
+
+export default () => {
+  return axios.create({
+    //baseURL: this.$store.state.settings.instance.mongo
+    baseURL: "http://localhost:8081"
+  });
+};
diff --git a/src/services/PostsService.js b/src/services/PostsService.js
new file mode 100644
index 0000000..5179471
--- /dev/null
+++ b/src/services/PostsService.js
@@ -0,0 +1,12 @@
+import Api from "@/services/Api";
+
+export default {
+  fetchPosts() {
+    console.log("in fetch posts", Api().get("posts"));
+    return Api().get("posts");
+  },
+
+  addPost(params) {
+    return Api().post("posts", params);
+  }
+};
diff --git a/src/store/index.js b/src/store/index.js
index ce99f7b..10107d6 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -14,7 +14,8 @@ export default new Vuex.Store({
       instance: {
         api: "",
         solr: "",
-        baseurl: ""
+        baseurl: "",
+        mongo: ""
       },
       global: {
         suggesters: {}
@@ -57,6 +58,9 @@ export default new Vuex.Store({
     setInstanceSolr(state, solr) {
       state.settings.instance.solr = solr;
     },
+    setInstanceMongo(state, mongo) {
+      state.settings.instance.mongo = mongo;
+    },
     setInstancePhaidra(state, baseurl) {
       state.settings.instance.baseurl = baseurl;
     },
-- 
GitLab