簡體   English   中英

如何在 clojure 中發送帶有 Id 的沖突響應

[英]How can I send a Conflict response with an Id in clojure

我定義了一個方法,我只是簡單地檢查具有特定名稱和狀態的作業存在於何處,以防作業存在我想發送沖突響應但帶有 id

(defn insert-job [name status req]
      (if (->> {:job-name name :status status}
               db/insert-job
               :amount
               pos? )
        (conflict)  ; here I want to send a response as conflicts with a particular id as Long
        (insert-job req)))

正如在下面的方法中,我能夠產生一個created的響應作為 Long

(defn insert-job [req]
  (let [[errors job] (v/validate-job (:body req))]
    (if errors
      (unprocessable-entity {:errors errors})
      (let [id (db/insert-job job)]
        (created (format "/jobs/%d" id) {:id id})))))

好的。 這會很容易

  1. 更新您的查詢以查找工作 ID(如果存在)
  2. 更新您的代碼以實際拋出它。

在你的 hugsql 查詢文件中:

-- :name find-job-id :? :1
select id from job_history 
where name = :name and status = :status 
limit 1;

在你的倉庫中:

(defn insert-job [name status rec]
  (if-let [id (some->> {:name name :status status}
                       (find-job-id db-spec)
                       :id)]
    (ring.util.http-response/conflict! 
      {:message "record already exists"
       :id id})

    ;; here do whatever you need for actual insertion
    ))

但是沒問題。 我強烈建議您閱讀有關您選擇的語言和圖書館的一些東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM