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]]
[ring.util.response :as resp]
[formulare.core :as form]
[wanijo.instance.view :as view]
[wanijo.instance.domain :as domain]
[wanijo.instance
[view :as view]
[domain :as domain]
[forms :as forms-inst]]
[wanijo.framework.routing :refer [register! path]]
[wanijo.schema.domain :as domain-schema]
[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]
(view/list! (domain-schema/find-by-uuid! schema-uuid)
(domain/find-by-schema! schema-uuid)
(forms-inst/instance-form 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))]
(let [form-data (form/form-data form-def req)
req-attrs (domain-attr/required! schema-uuid)
instance (forms-inst/form-data->instance form-data
req-attrs)]
(domain/create! schema-uuid
instance)
(resp/redirect (path :instance-list

@ -7,11 +7,6 @@
[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

Loading…
Cancel
Save