From 2cf7a11c7b467b4118b2878720a2587fc4fb67c1 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 9 Jun 2018 11:15:08 -0400 Subject: [PATCH] preperatory refactor of update-collection! will need to use it in bulk operations, so split out the core per-card collection updating logic --- src/mtgcoll/models/collection.clj | 72 +++++++++++++++++-------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/src/mtgcoll/models/collection.clj b/src/mtgcoll/models/collection.clj index 3b8babe..8a7efb3 100644 --- a/src/mtgcoll/models/collection.clj +++ b/src/mtgcoll/models/collection.clj @@ -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]