instance editing with property merging

integration-tests
Josha von Gizycki 6 years ago
parent cccfa3e237
commit a8a16a0663

@ -31,6 +31,7 @@
[lein-kibit "LATEST"]]} [lein-kibit "LATEST"]]}
:uberjar {:aot :all}} :uberjar {:aot :all}}
:repl-options {:init-ns wanijo.framework.repl}
:ring {:handler wanijo.handler/app} :ring {:handler wanijo.handler/app}
:less {:source-paths ["resources/app/stylesheets"] :less {:source-paths ["resources/app/stylesheets"]
:target-path "resources/public/css"} :target-path "resources/public/css"}

@ -32,7 +32,8 @@
CREATE (p:property {uuid:{prop_uuid}})-[:of]->(i), CREATE (p:property {uuid:{prop_uuid}})-[:of]->(i),
(p)-[:of]->(a) (p)-[:of]->(a)
SET p.value = {value}, SET p.value = {value},
p.created_at = {created_at}") p.created_at = {created_at},
p.updated_at = {updated_at}")
(defn create! [schema-uuid instance] (defn create! [schema-uuid instance]
(let [instance-uuid (neo4j/uuid) (let [instance-uuid (neo4j/uuid)
@ -82,8 +83,29 @@
SET i.name = {name}, SET i.name = {name},
i.updated_at = {updated_at}") 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] (defn edit! [instance]
(neo4j/exec-query! edit-instance (clojure.pprint/pprint instance)
{:uuid (:uuid instance) (let [prop-tuples (map #(vector edit-property
:name (:name instance) {:uuid (:uuid % (neo4j/uuid))
:updated_at (neo4j/now-str)})) :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))))

@ -20,6 +20,9 @@
:required (= 1 (:required attr)) :required (= 1 (:required attr))
:widget (attr-type->widget (:type attr))}) :widget (attr-type->widget (:type attr))})
(defn attr->uuid-field-id [attr]
(keyword (str "attr-" (:uuid attr) "-prop-uuid")))
(defn with-attributes [attrs] (defn with-attributes [attrs]
(update form (update form
:fields :fields
@ -27,21 +30,26 @@
(reduce (fn [fields attr] (reduce (fn [fields attr]
(assoc fields (assoc fields
(attr->field-id attr) (attr->field-id attr)
(attr->field attr))) (attr->field attr)
(attr->uuid-field-id attr)
{:widget :hidden}))
fields fields
attrs)))) attrs))))
(defn form-data->instance [form-data attrs] (defn form-data->instance [form-data attrs]
{:name (:name form-data) {:name (:name form-data)
:properties (map (fn [ra] :properties (map (fn [attr]
{:attribute ra {:attribute attr
:value ((attr->field-id ra) form-data)}) :value ((attr->field-id attr) form-data)
:uuid ((attr->uuid-field-id attr) form-data)})
attrs)}) attrs)})
(defn instance->form-data [instance] (defn instance->form-data [instance]
(merge {:name (:name instance)} (merge {:name (:name instance)}
(reduce #(assoc %1 (reduce #(assoc %1
(attr->field-id (:attribute %2)) (attr->field-id (:attribute %2))
(:value %2)) (:value %2)
(attr->uuid-field-id (:attribute %2))
(:uuid %2))
{} {}
(:properties instance)))) (:properties instance))))

@ -41,4 +41,4 @@
[[:h1 (:name instance)] [[:h1 (:name instance)]
(hform/form-to [:post (path :instance-edit instance)] (hform/form-to [:post (path :instance-edit instance)]
(form/render-widgets form form-data req) (form/render-widgets form form-data req)
(hform/submit-button "Create!"))])) (hform/submit-button "Edit!"))]))

Loading…
Cancel
Save