add foil tracking to card collection
This commit is contained in:
parent
0879a24e9f
commit
a957440f3b
|
@ -0,0 +1,4 @@
|
|||
ALTER TABLE collection DROP COLUMN IF EXISTS foil;
|
||||
|
||||
ALTER TABLE collection DROP CONSTRAINT IF EXISTS collection_card_id_quality_foil_key;
|
||||
ALTER TABLE collection ADD CONSTRAINT collection_card_id_quality_key UNIQUE (card_id, quality);
|
|
@ -0,0 +1,6 @@
|
|||
ALTER TABLE collection ADD COLUMN foil BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
|
||||
CREATE INDEX collection_foil_idx ON collection (foil);
|
||||
|
||||
ALTER TABLE collection DROP CONSTRAINT IF EXISTS collection_card_id_quality_key;
|
||||
ALTER TABLE collection ADD CONSTRAINT collection_card_id_quality_foil_key UNIQUE (card_id, quality, foil);
|
|
@ -56,7 +56,7 @@ div.card-title > h1 > small {
|
|||
}
|
||||
|
||||
.inventory-container {
|
||||
width: 300px;
|
||||
width: 500px;
|
||||
}
|
||||
|
||||
.inventory-container .row:hover {
|
||||
|
|
|
@ -12,15 +12,15 @@
|
|||
["online" "near mint" "lightly played" "moderately played" "heavily played" "damaged"])
|
||||
|
||||
(defn on-add-card
|
||||
[card-id quality]
|
||||
[card-id quality foil?]
|
||||
(ajax/POST (->url "/collection/add")
|
||||
:params {:card-id card-id :quality quality}
|
||||
:params {:card-id card-id :quality quality :foil foil?}
|
||||
:on-error #(set-error! "Server error while adding card to inventory.")))
|
||||
|
||||
(defn on-remove-card
|
||||
[card-id quality]
|
||||
[card-id quality foil?]
|
||||
(ajax/POST (->url "/collection/remove")
|
||||
:params {:card-id card-id :quality quality}
|
||||
:params {:card-id card-id :quality quality :foil foil?}
|
||||
:on-error #(set-error! "Server error while adding card to inventory.")))
|
||||
|
||||
(defvc inventory-management
|
||||
|
@ -28,26 +28,47 @@
|
|||
(let [inventory (view-cursor :owned-card card-id)
|
||||
inventory (group-by :quality @inventory)]
|
||||
[bs/Grid {:fluid true :class "inventory-container"}
|
||||
[bs/Row
|
||||
[bs/Col {:sm 4} ""]
|
||||
[bs/Col {:sm 4 :class "text-center"} [:strong "Normal"]]
|
||||
[bs/Col {:sm 4 :class "text-center"} [:strong "Foil"]]]
|
||||
(map-indexed
|
||||
(fn [idx quality]
|
||||
(let [inventory (first (get inventory quality))
|
||||
quantity (or (:quantity inventory) 0)]
|
||||
(let [inventory (get inventory quality)
|
||||
quantities (group-by :foil inventory)
|
||||
foil-quantity (or (:quantity (first (get quantities true))) 0)
|
||||
non-foil-quantity (or (:quantity (first (get quantities false))) 0)]
|
||||
^{:key idx}
|
||||
[bs/Row
|
||||
{:class (if (> quantity 0) "bg-warning")}
|
||||
[bs/Col {:sm 6 :class "text-right"}
|
||||
{:class (if (or (> foil-quantity 0)
|
||||
(> non-foil-quantity 0))
|
||||
"bg-warning")}
|
||||
[bs/Col {:sm 4 :class "text-right"}
|
||||
[bs/FormControl.Static
|
||||
(str (string/capitalize quality) ": ")]]
|
||||
[bs/Col {:sm 2}
|
||||
;; non-foil
|
||||
[bs/Col {:sm 1}
|
||||
[bs/FormControl.Static
|
||||
[:strong quantity]]]
|
||||
[bs/Col {:sm 4}
|
||||
[:strong non-foil-quantity]]]
|
||||
[bs/Col {:sm 3}
|
||||
[bs/ButtonGroup {:justified true}
|
||||
[bs/ButtonGroup
|
||||
[bs/Button {:bsStyle "success" :on-click #(on-add-card card-id quality)}
|
||||
[bs/Button {:bsStyle "success" :on-click #(on-add-card card-id quality false)}
|
||||
[bs/Glyphicon {:glyph "plus"}]]]
|
||||
[bs/ButtonGroup
|
||||
[bs/Button {:bsStyle "danger" :disabled (= 0 quantity) :on-click #(on-remove-card card-id quality)}
|
||||
[bs/Button {:bsStyle "danger" :disabled (= 0 non-foil-quantity) :on-click #(on-remove-card card-id quality false)}
|
||||
[bs/Glyphicon {:glyph "minus"}]]]]]
|
||||
;; foil
|
||||
[bs/Col {:sm 1}
|
||||
[bs/FormControl.Static
|
||||
[:strong foil-quantity]]]
|
||||
[bs/Col {:sm 3}
|
||||
[bs/ButtonGroup {:justified true}
|
||||
[bs/ButtonGroup
|
||||
[bs/Button {:bsStyle "success" :on-click #(on-add-card card-id quality true)}
|
||||
[bs/Glyphicon {:glyph "plus"}]]]
|
||||
[bs/ButtonGroup
|
||||
[bs/Button {:bsStyle "danger" :disabled (= 0 foil-quantity) :on-click #(on-remove-card card-id quality true)}
|
||||
[bs/Glyphicon {:glyph "minus"}]]]]]]))
|
||||
qualities)]))
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
[mtgcoll.views.core :refer [view-system]]))
|
||||
|
||||
(defn update-collection!
|
||||
[card-id quality quantity-change]
|
||||
[card-id quality foil? quantity-change]
|
||||
;; written assuming postgresql server is _not_ 9.5+ (so, without access to UPSERT functionality)
|
||||
(with-view-transaction
|
||||
view-system
|
||||
|
@ -15,22 +15,23 @@
|
|||
["update collection
|
||||
set quantity = quantity + ?
|
||||
where card_id = ? and
|
||||
quality = ?"
|
||||
quantity-change card-id quality]))]
|
||||
quality = ? and
|
||||
foil = ?"
|
||||
quantity-change card-id quality foil?]))]
|
||||
(if (= 0 num-updates)
|
||||
(first
|
||||
(vexec! view-system dt
|
||||
["insert into collection
|
||||
(card_id, quality, quantity)
|
||||
(card_id, quality, quantity, foil)
|
||||
values
|
||||
(?, ?, ?)"
|
||||
card-id quality quantity-change]))
|
||||
(?, ?, ?, ?)"
|
||||
card-id quality quantity-change foil?]))
|
||||
num-updates))))
|
||||
|
||||
(defn add-to-collection!
|
||||
[card-id quality]
|
||||
(update-collection! card-id quality 1))
|
||||
[card-id quality foil?]
|
||||
(update-collection! card-id quality foil? 1))
|
||||
|
||||
(defn remove-from-collection!
|
||||
[card-id quality]
|
||||
(update-collection! card-id quality -1))
|
||||
[card-id quality foil?]
|
||||
(update-collection! card-id quality foil? -1))
|
||||
|
|
|
@ -6,10 +6,10 @@
|
|||
|
||||
(def collection-routes
|
||||
(routes
|
||||
(POST "/collection/add" [card-id quality]
|
||||
(collection/add-to-collection! card-id quality)
|
||||
(POST "/collection/add" [card-id quality foil]
|
||||
(collection/add-to-collection! card-id quality foil)
|
||||
(response/json {:status "ok"}))
|
||||
|
||||
(POST "/collection/remove" [card-id quality]
|
||||
(collection/remove-from-collection! card-id quality)
|
||||
(POST "/collection/remove" [card-id quality foil]
|
||||
(collection/remove-from-collection! card-id quality foil)
|
||||
(response/json {:status "ok"}))))
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
(defn owned-card
|
||||
[card-id]
|
||||
["select quality, quantity
|
||||
["select quality, quantity, foil
|
||||
from collection
|
||||
where card_id = ?"
|
||||
card-id])
|
||||
|
|
Loading…
Reference in a new issue