editing schemas, set created_at on attributes

integration-tests
Josha von Gizycki 6 years ago
parent ee21f5f7c1
commit 02063e8405

@ -13,6 +13,9 @@
(spec/def ::required (spec/def ::required
#(some (partial = %) ["on" nil 0 1 "0" "1"])) #(some (partial = %) ["on" nil 0 1 "0" "1"]))
(spec/def ::created-at
(spec/and string? ::neo4j/date-str))
(neo4j/defquery (neo4j/defquery
findy-by-schema findy-by-schema
"MATCH (a:attribute)-->(s:schema) "MATCH (a:attribute)-->(s:schema)
@ -37,7 +40,8 @@
SET a.type = {type} SET a.type = {type}
SET a.name = {name} SET a.name = {name}
SET a.uuid = {attribute_uuid} SET a.uuid = {attribute_uuid}
SET a.required = {required}") SET a.required = {required}
SET a.created_at = {created_at}")
(defn create-new! [attr schema-uuid user-uuid] (defn create-new! [attr schema-uuid user-uuid]
(neo4j/exec-query! (neo4j/exec-query!
@ -46,7 +50,8 @@
(assoc (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))
(update :required #(if (some? %) 1 0))))) (update :required #(if (some? %) 1 0)))))
(neo4j/defquery edit (neo4j/defquery edit

@ -117,7 +117,7 @@
:textarea render-textarea :textarea render-textarea
:select render-select}) :select render-select})
(defn render-form [form-def values req] (defn render-widgets [form-def values req]
{:pre [(spec/valid? ::form form-def)]} {:pre [(spec/valid? ::form form-def)]}
(let [form-hash (str (hash [form-def values])) (let [form-hash (str (hash [form-def values]))
submitted-hash (get-in req [:params :__form-hash]) submitted-hash (get-in req [:params :__form-hash])

@ -2,8 +2,7 @@
(:require [clj-time.format :as format])) (:require [clj-time.format :as format]))
(defn prettify-dt [date-str] (defn prettify-dt [date-str]
(->> (some->> date-str
date-str
(format/parse (format/parse
(format/formatters :basic-date-time)) (format/formatters :basic-date-time))
(format/unparse (format/unparse

@ -89,3 +89,14 @@
(neo4j/exec-query! (neo4j/exec-query!
delete delete
{:uuid uuid})) {:uuid uuid}))
(neo4j/defquery
edit
"MATCH (s:schema)
WHERE s.uuid = {uuid}
SET s.name = {name}")
(defn edit! [schema]
(neo4j/exec-query!
edit
schema))

@ -3,25 +3,36 @@
[ring.util.response :as resp] [ring.util.response :as resp]
[wanijo.framework.view :as view] [wanijo.framework.view :as view]
[wanijo.framework.form :as form] [wanijo.framework.form :as form]
[wanijo.framework.routing :refer [register!]] [wanijo.framework.routing :refer [register! path]]
[wanijo.schema.domain :as domain] [wanijo.schema.domain :as domain]
[wanijo.schema.view :as view-schema] [wanijo.schema.view :as view-schema]
[wanijo.attribute.domain :as attr-domain])) [wanijo.attribute.domain :as domain-attr]))
(defn new! [req] (defn new! [req]
(if (form/valid? view-schema/new-form req) (if (form/valid? view-schema/form req)
(do (do
(domain/create-new! (domain/create-new!
(get-in req [:params :schema-name]) (get-in req [:params :name])
(get-in req [:session :uuid])) (get-in req [:session :uuid]))
(resp/redirect "/schema")) (resp/redirect (path :schema-overview)))
(view-schema/overview! req))) (view-schema/overview! req)))
(defn edit! [req]
(let [uuid (get-in req [:params :uuid])]
(if (form/valid? view-schema/form req)
(do
(domain/edit! (:params req))
(resp/redirect (path :schema-show (:params req))))
(view-schema/show-schema!
(domain/find-by-uuid! uuid)
(domain-attr/find-by-schema! uuid)
req))))
(defn delete-schema! [uuid session] (defn delete-schema! [uuid session]
(if (domain/can-user-modify? uuid (:uuid session)) (if (domain/can-user-modify? uuid (:uuid session))
(do (do
(domain/delete! uuid) (domain/delete! uuid)
(resp/redirect "/schema")) (resp/redirect (path :schema-overview)))
{:status 403})) {:status 403}))
(defroutes routes (defroutes routes
@ -32,11 +43,14 @@
[uuid :as req] [uuid :as req]
(view-schema/show-schema! (view-schema/show-schema!
(domain/find-by-uuid! uuid) (domain/find-by-uuid! uuid)
(attr-domain/find-by-schema! uuid) (domain-attr/find-by-schema! uuid)
req)) req))
(POST (register! :schema-new "/schema/new") (POST (register! :schema-new "/schema/new")
[] []
new!) new!)
(POST (register! :schema-edit "/schema/edit")
[]
edit!)
(DELETE (register! :schema-delete "/schema/:uuid") (DELETE (register! :schema-delete "/schema/:uuid")
[uuid :as req] [uuid :as req]
(delete-schema! uuid (:session req)))) (delete-schema! uuid (:session req))))

@ -8,8 +8,8 @@
[wanijo.schema.domain :as domain] [wanijo.schema.domain :as domain]
[wanijo.attribute.domain :as attr-domain])) [wanijo.attribute.domain :as attr-domain]))
(def new-form (def form
{:fields {:schema-name {:label "Name" {:fields {:name {:label "Name"
:required true :required true
:spec ::domain/name}}}) :spec ::domain/name}}})
@ -52,8 +52,7 @@
(prettify-dt (:created_at schema))]])]] (prettify-dt (:created_at schema))]])]]
[:h1 "New schema"] [:h1 "New schema"]
(hform/form-to [:post (path :schema-new)] (hform/form-to [:post (path :schema-new)]
(anti-forgery-field) (form/render-form form {} req)
(form/field new-form :schema-name req)
(hform/submit-button "Create"))]))) (hform/submit-button "Create"))])))
(defn show-schema! [schema attrs req] (defn show-schema! [schema attrs req]
@ -62,22 +61,27 @@
:content :content
[[:h1 "Schema " [[:h1 "Schema "
[:span.schema-title__name (:name schema)]] [:span.schema-title__name (:name schema)]]
(hform/form-to [:post "/schema/edit"]) [:h2 "Edit"]
(hform/form-to [:post (path :schema-edit)]
(form/render-widgets form schema req)
(hform/hidden-field "uuid" (:uuid schema))
(hform/submit-button "Save"))
[:h2 "Attributes"] [:h2 "Attributes"]
[:ul.schema-attributes [:ul.schema-attributes
(for [attr attrs] (for [attr attrs]
[:li [:li
(hform/form-to [:post (path :attribute-edit {:schema (:uuid schema)})] (hform/form-to [:post (path :attribute-edit {:schema (:uuid schema)})]
(form/render-form attr-form attr req) (form/render-widgets attr-form attr req)
(hform/hidden-field "uuid" (:uuid attr)) (hform/hidden-field "uuid" (:uuid attr))
(hform/submit-button "Save")) (hform/submit-button "Save"))
(hform/form-to [:delete (path :attribute-delete attr)] (hform/form-to [:delete (path :attribute-delete attr)]
(anti-forgery-field) (anti-forgery-field)
(hform/hidden-field "schema" (:uuid schema)) (hform/hidden-field "schema" (:uuid schema))
(hform/submit-button "Delete!"))])] (hform/submit-button "Delete!")
[:label "Created at " [:em (prettify-dt (:created_at attr))]])])]
[:h3 "New attribute"] [:h3 "New attribute"]
(hform/form-to [:post (path :attribute-new)] (hform/form-to [:post (path :attribute-new)]
(form/render-form attr-form {} req) (form/render-widgets attr-form {} req)
(hform/hidden-field "schema" (:uuid schema)) (hform/hidden-field "schema" (:uuid schema))
(hform/submit-button "Save")) (hform/submit-button "Save"))
[:h2 "Actions"] [:h2 "Actions"]

Loading…
Cancel
Save