creating instances
							parent
							
								
									61d5714ba8
								
							
						
					
					
						commit
						923bd69754
					
				| @ -0,0 +1,56 @@ | ||||
| (ns wanijo.instance.domain | ||||
|   (:require [clojure.spec.alpha :as spec] | ||||
|             [wanijo.framework.neo4j :as neo4j] | ||||
|             [clojure.pprint :as pprint])) | ||||
| 
 | ||||
| (spec/def ::created-at ::neo4j/date-str) | ||||
| (spec/def ::updated-at ::neo4j/date-str) | ||||
| (spec/def ::name (spec/and (complement empty?) string?)) | ||||
| 
 | ||||
| (neo4j/defquery | ||||
|   findy-by-schema | ||||
|   "MATCH (i:instance)-->(s:schema) | ||||
|    WHERE s.uuid = {uuid} | ||||
|    RETURN i | ||||
|    ORDER BY i.updated_at DESC") | ||||
| 
 | ||||
| (defn find-by-schema! [schema-uuid] | ||||
|   (->> | ||||
|    (neo4j/exec-query! | ||||
|     findy-by-schema | ||||
|     {:uuid schema-uuid}) | ||||
|    (map :i))) | ||||
| 
 | ||||
| (neo4j/defquery create-instance | ||||
|   "MATCH (s:schema {uuid:{schema_uuid}}) | ||||
|    CREATE (i:instance {uuid:{uuid}})-[:of]->(s) | ||||
|       SET i.name = {name}, | ||||
|           i.created_at = {created_at}, | ||||
|           i.updated_at = {created_at}") | ||||
| 
 | ||||
| (neo4j/defquery create-property | ||||
|   "MATCH (i:instance {uuid:{uuid}}), | ||||
|          (a:attribute {uuid:{attr_uuid}}) | ||||
|    CREATE (p:property {uuid:{prop_uuid}})-[:of]->(i), | ||||
|           (p)-[:of]->(a) | ||||
|      SET p.value = {value}, | ||||
|          p.created_at = {created_at}") | ||||
| 
 | ||||
| (defn create! [schema-uuid instance] | ||||
|   (let [instance-uuid (neo4j/uuid) | ||||
|         now (neo4j/now-str) | ||||
|         instance-tuple [create-instance | ||||
|                         {:schema_uuid schema-uuid | ||||
|                          :name (:name instance) | ||||
|                          :uuid instance-uuid | ||||
|                          :created_at now}] | ||||
|         prop-tuples (for [{:keys [attribute value]} (:properties instance)] | ||||
|                       [create-property | ||||
|                        {:uuid instance-uuid | ||||
|                         :attr_uuid (:uuid attribute) | ||||
|                         :prop_uuid (neo4j/uuid) | ||||
|                         :value value | ||||
|                         :created_at now}])] | ||||
|     (apply neo4j/exec-queries! | ||||
|            (concat [instance-tuple] | ||||
|                    prop-tuples)))) | ||||
| @ -0,0 +1,66 @@ | ||||
| (ns wanijo.instance.routes | ||||
|   (:require [compojure.core :refer [defroutes GET POST DELETE]] | ||||
|             [ring.util.response :as resp] | ||||
|             [formulare.core :as form] | ||||
|             [wanijo.instance.view :as view] | ||||
|             [wanijo.instance.domain :as domain] | ||||
|             [wanijo.framework.routing :refer [register! path]] | ||||
|             [wanijo.schema.domain :as domain-schema] | ||||
|             [wanijo.attribute.domain :as domain-attr])) | ||||
| 
 | ||||
| (defn attr-type->widget [type] | ||||
|   (case type | ||||
|     ("markdown" "text") :textarea | ||||
|     :input)) | ||||
| 
 | ||||
| (defn attr->field-id [attr] | ||||
|   (keyword (str "attr-" (:name attr)))) | ||||
| 
 | ||||
| (defn attr->field [attr] | ||||
|   {:label (:name attr) | ||||
|    :required true | ||||
|    :widget (attr-type->widget (:type attr))}) | ||||
| 
 | ||||
| (defn new-form [schema-uuid] | ||||
|   (update view/new-form | ||||
|           :fields | ||||
|           (fn [fields] | ||||
|             (reduce (fn [fields attr] | ||||
|                       (assoc fields | ||||
|                              (attr->field-id attr) | ||||
|                              (attr->field attr))) | ||||
|                     fields | ||||
|                     (domain-attr/required! schema-uuid))))) | ||||
| 
 | ||||
| (defn list! [schema-uuid req] | ||||
|   (view/list! (domain-schema/find-by-uuid! schema-uuid) | ||||
|               (domain/find-by-schema! schema-uuid) | ||||
|               (new-form schema-uuid) | ||||
|               req)) | ||||
| 
 | ||||
| (defn form-data->instance [form-data required-attrs] | ||||
|   {:name (:name form-data) | ||||
|    :properties (map (fn [ra] | ||||
|                       {:attribute ra | ||||
|                        :value ((attr->field-id ra) form-data)}) | ||||
|                     required-attrs)}) | ||||
| 
 | ||||
| (defn new! [req] | ||||
|   (let [schema-uuid (get-in req [:params :schema-uuid]) | ||||
|         form-def (new-form schema-uuid)] | ||||
|     (if (form/valid? form-def req) | ||||
|       (let [instance (form-data->instance | ||||
|                       (form/form-data form-def req) | ||||
|                       (domain-attr/required! schema-uuid))] | ||||
|         (domain/create! schema-uuid | ||||
|                         instance) | ||||
|         (resp/redirect (path :instance-list | ||||
|                              (:params req)))) | ||||
|       (list! schema-uuid 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!)) | ||||
| @ -0,0 +1,39 @@ | ||||
| (ns wanijo.instance.view | ||||
|   (:require [hiccup.form :as hform] | ||||
|             [ring.util.anti-forgery :refer [anti-forgery-field]] | ||||
|             [formulare.core :as form] | ||||
|             [wanijo.instance.domain :as domain] | ||||
|             [wanijo.framework.view :as view] | ||||
|             [wanijo.framework.routing :refer [path]] | ||||
|             [wanijo.framework.time :refer [prettify-dt]])) | ||||
| 
 | ||||
| (def new-form | ||||
|   {:fields {:name {:label "Name" | ||||
|                    :required true | ||||
|                    :spec ::domain/name}}}) | ||||
| 
 | ||||
| (defn list! [schema instances new-form req] | ||||
|   (view/layout! | ||||
|    :request req | ||||
|    :content | ||||
|    [[:h1 "All Instances of schema " | ||||
|      [:span.schema-title__name (:name schema)]] | ||||
|     [:table | ||||
|      [:thead | ||||
|       [:tr | ||||
|        [:th "Name"] | ||||
|        [:th "Updated"] | ||||
|        [:th "Created"]]] | ||||
|      [:tbody | ||||
|       (for [instance instances] | ||||
|         [:tr | ||||
|          [:td | ||||
|           [:a (:name instance)]] | ||||
|          [:td (prettify-dt (:updated_at instance))] | ||||
|          [:td (prettify-dt (:created_at instance))]])]] | ||||
|     [:h1 "New Instance"] | ||||
|     (hform/form-to [:post (path :instance-new)] | ||||
|                    (form/render-widgets new-form {} req) | ||||
|                    (hform/hidden-field "schema-uuid" | ||||
|                                        (:uuid schema)) | ||||
|                    (hform/submit-button "Create!"))])) | ||||
					Loading…
					
					
				
		Reference in New Issue
	
	 Josha von Gizycki
						Josha von Gizycki