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:
Gered 2018-06-09 11:15:08 -04:00
parent 710c4bbba1
commit 2cf7a11c7b

View file

@ -1,24 +1,19 @@
(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! (defn- update-collection!*
[card-id quality foil? list-id user-id quantity-change] [db card-id quality foil? list-id quantity-change]
;; written assuming postgresql server is _not_ 9.5+ (so, without access to UPSERT functionality) ;; 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
[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 (let [num-updates (first
; i love that SQL forces you to use "is null" ... ; i love that SQL forces you to use "is null" ...
(if (nil? quality) (if (nil? quality)
(vexec! view-system dt (jdbc/execute!
db
["update collection ["update collection
set quantity = quantity + ? set quantity = quantity + ?
where card_id = ? and where card_id = ? and
@ -26,7 +21,8 @@
foil = ? and foil = ? and
list_id = ?" list_id = ?"
quantity-change card-id foil? list-id]) quantity-change card-id foil? list-id])
(vexec! view-system dt (jdbc/execute!
db
["update collection ["update collection
set quantity = quantity + ? set quantity = quantity + ?
where card_id = ? and where card_id = ? and
@ -36,13 +32,25 @@
quantity-change card-id quality foil? list-id])))] quantity-change card-id quality foil? list-id])))]
(if (= 0 num-updates) (if (= 0 num-updates)
(first (first
(vexec! view-system dt (jdbc/execute!
db
["insert into collection ["insert into collection
(card_id, quality, quantity, foil, list_id) (card_id, quality, quantity, foil, list_id)
values values
(?, ?, ?, ?, ?)" (?, ?, ?, ?, ?)"
card-id quality quantity-change foil? list-id])) card-id quality quantity-change foil? list-id]))
num-updates)))))) num-updates)))
(defn update-collection!
[card-id quality foil? list-id user-id quantity-change]
(let [list-id (int list-id)
public-only? (nil? user-id)]
(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)))
(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! (defn add-to-collection!
[card-id quality foil? list-id user-id] [card-id quality foil? list-id user-id]