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

(defn edit-order
  [param-data]

(swap! database
             (fn [old-orders]
                 (mapv (fn [order]
                           (if (= (:orderid order)
                                  (:orderid param-data))
                             (assoc-in database [:state] "deleted") 
                             order))
                       old-orders)))
                     )


 (edit-order 0)

I want to replace the "active" to "deleted" after I give the Order ID. I tried with assoc-in, but it doesn't work. Thanks for helping.

2
You are calling the function with the order-id directly (0) but in your if there you access param-data like a map to get the :order-id from it.cfrick
So, how should I do it, if I want to call directly by order-id (int)?user7697691

2 Answers

0
votes

There are two problems in your code. First, in your if, you are comparing the :orderid of an order entry with an :orderid of the param-data, which doesn't make sense because param-data a number and not a map. What you want to do is compare the :orderid of your order entry with the value of param-data, like this:

(if (= (:orderid order)
        param-data)

Next, it seems that you're a bit confused about what you're doing in your mapping function. In the else-branch of your if, you return an order entry, which is correct since you're mapping over the vector of all orders. But in the other branch (the "then" branch), you're trying to use assoc-in on database, which is not want you want to do, since assoc-in will return an updated version of your full order list. I think what you want to do instead is just use assoc on the order parameter, like this:

(assoc order :state "deleted")

Which makes the full function look like this:

(defn edit-order
  [param-data]
  (swap! database
    (fn [old-orders]
       (mapv (fn [order]
                 (if (= (:orderid order)
                        param-data)
                   (assoc order :state "deleted") 
                   order))
             old-orders))))
0
votes

Your code as it is right now has two issues:

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

(defn edit-order
  [param-data]

(swap! database
            (fn [old-orders]
                (mapv (fn [order]
                          (if (= (:orderid order)
                                  (:orderid param-data))   ;; Issue 1
                            (assoc-in database [:state] "deleted") ;; Issue 2
                            order))
                      old-orders)))
                    )


(edit-order 0)

Issue 1 stems from how you call edit-order -- there is no :orderid key there, it's just a number.

Issue 2 is that you are running assoc-in on Var that holds an Atom, while you're modifying that Atom already within swap!, iterating on orders stored there. Working code with both issues resolved:

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


(defn edit-order
  [param-data]
  (swap! database
        (fn [old-orders]
          (mapv (fn [order]
                  (if (= (:orderid order) param-data)
                    (assoc order :state "deleted")
                    order))
                old-orders))))


(edit-order 0)