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
|
(ns mtgcoll.models.collection
|
||||||
(:require
|
(:require
|
||||||
[clojure.java.jdbc :as jdbc]
|
[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.db :refer [db]]
|
||||||
[mtgcoll.views.core :refer [view-system]]))
|
[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!
|
(defn update-collection!
|
||||||
[card-id quality foil? list-id user-id quantity-change]
|
[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)
|
(let [list-id (int list-id)
|
||||||
public-only? (nil? user-id)]
|
public-only? (nil? user-id)]
|
||||||
(with-view-transaction
|
(jdbc/with-db-transaction
|
||||||
view-system
|
|
||||||
[dt db]
|
[dt db]
|
||||||
(if-not (first (jdbc/query dt ["select count(*) from lists where id = ? and is_public in (true, ?)" list-id public-only?]))
|
(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)))
|
(throw (new Exception (str "Not authorized to update list:" list-id)))
|
||||||
(let [num-updates (first
|
(update-collection!* dt card-id quality foil? list-id quantity-change)))
|
||||||
; i love that SQL forces you to use "is null" ...
|
(views/put-hints! view-system [(views/hint nil #{:collection} vsql/hint-type)])))
|
||||||
(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))))))
|
|
||||||
|
|
||||||
(defn add-to-collection!
|
(defn add-to-collection!
|
||||||
[card-id quality foil? list-id user-id]
|
[card-id quality foil? list-id user-id]
|
||||||
|
|
Loading…
Reference in a new issue