From 02063e8405942fbcd85654ea8c63842d5078c398 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Wed, 12 Sep 2018 09:54:27 +0200 Subject: [PATCH] editing schemas, set created_at on attributes --- src/wanijo/attribute/domain.clj | 9 +++++++-- src/wanijo/framework/form.clj | 2 +- src/wanijo/framework/time.clj | 11 +++++------ src/wanijo/schema/domain.clj | 11 +++++++++++ src/wanijo/schema/routes.clj | 28 +++++++++++++++++++++------- src/wanijo/schema/view.clj | 20 ++++++++++++-------- 6 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/wanijo/attribute/domain.clj b/src/wanijo/attribute/domain.clj index 53fb2e7..1f98ab6 100644 --- a/src/wanijo/attribute/domain.clj +++ b/src/wanijo/attribute/domain.clj @@ -13,6 +13,9 @@ (spec/def ::required #(some (partial = %) ["on" nil 0 1 "0" "1"])) +(spec/def ::created-at + (spec/and string? ::neo4j/date-str)) + (neo4j/defquery findy-by-schema "MATCH (a:attribute)-->(s:schema) @@ -37,7 +40,8 @@ SET a.type = {type} SET a.name = {name} 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] (neo4j/exec-query! @@ -46,7 +50,8 @@ (assoc :user_uuid user-uuid :schema_uuid schema-uuid - :attribute_uuid (neo4j/uuid)) + :attribute_uuid (neo4j/uuid) + :created_at (neo4j/now-str)) (update :required #(if (some? %) 1 0))))) (neo4j/defquery edit diff --git a/src/wanijo/framework/form.clj b/src/wanijo/framework/form.clj index a87cd29..65aadfd 100644 --- a/src/wanijo/framework/form.clj +++ b/src/wanijo/framework/form.clj @@ -117,7 +117,7 @@ :textarea render-textarea :select render-select}) -(defn render-form [form-def values req] +(defn render-widgets [form-def values req] {:pre [(spec/valid? ::form form-def)]} (let [form-hash (str (hash [form-def values])) submitted-hash (get-in req [:params :__form-hash]) diff --git a/src/wanijo/framework/time.clj b/src/wanijo/framework/time.clj index 7b42617..0667f55 100644 --- a/src/wanijo/framework/time.clj +++ b/src/wanijo/framework/time.clj @@ -2,9 +2,8 @@ (:require [clj-time.format :as format])) (defn prettify-dt [date-str] - (->> - date-str - (format/parse - (format/formatters :basic-date-time)) - (format/unparse - (format/formatter "yyyy-MM-dd HH:mm:ss")))) + (some->> date-str + (format/parse + (format/formatters :basic-date-time)) + (format/unparse + (format/formatter "yyyy-MM-dd HH:mm:ss")))) diff --git a/src/wanijo/schema/domain.clj b/src/wanijo/schema/domain.clj index 824c5f9..55c1191 100644 --- a/src/wanijo/schema/domain.clj +++ b/src/wanijo/schema/domain.clj @@ -89,3 +89,14 @@ (neo4j/exec-query! delete {:uuid uuid})) + +(neo4j/defquery + edit + "MATCH (s:schema) + WHERE s.uuid = {uuid} + SET s.name = {name}") + +(defn edit! [schema] + (neo4j/exec-query! + edit + schema)) diff --git a/src/wanijo/schema/routes.clj b/src/wanijo/schema/routes.clj index 3b53051..89d18ed 100644 --- a/src/wanijo/schema/routes.clj +++ b/src/wanijo/schema/routes.clj @@ -3,25 +3,36 @@ [ring.util.response :as resp] [wanijo.framework.view :as view] [wanijo.framework.form :as form] - [wanijo.framework.routing :refer [register!]] + [wanijo.framework.routing :refer [register! path]] [wanijo.schema.domain :as domain] [wanijo.schema.view :as view-schema] - [wanijo.attribute.domain :as attr-domain])) + [wanijo.attribute.domain :as domain-attr])) (defn new! [req] - (if (form/valid? view-schema/new-form req) + (if (form/valid? view-schema/form req) (do (domain/create-new! - (get-in req [:params :schema-name]) + (get-in req [:params :name]) (get-in req [:session :uuid])) - (resp/redirect "/schema")) + (resp/redirect (path :schema-overview))) (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] (if (domain/can-user-modify? uuid (:uuid session)) (do (domain/delete! uuid) - (resp/redirect "/schema")) + (resp/redirect (path :schema-overview))) {:status 403})) (defroutes routes @@ -32,11 +43,14 @@ [uuid :as req] (view-schema/show-schema! (domain/find-by-uuid! uuid) - (attr-domain/find-by-schema! uuid) + (domain-attr/find-by-schema! uuid) req)) (POST (register! :schema-new "/schema/new") [] new!) + (POST (register! :schema-edit "/schema/edit") + [] + edit!) (DELETE (register! :schema-delete "/schema/:uuid") [uuid :as req] (delete-schema! uuid (:session req)))) diff --git a/src/wanijo/schema/view.clj b/src/wanijo/schema/view.clj index 981c5ec..9839cc8 100644 --- a/src/wanijo/schema/view.clj +++ b/src/wanijo/schema/view.clj @@ -8,8 +8,8 @@ [wanijo.schema.domain :as domain] [wanijo.attribute.domain :as attr-domain])) -(def new-form - {:fields {:schema-name {:label "Name" +(def form + {:fields {:name {:label "Name" :required true :spec ::domain/name}}}) @@ -52,8 +52,7 @@ (prettify-dt (:created_at schema))]])]] [:h1 "New schema"] (hform/form-to [:post (path :schema-new)] - (anti-forgery-field) - (form/field new-form :schema-name req) + (form/render-form form {} req) (hform/submit-button "Create"))]))) (defn show-schema! [schema attrs req] @@ -62,22 +61,27 @@ :content [[:h1 "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"] [:ul.schema-attributes (for [attr attrs] [:li (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/submit-button "Save")) (hform/form-to [:delete (path :attribute-delete attr)] (anti-forgery-field) (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"] (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/submit-button "Save")) [:h2 "Actions"]