Clojure幽灵合并导航值与其他地图

时间:2018-04-18 12:34:37

标签: clojure specter

我尝试使用Clojure幽灵编辑来自"数据库"的订单信息。此应用程序基于Rest API。

我的API调用:

(PUT "/orders" []
         :return :specs.models.order/orderSpec
         :body-params [orderid :- :specs.models.order/orderid
                       {amount :- :specs.models.order/amount (orderController/getAmount orderid)}
                       {description :- :specs.models.order/description (orderController/getDescription orderid)}
                       {productid :- :specs.models.order/productid (orderController/getProductid orderid)}]
         :summary "Edits the description and/or amount and/or productid of an order"
         (getResponseFromContent (orderController/editOrder orderid amount description productid))
         )

这是我的"数据库":

      [{:orderid 0 :productid 0 :description "A" :amount 2 :state "active"}
        {:orderid 1 :productid 1 :description "A" :amount 2 :state "active"}]

它位于不同的文件中,我用它在模型中调用它

(def filename "resources/mockorderDatabase.dat")
(def database (atom (try
                      (clojure.edn/read-string (slurp filename))
                      (catch Exception e []))))

这是我的控制者:

(defn editOrder
  "Edit order's description and/or amount and/or productid by orderid"
  [orderid amount description productid]
  (if-let [editedorder (order/edit-order db/config {:orderid orderid :amount amount :description description :productid productid})]
    (first editedorder)
    nil))

我被edit-order困住了。我该怎么做呢?

到目前为止,我提出了edit-order

(defn edit-order                                           
  [db orderid amount description productid]
  (->> (transform [ALL (comp (partial = orderid) :orderid)]
                  #(assoc % :productid productid :amount amount :description description)
                  @database
       )
   )
  )

我得到的回应是:

{
  "type": "unknown-exception",
  "class": "java.lang.IllegalArgumentException"
}

如果我使用setval代替transfrom会更好吗?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

你真的不需要Spectre:

(def data
  [{:orderid 0 :productid 0 :description "A" :amount 2 :state "active"}
   {:orderid 1 :productid 1 :description "A" :amount 2 :state "active"}])

(defn update-by-orderid [orders orderid description amount productid]
  (vec
    (for [order orders]
      (if (= orderid (:orderid order))
        (assoc order          ; change if match
          :description description
          :amount amount
          :productid productid)
        order))))                 ; return unchanged if no match


(update-by-orderid data 0 "DESC" 99 123) =>
[{:orderid 0,
  :productid 123,
  :description "DESC",
  :amount 99,
  :state "active"}
 {:orderid 1,
  :productid 1,
  :description "A",
  :amount 2,
  :state "active"}]

答案 1 :(得分:0)

(def filename "mockorderDatabase.dat")
(def database (atom (try
                        (clojure.edn/read-string (slurp filename))
                        (catch Exception e []))))

(require '[com.rpl.specter :refer :all])

(defn change-order [*database id productid amount desc]
    (transform [ATOM ALL (comp (partial = id) :orderid)]
               #(assoc % :productid productid :amount amount :description desc)
               *database))

(prn (change-order database 0 111 222 "XXXX-YYYY"))