Home > Enterprise >  How can I send a Conflict response with an Id in closure
How can I send a Conflict response with an Id in closure

Time:02-02

I've defined a method where I'm simply checking where a job exists with a specific name and status, in case if job exists I want to send a conflict response but with an 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)))

As in the below method I am able to produce a created response as 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})))))

CodePudding user response:

ok. this would be as easy as

  1. update your query to find the job id if exists
  2. update your code to actually throw it.

in your hugsql queries file:

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

in your repo:

(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
    ))

but ok. i would heavily advice you to read something on your chosen language and libraries.

  • Related