creating instances

integration-tests
Josha von Gizycki 6 years ago
parent 61d5714ba8
commit 923bd69754

@ -26,7 +26,8 @@
[lein-ancient "0.6.15"] [lein-ancient "0.6.15"]
[jonase/eastwood "0.3.1"] [jonase/eastwood "0.3.1"]
[lein-bikeshed "0.5.1"] [lein-bikeshed "0.5.1"]
[lein-cloverage "1.0.13"]]} [lein-cloverage "1.0.13"]
[cider/cider-nrepl "LATEST"]]}
:uberjar {:aot :all}} :uberjar {:aot :all}}
:ring {:handler wanijo.handler/app} :ring {:handler wanijo.handler/app}

@ -12,7 +12,7 @@
(spec/and string? types)) (spec/and string? types))
(spec/def ::required (spec/def ::required
#(some (partial = %) ["on" nil 0 1 "0" "1"])) #{"on" nil 0 1 "0" "1"})
(spec/def ::created-at (spec/def ::created-at
(spec/and string? ::neo4j/date-str)) (spec/and string? ::neo4j/date-str))
@ -47,13 +47,11 @@
(defn create-new! [attr schema-uuid user-uuid] (defn create-new! [attr schema-uuid user-uuid]
(neo4j/exec-query! (neo4j/exec-query!
create-new create-new
(-> attr (assoc attr
(assoc :user_uuid user-uuid
:user_uuid user-uuid :schema_uuid schema-uuid
:schema_uuid schema-uuid :attribute_uuid (neo4j/uuid)
:attribute_uuid (neo4j/uuid) :created_at (neo4j/now-str))))
:created_at (neo4j/now-str))
(update :required #(if (some? %) 1 0)))))
(neo4j/defquery edit (neo4j/defquery edit
"MATCH (a:attribute) "MATCH (a:attribute)
@ -77,3 +75,14 @@
(neo4j/exec-query! (neo4j/exec-query!
delete-by-uuid delete-by-uuid
{:uuid uuid})) {:uuid uuid}))
(neo4j/defquery required
"MATCH (a:attribute)-[:of]->(s:schema {uuid:{schema_uuid}})
WHERE a.required = 1
RETURN a
ORDER BY a.name")
(defn required! [schema-uuid]
(map :a
(neo4j/exec-query! required
{:schema_uuid schema-uuid})))

@ -12,7 +12,8 @@
(let [schema-uuid (get-in req [:params :schema])] (let [schema-uuid (get-in req [:params :schema])]
(if (form/valid? view-schema/attr-form req) (if (form/valid? view-schema/attr-form req)
(do (do
(domain/create-new! (:params req) (domain/create-new! (merge {:required 0}
(:params req))
schema-uuid schema-uuid
(get-in req [:session :uuid])) (get-in req [:session :uuid]))
(resp/redirect (path :schema-show {:uuid schema-uuid}))) (resp/redirect (path :schema-show {:uuid schema-uuid})))

@ -59,14 +59,12 @@
[:nav [:nav
(when authed? (when authed?
[:section.schemas [:section.schemas
[:h2 [:span.__icon "▤"] "Created Schemas"] [:h2 [:span.__icon "▤"] "Schemas"]
[:ul [:ul
(for [schema (:created-schemas session)] (for [schema (:schemas session)]
[:li (:name schema)])] [:li [:a {:href (path :instance-list
[:h2 [:span.__icon "▤"] "Other Schemas"] {:schema-uuid (:uuid schema)})}
[:ul (:name schema)]])]])]
(for [schema (:other-schemas session)]
[:li (:name schema)])]])]
(into [:main (into [:main
(for [msg (:flash request)] (for [msg (:flash request)]
(flash-error msg))] (flash-error msg))]

@ -11,6 +11,7 @@
[wanijo.schema.middleware :as schema-middleware] [wanijo.schema.middleware :as schema-middleware]
[wanijo.user.routes :as user-routes] [wanijo.user.routes :as user-routes]
[wanijo.attribute.routes :as attr-routes] [wanijo.attribute.routes :as attr-routes]
[wanijo.instance.routes :as instance-routes]
[wanijo.framework.auth :as auth] [wanijo.framework.auth :as auth]
[wanijo.framework.devmode :as devmode] [wanijo.framework.devmode :as devmode]
[wanijo.framework.routing :refer [path]])) [wanijo.framework.routing :refer [path]]))
@ -27,7 +28,8 @@
(routes home-routes/routes (routes home-routes/routes
schema-routes/routes schema-routes/routes
user-routes/routes user-routes/routes
attr-routes/routes)) attr-routes/routes
instance-routes/routes))
(route/not-found "Not Found")) (route/not-found "Not Found"))
(def app (def app

@ -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!"))]))

@ -96,15 +96,35 @@
-[:permission {type:{type}}]- -[:permission {type:{type}}]-
(s)) AS is_public") (s)) AS is_public")
(defn has-user-write-permissions? [schema-uuid user-uuid] (defn has-user-permission? [type schema-uuid user-uuid]
(let [permissions (first (neo4j/exec-query! schema-permissions (let [permissions (first (neo4j/exec-query! schema-permissions
{:schema_uuid schema-uuid {:schema_uuid schema-uuid
:user_uuid user-uuid :user_uuid user-uuid
:type "write"})) :type type}))
public? (neo4j/bool (:is_public permissions)) public? (neo4j/bool (:is_public permissions))
user? (neo4j/bool (:user_has_permission permissions))] user? (neo4j/bool (:user_has_permission permissions))]
(or public? user?))) (or public? user?)))
(defn has-user-write-permissions? [schema-uuid user-uuid]
(has-user-permission? "write" schema-uuid user-uuid))
(defn has-user-read-permissions? [schema-uuid user-uuid]
(has-user-permission? "read" schema-uuid user-uuid))
(neo4j/defquery
accessible-schemas
"MATCH (s:schema),
(u:user {uuid:{user_uuid}})
WHERE EXISTS((u)-[:permission {type:'read'}]->(s))
OR NOT EXISTS((:user)-[:permission {type:'read'}]->(s))
RETURN s
ORDER BY s.name")
(defn accessible-schemas! [user-uuid]
(map :s
(neo4j/exec-query! accessible-schemas
{:user_uuid user-uuid})))
(neo4j/defquery (neo4j/defquery
delete delete
"MATCH (s:schema) "MATCH (s:schema)

@ -5,12 +5,7 @@
(defn wrap-user-schemas [handler] (defn wrap-user-schemas [handler]
(fn [req] (fn [req]
(if-let [uuid (get-in req [:session :uuid])] (if-let [uuid (get-in req [:session :uuid])]
(let [created (domain/all-created-by! uuid) (handler (assoc-in req
created-uids (map :uuid created) [:session :schemas]
others (filter (fn [other] (domain/accessible-schemas! uuid)))
(not (in? created-uids (:uuid other))))
(domain/all!))]
(handler (-> req
(assoc-in [:session :created-schemas] created)
(assoc-in [:session :other-schemas] others))))
(handler req)))) (handler req))))

@ -22,12 +22,14 @@
:type {:label "Type" :type {:label "Type"
:required true :required true
:spec ::attr-domain/type :spec ::attr-domain/type
:widget :select
:options (map #(vector (capitalize %) %) :options (map #(vector (capitalize %) %)
attr-domain/types)} attr-domain/types)}
:required {:label "Required" :required {:label "Required"
:required false :required false
:spec ::attr-domain/required :spec ::attr-domain/required
:widget :checkbox} :widget :checkbox
:from-req #(if (some? %) 1 0)}
:uuid {:widget :hidden}}}) :uuid {:widget :hidden}}})
(def assign-form (def assign-form

Loading…
Cancel
Save