own namespace for instance forms

integration-tests
Josha von Gizycki 6 years ago
parent 33bda110be
commit 29228853dc

@ -0,0 +1,39 @@
(ns wanijo.instance.forms
(:require [wanijo.instance.domain :as domain]
[wanijo.attribute.domain :as domain-attr]))
(def form
{:fields {:name {:label "Name"
:required true
:spec ::domain/name}}})
(defn attr-type->widget [attr-type]
(case attr-type
("markdown" "text") :textarea
:input))
(defn attr->field-id [attr]
(keyword (str "attr-" (:uuid attr))))
(defn attr->field [attr]
{:label (:name attr)
:required true
:widget (attr-type->widget (:type attr))})
(defn instance-form [schema-uuid]
(update form
:fields
(fn [fields]
(reduce (fn [fields attr]
(assoc fields
(attr->field-id attr)
(attr->field attr)))
fields
(domain-attr/required! schema-uuid)))))
(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)})

@ -2,56 +2,29 @@
(:require [compojure.core :refer [defroutes GET POST DELETE]] (:require [compojure.core :refer [defroutes GET POST DELETE]]
[ring.util.response :as resp] [ring.util.response :as resp]
[formulare.core :as form] [formulare.core :as form]
[wanijo.instance.view :as view] [wanijo.instance
[wanijo.instance.domain :as domain] [view :as view]
[domain :as domain]
[forms :as forms-inst]]
[wanijo.framework.routing :refer [register! path]] [wanijo.framework.routing :refer [register! path]]
[wanijo.schema.domain :as domain-schema] [wanijo.schema.domain :as domain-schema]
[wanijo.attribute.domain :as domain-attr])) [wanijo.attribute.domain :as domain-attr]))
(defn attr-type->widget [attr-type]
(case attr-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] (defn list! [schema-uuid req]
(view/list! (domain-schema/find-by-uuid! schema-uuid) (view/list! (domain-schema/find-by-uuid! schema-uuid)
(domain/find-by-schema! schema-uuid) (domain/find-by-schema! schema-uuid)
(forms-inst/instance-form schema-uuid)
(new-form schema-uuid) (new-form schema-uuid)
req)) 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] (defn new! [req]
(let [schema-uuid (get-in req [:params :schema-uuid]) (let [schema-uuid (get-in req [:params :schema-uuid])
form-def (new-form schema-uuid)] form-def (new-form schema-uuid)]
(if (form/valid? form-def req) (if (form/valid? form-def req)
(let [instance (form-data->instance (let [form-data (form/form-data form-def req)
(form/form-data form-def req) req-attrs (domain-attr/required! schema-uuid)
(domain-attr/required! schema-uuid))] instance (forms-inst/form-data->instance form-data
req-attrs)]
(domain/create! schema-uuid (domain/create! schema-uuid
instance) instance)
(resp/redirect (path :instance-list (resp/redirect (path :instance-list

@ -7,11 +7,6 @@
[wanijo.framework.routing :refer [path]] [wanijo.framework.routing :refer [path]]
[wanijo.framework.time :refer [prettify-dt]])) [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] (defn list! [schema instances new-form req]
(view/layout! (view/layout!
:request req :request req

Loading…
Cancel
Save