From a72ddfb7f490d2053a680531807353056b9d1a49 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Thu, 18 Oct 2018 12:58:49 +0200 Subject: [PATCH] ultra basic show action for instances --- src/wanijo/instance/domain.clj | 24 ++++++++++++++++++++++++ src/wanijo/instance/forms.clj | 4 ++-- src/wanijo/instance/routes.clj | 17 ++++++++++++++--- src/wanijo/instance/view.clj | 14 +++++++++++++- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/wanijo/instance/domain.clj b/src/wanijo/instance/domain.clj index 18451d2..2789249 100644 --- a/src/wanijo/instance/domain.clj +++ b/src/wanijo/instance/domain.clj @@ -53,3 +53,27 @@ (apply neo4j/exec-queries! (concat [instance-tuple] prop-tuples)))) + +(neo4j/defquery find-by-uuid + "MATCH (i:instance {uuid:{uuid}}) + RETURN i") + +(defn find-by-uuid! [uuid] + (->> (neo4j/exec-query! find-by-uuid + {:uuid uuid}) + (map :i) + first)) + +(neo4j/defquery find-properties + "MATCH (i:instance {uuid:{uuid}}), + (p:property)-[:of]->(i), + (p)-[:of]->(a:attribute) + RETURN p, a + ORDER BY a.name") + +(defn find-properties! [uuid] + (map #(assoc (:p %) + :attribute + (:a %)) + (neo4j/exec-query! find-properties + {:uuid uuid}))) diff --git a/src/wanijo/instance/forms.clj b/src/wanijo/instance/forms.clj index 13b11f6..4cc5df6 100644 --- a/src/wanijo/instance/forms.clj +++ b/src/wanijo/instance/forms.clj @@ -20,7 +20,7 @@ :required true :widget (attr-type->widget (:type attr))}) -(defn instance-form [schema-uuid] +(defn new-form [schema-uuid required-attrs] (update form :fields (fn [fields] @@ -29,7 +29,7 @@ (attr->field-id attr) (attr->field attr))) fields - (domain-attr/required! schema-uuid))))) + required-attrs)))) (defn form-data->instance [form-data required-attrs] {:name (:name form-data) diff --git a/src/wanijo/instance/routes.clj b/src/wanijo/instance/routes.clj index bca15d7..e478fdc 100644 --- a/src/wanijo/instance/routes.clj +++ b/src/wanijo/instance/routes.clj @@ -13,12 +13,14 @@ (defn list! [schema-uuid req] (view/list! (domain-schema/find-by-uuid! schema-uuid) (domain/find-by-schema! schema-uuid) - (forms-inst/instance-form schema-uuid) + (forms-inst/new-form schema-uuid + (domain-attr/required! schema-uuid)) req)) (defn new! [req] (let [schema-uuid (get-in req [:params :schema-uuid]) - form-def (forms-inst/instance-form schema-uuid)] + form-def (forms-inst/new-form schema-uuid + (domain-attr/required! schema-uuid))] (if (form/valid? form-def req) (let [form-data (form/form-data form-def req) req-attrs (domain-attr/required! schema-uuid) @@ -30,9 +32,18 @@ (:params req)))) (list! schema-uuid req)))) +(defn show! [uuid req] + (let [instance (assoc (domain/find-by-uuid! uuid) + :properties + (domain/find-properties! uuid))] + (view/show! instance req))) + (defroutes routes (GET (register! :instance-list "/instance/list/:schema-uuid") [schema-uuid :as req] (list! schema-uuid req)) (POST (register! :instance-new "/instance/new") [] - new!)) + new!) + (GET (register! :instance-show "/instance/:uuid") + [uuid :as req] + (show! uuid req))) diff --git a/src/wanijo/instance/view.clj b/src/wanijo/instance/view.clj index 9d08e7d..a3caaa3 100644 --- a/src/wanijo/instance/view.clj +++ b/src/wanijo/instance/view.clj @@ -23,7 +23,8 @@ (for [instance instances] [:tr [:td - [:a (:name instance)]] + [:a {:href (path :instance-show instance)} + (:name instance)]] [:td (prettify-dt (:updated_at instance))] [:td (prettify-dt (:created_at instance))]])]] [:h1 "New Instance"] @@ -32,3 +33,14 @@ (hform/hidden-field "schema-uuid" (:uuid schema)) (hform/submit-button "Create!"))])) + +(defn show! [instance req] + (view/layout! + :request req + :content + [[:h1 (:name instance)] + [:dl + (for [prop (:properties instance)] + (list + [:dt (:name (:attribute prop))] + [:dd (:value prop)]))]]))