funcool / catacumba

Asynchronous web toolkit for clojure built on top of Ratpack / Netty
https://funcool.github.io/catacumba/latest/
BSD 2-Clause "Simplified" License
192 stars 26 forks source link

Add restfull handlers support. #6

Closed niwinz closed 9 years ago

niwinz commented 9 years ago

The proposal is do something like this:

;; Using protocols

(def myresource
  (reify IResource
    IResourceIndex
    (-index [context]
      (http/ok "foobar"))

    IResourceShow
    (-show [context]
      (http/ok "foobar"))

    IResourceCreate
    (-create [context]
      (http/created "foobar"))

    IResourceUpdate
    (-update [context]
      (http/ok "foobar"))

    IResourceDelete
    (-delete [context]
      (http/ok "foobar"))))

;; Using multimethods

(defmulti myresource
  (fn [type context] type))

(defmethod myresource :index
  [_ context]
  (http/ok "foobar"))

(defmethod myresource :show
  [_ context]
  (http/ok "foobar"))

(defmethod myresource :create
  [_ context]
  (http/ok "foobar"))

(defmethod myresource :update
  [_ context]
  (http/ok "foobar"))

(defmethod myresource :delete
  [_ context]
  (http/ok "foobar"))

;; Using plain functions and hash-maps

(defn index
  [contex]
  (http/ok "foobar"))

(defn show
  [contex]
  (http/ok "foobar"))

(defn create
  [contex]
  (http/ok "foobar"))

(defn update
  [contex]
  (http/ok "foobar"))

(defn delete
  [contex]
  (http/ok "foobar"))

(def myresource
  {:index index
   :show show
   :create create
   :update update
   :delete delete})

And it will be used in this way:

(require '[catacumba.handlers.restfull :as rest])
(ct/routes [[:prefix "api/v1"
             (rest/resource "authors" myauthorresource)
             (rest/resource "books" mybooksresource)]])
niwinz commented 9 years ago

Already implemented in master ;)