From a8a16a0663e298cdec1bf25144b19eb0cf6c297a Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Wed, 7 Nov 2018 14:56:02 +0100 Subject: [PATCH] instance editing with property merging --- project.clj | 1 + src/wanijo/instance/domain.clj | 32 +++++++++++++++++++++++++++----- src/wanijo/instance/forms.clj | 18 +++++++++++++----- src/wanijo/instance/view.clj | 2 +- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/project.clj b/project.clj index 24321d3..e20a9f8 100644 --- a/project.clj +++ b/project.clj @@ -31,6 +31,7 @@ [lein-kibit "LATEST"]]} :uberjar {:aot :all}} + :repl-options {:init-ns wanijo.framework.repl} :ring {:handler wanijo.handler/app} :less {:source-paths ["resources/app/stylesheets"] :target-path "resources/public/css"} diff --git a/src/wanijo/instance/domain.clj b/src/wanijo/instance/domain.clj index 5e0d5cd..56570f8 100644 --- a/src/wanijo/instance/domain.clj +++ b/src/wanijo/instance/domain.clj @@ -32,7 +32,8 @@ CREATE (p:property {uuid:{prop_uuid}})-[:of]->(i), (p)-[:of]->(a) SET p.value = {value}, - p.created_at = {created_at}") + p.created_at = {created_at}, + p.updated_at = {updated_at}") (defn create! [schema-uuid instance] (let [instance-uuid (neo4j/uuid) @@ -82,8 +83,29 @@ SET i.name = {name}, i.updated_at = {updated_at}") +(neo4j/defquery edit-property + "MATCH (i:instance {uuid:{instance_uuid}}), + (a:attribute {uuid:{attribute_uuid}}) + MERGE (p:property {uuid:{uuid}})-[:of]->(i) + MERGE (p)-[:of]->(a) + ON CREATE SET p.created_at = {now}, + p.updated_at = {now}, + p.value = {value} + ON MATCH SET p.updated_at = {now}, + p.value = {value}") + (defn edit! [instance] - (neo4j/exec-query! edit-instance - {:uuid (:uuid instance) - :name (:name instance) - :updated_at (neo4j/now-str)})) + (clojure.pprint/pprint instance) + (let [prop-tuples (map #(vector edit-property + {:uuid (:uuid % (neo4j/uuid)) + :now (neo4j/now-str) + :value (:value %) + :instance_uuid (:uuid instance) + :attribute_uuid (-> % :attribute :uuid)}) + (:properties instance))] + (apply neo4j/exec-queries! + (concat [[edit-instance + {:uuid (:uuid instance) + :name (:name instance) + :updated_at (neo4j/now-str)}]] + prop-tuples)))) diff --git a/src/wanijo/instance/forms.clj b/src/wanijo/instance/forms.clj index b14a66e..7c5c5ce 100644 --- a/src/wanijo/instance/forms.clj +++ b/src/wanijo/instance/forms.clj @@ -20,6 +20,9 @@ :required (= 1 (:required attr)) :widget (attr-type->widget (:type attr))}) +(defn attr->uuid-field-id [attr] + (keyword (str "attr-" (:uuid attr) "-prop-uuid"))) + (defn with-attributes [attrs] (update form :fields @@ -27,21 +30,26 @@ (reduce (fn [fields attr] (assoc fields (attr->field-id attr) - (attr->field attr))) + (attr->field attr) + (attr->uuid-field-id attr) + {:widget :hidden})) fields attrs)))) (defn form-data->instance [form-data attrs] {:name (:name form-data) - :properties (map (fn [ra] - {:attribute ra - :value ((attr->field-id ra) form-data)}) + :properties (map (fn [attr] + {:attribute attr + :value ((attr->field-id attr) form-data) + :uuid ((attr->uuid-field-id attr) form-data)}) attrs)}) (defn instance->form-data [instance] (merge {:name (:name instance)} (reduce #(assoc %1 (attr->field-id (:attribute %2)) - (:value %2)) + (:value %2) + (attr->uuid-field-id (:attribute %2)) + (:uuid %2)) {} (:properties instance)))) diff --git a/src/wanijo/instance/view.clj b/src/wanijo/instance/view.clj index 26f7f3c..e1aa096 100644 --- a/src/wanijo/instance/view.clj +++ b/src/wanijo/instance/view.clj @@ -41,4 +41,4 @@ [[:h1 (:name instance)] (hform/form-to [:post (path :instance-edit instance)] (form/render-widgets form form-data req) - (hform/submit-button "Create!"))])) + (hform/submit-button "Edit!"))]))