diff --git a/src/wanijo/instance/domain.clj b/src/wanijo/instance/domain.clj index 2286626..5e0d5cd 100644 --- a/src/wanijo/instance/domain.clj +++ b/src/wanijo/instance/domain.clj @@ -76,3 +76,14 @@ (:a %)) (neo4j/exec-query! find-properties {:uuid uuid}))) + +(neo4j/defquery edit-instance + "MATCH (i:instance {uuid:{uuid}}) + SET i.name = {name}, + i.updated_at = {updated_at}") + +(defn edit! [instance] + (neo4j/exec-query! edit-instance + {:uuid (:uuid instance) + :name (:name instance) + :updated_at (neo4j/now-str)})) diff --git a/src/wanijo/instance/forms.clj b/src/wanijo/instance/forms.clj index 5bae277..b14a66e 100644 --- a/src/wanijo/instance/forms.clj +++ b/src/wanijo/instance/forms.clj @@ -17,7 +17,7 @@ (defn attr->field [attr] {:label (:name attr) - :required true + :required (= 1 (:required attr)) :widget (attr-type->widget (:type attr))}) (defn with-attributes [attrs] diff --git a/src/wanijo/instance/routes.clj b/src/wanijo/instance/routes.clj index 8fcce36..7ad755e 100644 --- a/src/wanijo/instance/routes.clj +++ b/src/wanijo/instance/routes.clj @@ -1,13 +1,16 @@ (ns wanijo.instance.routes - (:require [compojure.core :refer [defroutes GET POST DELETE]] + (:require [compojure.core :refer [defroutes wrap-routes + GET POST DELETE]] [ring.util.response :as resp] [formulare.core :as form] [wanijo.instance [view :as view] [domain :as domain] [forms :as forms-inst]] + [wanijo.schema + [domain :as domain-schema] + [middleware :as middleware-schema]] [wanijo.framework.routing :refer [register! path]] - [wanijo.schema.domain :as domain-schema] [wanijo.attribute.domain :as domain-attr])) (defn list! [schema-uuid req] @@ -31,22 +34,46 @@ (:params req)))) (list! schema-uuid req)))) +(defn form! [uuid] + (forms-inst/with-attributes + (domain-attr/find-by-instance! uuid))) + +(defn instance! [uuid] + (assoc (domain/find-by-uuid! uuid) + :properties + (domain/find-properties! uuid))) + (defn show! [uuid req] - (let [instance (assoc (domain/find-by-uuid! uuid) - :properties - (domain/find-properties! uuid)) + (let [instance (instance! uuid) attrs (domain-attr/find-by-instance! uuid)] (view/show! instance - (forms-inst/with-attributes attrs) + (form! uuid) (forms-inst/instance->form-data instance) req))) +(defn edit! [uuid req] + (let [form-def (form! uuid) + instance (instance! uuid)] + (if (form/valid? form-def req) + (let [form-data (form/form-data form-def req) + attrs (domain-attr/find-by-instance! uuid) + form-instance (forms-inst/form-data->instance form-data attrs) + instance (assoc form-instance :uuid uuid)] + (domain/edit! instance) + (resp/redirect (path :instance-show instance))) + (show! uuid req)))) + (defroutes routes - (GET (register! :instance-list "/instance/list/:schema-uuid") - [schema-uuid :as req] - (list! schema-uuid req)) + (wrap-routes (GET (register! :instance-list "/instance/list/:schema-uuid") + [schema-uuid :as req] + (list! schema-uuid req)) + (middleware-schema/wrap-allowed-to-read + #(get-in % [:params :schema-uuid]))) (POST (register! :instance-new "/instance/new") [] new!) (GET (register! :instance-show "/instance/:uuid") [uuid :as req] - (show! uuid req))) + (show! uuid req)) + (POST (register! :instance-edit "/instance/:uuid") + [uuid :as req] + (edit! uuid req))) diff --git a/src/wanijo/instance/view.clj b/src/wanijo/instance/view.clj index 00e1bfe..26f7f3c 100644 --- a/src/wanijo/instance/view.clj +++ b/src/wanijo/instance/view.clj @@ -39,5 +39,6 @@ :request req :content [[:h1 (:name instance)] - (hform/form-to [:post ""] - (form/render-widgets form form-data req))])) + (hform/form-to [:post (path :instance-edit instance)] + (form/render-widgets form form-data req) + (hform/submit-button "Create!"))])) diff --git a/src/wanijo/schema/middleware.clj b/src/wanijo/schema/middleware.clj index e61ffd9..093132f 100644 --- a/src/wanijo/schema/middleware.clj +++ b/src/wanijo/schema/middleware.clj @@ -25,12 +25,17 @@ (defn wrap-allowed-to-write [] (write-permission-middleware #(get-in % [:params :uuid]))) -(defn wrap-allowed-to-read [schema-fn] - (fn [handler] - (fn [req] - (let [uuid (schema-fn req) - check-fn domain/has-user-read-permissions?] - (if (check-fn uuid (get-in req [:session :uuid])) - (handler req) - (assoc (resp/redirect (path :schema-overview)) - :flash ["No read permission for schema"])))))) +(defn wrap-allowed-to-read + ([schema-fn] + (wrap-allowed-to-read schema-fn + (fn [_] + (assoc (resp/redirect (path :schema-overview)) + :flash ["No read permission for schema"])))) + ([schema-fn not-allowed-fn] + (fn [handler] + (fn [req] + (let [uuid (schema-fn req) + check-fn domain/has-user-read-permissions?] + (if (check-fn uuid (get-in req [:session :uuid])) + (handler req) + (not-allowed-fn req))))))) diff --git a/src/wanijo/schema/routes.clj b/src/wanijo/schema/routes.clj index 541513a..b746f63 100644 --- a/src/wanijo/schema/routes.clj +++ b/src/wanijo/schema/routes.clj @@ -2,13 +2,14 @@ (:require [compojure.core :refer [defroutes GET POST DELETE] :as comp] [ring.util.response :as resp] [formulare.core :as form] - [wanijo.schema.middleware :as mw] [wanijo.framework.view :as view] [wanijo.framework.routing :refer [register! path]] - [wanijo.schema.domain :as domain] [wanijo.user.domain :as domain-user] - [wanijo.schema.view :as schema-view] - [wanijo.schema.forms :as schema-forms] + [wanijo.schema + [view :as schema-view] + [forms :as schema-forms] + [domain :as domain] + [middleware :as middleware]] [wanijo.attribute.domain :as domain-attr])) (defn new! [req] @@ -86,7 +87,7 @@ (POST (register! :schema-new "/schema/new") [] new!) (comp/wrap-routes read-routes - (mw/wrap-allowed-to-read + (middleware/wrap-allowed-to-read #(get-in % [:route-params :uuid]))) (comp/wrap-routes write-routes - (mw/wrap-allowed-to-write))) + (middleware/wrap-allowed-to-write)))