preperatory refactor of update-collection!
will need to use it in bulk operations, so split out the core per-card collection updating logic
This commit is contained in:
parent
710c4bbba1
commit
2cf7a11c7b
|
@ -1,48 +1,56 @@
|
|||
(ns mtgcoll.models.collection
|
||||
(:require
|
||||
[clojure.java.jdbc :as jdbc]
|
||||
[views.sql.core :refer [vexec! with-view-transaction]]
|
||||
[views.core :as views]
|
||||
[views.sql.core :as vsql :refer [vexec! with-view-transaction]]
|
||||
[mtgcoll.db :refer [db]]
|
||||
[mtgcoll.views.core :refer [view-system]]))
|
||||
|
||||
(defn- update-collection!*
|
||||
[db card-id quality foil? list-id quantity-change]
|
||||
;; written assuming postgresql server is _not_ 9.5+ (so, without access to UPSERT functionality)
|
||||
(let [num-updates (first
|
||||
; i love that SQL forces you to use "is null" ...
|
||||
(if (nil? quality)
|
||||
(jdbc/execute!
|
||||
db
|
||||
["update collection
|
||||
set quantity = quantity + ?
|
||||
where card_id = ? and
|
||||
quality is null and
|
||||
foil = ? and
|
||||
list_id = ?"
|
||||
quantity-change card-id foil? list-id])
|
||||
(jdbc/execute!
|
||||
db
|
||||
["update collection
|
||||
set quantity = quantity + ?
|
||||
where card_id = ? and
|
||||
quality = ? and
|
||||
foil = ? and
|
||||
list_id = ?"
|
||||
quantity-change card-id quality foil? list-id])))]
|
||||
(if (= 0 num-updates)
|
||||
(first
|
||||
(jdbc/execute!
|
||||
db
|
||||
["insert into collection
|
||||
(card_id, quality, quantity, foil, list_id)
|
||||
values
|
||||
(?, ?, ?, ?, ?)"
|
||||
card-id quality quantity-change foil? list-id]))
|
||||
num-updates)))
|
||||
|
||||
(defn update-collection!
|
||||
[card-id quality foil? list-id user-id quantity-change]
|
||||
;; written assuming postgresql server is _not_ 9.5+ (so, without access to UPSERT functionality)
|
||||
(let [list-id (int list-id)
|
||||
public-only? (nil? user-id)]
|
||||
(with-view-transaction
|
||||
view-system
|
||||
(jdbc/with-db-transaction
|
||||
[dt db]
|
||||
(if-not (first (jdbc/query dt ["select count(*) from lists where id = ? and is_public in (true, ?)" list-id public-only?]))
|
||||
(throw (new Exception (str "Not authorized to update list:" list-id)))
|
||||
(let [num-updates (first
|
||||
; i love that SQL forces you to use "is null" ...
|
||||
(if (nil? quality)
|
||||
(vexec! view-system dt
|
||||
["update collection
|
||||
set quantity = quantity + ?
|
||||
where card_id = ? and
|
||||
quality is null and
|
||||
foil = ? and
|
||||
list_id = ?"
|
||||
quantity-change card-id foil? list-id])
|
||||
(vexec! view-system dt
|
||||
["update collection
|
||||
set quantity = quantity + ?
|
||||
where card_id = ? and
|
||||
quality = ? and
|
||||
foil = ? and
|
||||
list_id = ?"
|
||||
quantity-change card-id quality foil? list-id])))]
|
||||
(if (= 0 num-updates)
|
||||
(first
|
||||
(vexec! view-system dt
|
||||
["insert into collection
|
||||
(card_id, quality, quantity, foil, list_id)
|
||||
values
|
||||
(?, ?, ?, ?, ?)"
|
||||
card-id quality quantity-change foil? list-id]))
|
||||
num-updates))))))
|
||||
(update-collection!* dt card-id quality foil? list-id quantity-change)))
|
||||
(views/put-hints! view-system [(views/hint nil #{:collection} vsql/hint-type)])))
|
||||
|
||||
(defn add-to-collection!
|
||||
[card-id quality foil? list-id user-id]
|
||||
|
|
Loading…
Reference in a new issue