1

liberator と monger を使用して、単純な REST API を構築しようとしています。

(ns tm.place.endpoint
  (:require [liberator.core :refer [defresource]]
            [monger [collection :as mc] [query :as mq]]
            [clojure.data [json :as json]]
            monger.json))

(defresource entity-place-resource [id]
  :available-media-types ["application/json"]
  :allowed-methods [:get :put :delete]
  :exists? (if-let [place (mc/find-map-by-id "place" id)] {:place place})
  :handle-ok :place
;  :handle-created (fn [ctx] 
;                     (let [body (-> ctx :request :body)]
;                       (assoc body :_id id)))
  :handle-not-found []
  :handle-not-acceptable "Should accept application/json"
  :handle-method-not-allowed (json/write-str 
                              {:message "Can only handle get, put and delete"})
;  :handle-not-implemented
;  :handle-no-content
  :put! (fn [ctx] 
          (let [body (-> ctx :request :body)]
            (mc/update-by-id "place" id (assoc body :_id id))))
  :delete! (mc/remove-by-id "place" id)
  :can-put-to-missing? false)

私はadvanced rest clientそれが機能するかどうかを確認するために使用しています。メソッドが:getまたはの場合:delete、それは私が望むことを完全に行います (最初にドキュメントが存在するかどうかを確認し、次に適切なアクションを実行します)。ただし、メソッドが の場合、:put作成された http 201 を吐き出すだけで、リクエストは成功したと思いますが、対応するドキュメントは削除され、更新されません。

:delete! をコメントアウトすると、行、:put! 期待どおりに動作するので、犯人は行だと推測していますが、メソッドを使用しているため、:delete! を想定して:delete!いるため、その理由はわかりません。:put手付かずのままでなければなりません。理由はありますか?

4

1 に答える 1