From 06b049f0ff3d97a1933f0ef3acecee269391adde Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Tue, 8 Jan 2019 20:08:18 +0100 Subject: [PATCH] creating links between instances --- src/wanijo/instance/domain.clj | 14 ++++++++++++++ src/wanijo/instance/forms.clj | 9 +++++++++ src/wanijo/instance/routes.clj | 34 ++++++++++++++++++++++++++++++++-- src/wanijo/instance/view.clj | 28 ++++++++++++++++++++++++++-- 4 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/wanijo/instance/domain.clj b/src/wanijo/instance/domain.clj index 805de03..beea3ec 100644 --- a/src/wanijo/instance/domain.clj +++ b/src/wanijo/instance/domain.clj @@ -122,3 +122,17 @@ (defn delete! [uuid] (neo4j/exec-query! delete {:uuid uuid})) + +(neo4j/defquery create-link + "MATCH (i:instance {uuid:{instance_uuid}}), + (i2) + WHERE i2.uuid IN {instance_uuids} + CREATE (i)-[:link {name:{name}, + created_at:{created_at}}]->(i2)") + +(defn create-link! [instance-uuid name instance-uuids] + (neo4j/exec-query! create-link + {:instance_uuid instance-uuid + :name name + :created_at (neo4j/now-str) + :instance_uuids instance-uuids})) diff --git a/src/wanijo/instance/forms.clj b/src/wanijo/instance/forms.clj index 01e452a..0417cb5 100644 --- a/src/wanijo/instance/forms.clj +++ b/src/wanijo/instance/forms.clj @@ -6,6 +6,15 @@ :required true :spec ::domain/name}}}) +(defn link-form [instances] + {:fields {:name {:label "Name" + :required true} + :instances {:label "Instances" + :widget :mselect + :options (map #(vector (:name %) + (:uuid %)) + instances)}}}) + (defn attr-type->widget [attr-type] (case attr-type ("markdown" "text") :textarea diff --git a/src/wanijo/instance/routes.clj b/src/wanijo/instance/routes.clj index 2f0acbe..15cd3b3 100644 --- a/src/wanijo/instance/routes.clj +++ b/src/wanijo/instance/routes.clj @@ -48,10 +48,12 @@ (defn edit-form! [uuid req] (let [instance (instance! uuid) - attrs (domain-attr/find-by-instance! uuid)] + attrs (domain-attr/find-by-instance! uuid) + user-uuid (get-in req [:session :uuid])] (view/edit! instance (form! uuid) (forms-inst/instance->form-data instance) + (domain-schema/accessible-schemas! user-uuid) req))) (defn edit! [uuid req] @@ -72,6 +74,26 @@ (resp/redirect (path :instance-list {:schema-uuid (:uuid schema)})))) +(defn link-form! [schema-uuid] + (forms-inst/link-form + (domain/find-by-schema! schema-uuid))) + +(defn link-selection! [uuid schema-uuid req] + (view/link-selection (instance! uuid) + (domain-schema/find-by-uuid! schema-uuid) + (link-form! schema-uuid) + req)) + +(defn create-link! [uuid schema-uuid req] + (let [form (link-form! schema-uuid)] + (if (form/valid? form req) + (let [form-data (form/form-data form req)] + (domain/create-link! uuid + (:name form-data) + (:instances form-data)) + (resp/redirect (path :instance-show {:uuid uuid}))) + (link-selection! uuid schema-uuid req)))) + (defroutes routes (wrap-routes (GET (register! :instance-list "/instance/list/:schema-uuid") @@ -92,4 +114,12 @@ (edit! uuid req)) (DELETE (register! :instance-delete "/instance/:uuid") [uuid :as req] - (delete! uuid req))) + (delete! uuid req)) + (GET (register! :instance-link-selection + "/instance/:uuid/link/:schema-uuid") + [uuid schema-uuid :as req] + (link-selection! uuid schema-uuid req)) + (POST (register! :instance-create-link + "/instance/:uuid/link/:schema-uuid") + [uuid schema-uuid :as req] + (create-link! uuid schema-uuid req))) diff --git a/src/wanijo/instance/view.clj b/src/wanijo/instance/view.clj index 7533475..4c18005 100644 --- a/src/wanijo/instance/view.clj +++ b/src/wanijo/instance/view.clj @@ -62,7 +62,7 @@ "attr-type-" type)} (render-fn (:value prop))]))])) -(defn edit! [instance form form-data req] +(defn edit! [instance form form-data schemas req] (view/layout! :request req :content @@ -70,9 +70,33 @@ (h (-> instance :schema :name)) " " [:small (h (:name instance))]] + [:h2 "Edit Instance"] (hform/form-to [:post (path :instance-edit instance)] (form/render-widgets form form-data req) (hform/submit-button "Edit!")) (hform/form-to [:delete (path :instance-delete instance)] (anti-forgery-field) - (view/delete-btn))])) + (view/delete-btn)) + [:h2 "Link Instance with Instance of Schema..."] + [:ul + (for [schema schemas] + [:li + [:a {:href (path :instance-link-selection + {:uuid (:uuid instance) + :schema-uuid (:uuid schema)})} + (h (:name schema))]])]])) + +(defn link-selection [instance schema form req] + (view/layout! + :request req + :content + [[:h1 + [:small "Link " (-> instance :schema :name) " "] + (h (:name instance)) + [:small " with "] + (h (:name schema))] + (hform/form-to [:post (path :instance-create-link + {:uuid (:uuid instance) + :schema-uuid (:uuid schema)})] + (form/render-widgets form nil req) + (hform/submit-button "Link!"))]))