/------- Other system 1
om.next /
Browser <--------- Server -------- Other system 2
\
\------ Other system 3
(def handler (yada data))
{:description "Phonebook index"
:properties {…}
:methods {:get {…}
:post {…}}
{:get
{:parameters {:query {(s/optional-key :q) String}}
:produces [{:media-type #{"text/html" "application/json;q=0.9"}
:charset "UTF-8"}]
:handler
(fn [ctx]
(let [q (get-in ctx [:parameters :query :q])
entries (if q
(db/search-entries db q)
(db/get-entries db))]
(case (get-in ctx [:response :representation :media-type :name])
"text/html" (html/index-html entries @*routes q)
entries)))}}
{:post
{:parameters
{:form {:surname String :firstname String :phone [String]}}
:consumes
[{:media-type "application/x-www-form-urlencoded"
:charset "UTF-8"}]
:handler
(fn [ctx]
(let [id (db/add-entry db (get-in ctx [:parameters :form]))]
(-> (:response ctx)
(assoc :status 303)
(update :headers merge
{"location"
(bidi/path-for @*routes ::entry :entry id)}))))}}
{:description "Phonebook entry"
:parameters {:path {:entry Long}}
:properties (fn [ctx] {:last-modified …
:version …})
:produces {:media-type #{"text/html"
"application/json;q=0.8"}
:charset "UTF-8"}
:methods {:get {…}
:put {…}
:delete {…}}}
{:get
{:handler
(fn [ctx]
(when-let [entry (db/get-entry db
(get-in ctx [:parameters :path :entry]))]
(case (get-in ctx [:response :representation :media-type :name])
"text/html" (html/entry-html entry)
entry)))}}
{:delete
{:handler
(fn [ctx]
(let [id (get-in ctx [:parameters :path :entry])]
(db/delete-entry db id)))}}
[:button {:onclick (format "phonebook.update('%s')" entry)} "Update"]
update: function(url) {
x = new XMLHttpRequest()
x.open("PUT", url)
// FormData is built-in, sends multipart/form-data
x.send(new FormData(document.getElementById("entry")))
}
{:put
{:consumes [{:media-type #{"multipart/form-data"}}]
:parameters
{:form {:surname String
:firstname String
:phone [String]
:photo java.io.File}}
:handler
(fn [ctx]
(let [entry (get-in ctx [:parameters :path :entry])
form (get-in ctx [:parameters :form])]
(db/update-entry db entry form)))}}