Skip to content

Commit

Permalink
hide dead (#197)
Browse files Browse the repository at this point in the history
* add initial test

* fix toggle api call

* extra helper functions

* update makefile

* use active instead of enabled

* select only certain fields

* fix query

* use only active players in the list
  • Loading branch information
AndreaCrotti authored Jan 26, 2020
1 parent 9ab9e1f commit 1007e83
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 14 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ db-graph:


test:
lein test
lein kaocha

.PHONY: test
5 changes: 4 additions & 1 deletion api.rest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
:local = http://localhost:3335
:staging = http://fifa-elo-staging.herokuapp.com
:prod = http://fifa-elo.herokuapp.com
:league_id = 781c6c09-6dd8-4bf3-8647-225f1f542967
:league_id = f631b983-4de2-4be8-a1cf-2a630df3e2da

# check if the resources work
GET :local/css/screen.css
Expand All @@ -12,6 +12,9 @@ GET :local/js/compiled/app.js
# fetch players
GET :local/api/players?league_id=:league_id

# disable one of the players
POST :local/api/toggle-player?league_id=:league_id&player_id=f823ffe6-0238-40c7-bbf3-9abdaee45946&active=false

# fetch all the leagues
GET :local/api/leagues

Expand Down
29 changes: 21 additions & 8 deletions src/clj/byf/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
(:gen-class)
(:require [clojure.java.jdbc :as jdbc]
[clojure.data.json :as json]
[clj-time.format :as cf]
[bidi.ring :refer [make-handler]]
[buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]
[clojure.string :as str]
Expand Down Expand Up @@ -44,13 +43,6 @@
[m]
(medley/map-vals str m))

(defn convert
[m]
(cond
(map? m) (uuid-to-str m)
(sequential? m) (map uuid-to-str m)
:else m))

(defn- as-edn
[response]
(-> response
Expand Down Expand Up @@ -123,6 +115,13 @@
:league_id
validate/to-uuid))

(defn- get-player-id
[request]
(-> request
:params
:player_id
validate/to-uuid))

(defn get-players
[request]
(-> (get-league-id request)
Expand Down Expand Up @@ -183,10 +182,24 @@
(some? github-token))
:token github-token})))

(defn toggle-player!
[request]
(let [league-id (get-league-id request)
player-id (get-player-id request)
active_str (-> request :params :active)
active (if (= "true" active_str) true false)]
(db/toggle-player! league-id player-id active)
(resp/created
"api/players"
{:player-id player-id
:active active
:league_id league-id})))

;;TODO: add a not found page for everything else?
(def routes
["/" {"api/" {"add-player" add-player!
"add-game" add-game!
"toggle-player" toggle-player!

"league" get-league
"leagues" get-leagues
Expand Down
20 changes: 19 additions & 1 deletion src/clj/byf/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

(defn load-players-sql
[league-id]
(-> (h/select :*)
(-> (h/select :pl.id :pl.email :pl.name :lg.active)
(h/from [:player :pl])
(h/join [:league_players :lg]
[:= :pl.id :lg.player_id])
Expand Down Expand Up @@ -167,3 +167,21 @@
(-> (h/select :name)
(h/from :player)
(h/where [:= :id player-id])))

(defn toggle-player-sql
[league-id player-id active]
(->
(h/update :league-players)
(h/sset {:active active})
(h/where
[:and
[:= :player_id player-id]
[:= :league_id league-id]])))

(defn toggle-player!
[league-id player-id active]
(jdbc/execute! (db-spec)
(sql/format
(toggle-player-sql league-id
player-id
active))))
7 changes: 7 additions & 0 deletions src/cljs/byf/common/players.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
(map :id)
set)))

(rf/reg-sub ::active-players-full
:<- [::players]

(fn [players]
(->> players
(filter :active))))

(rf/reg-sub ::name-mapping
:<- [::players]

Expand Down
2 changes: 1 addition & 1 deletion src/cljs/byf/league_detail/add_game.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

(defn game-form
[]
(let [players (rf/subscribe [::players-handlers/players])
(let [players (rf/subscribe [::players-handlers/active-players-full])
valid-game? (rf/subscribe [::handlers/valid-game?])
game (rf/subscribe [::handlers/game])
league (rf/subscribe [::handlers/league])
Expand Down
27 changes: 25 additions & 2 deletions test/clj/byf/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
authenticated-req
sut/app)]

(assert (contains? #{201 401} status), "Invalid status code")
(when-not (contains? #{201 401} status)
(println "bad response, got " response)
(throw (Exception. "bad response, should be #{201, 401}")))
response))

(defn- store-users!
Expand Down Expand Up @@ -113,7 +115,12 @@
body-obj (json/read-str (:body response))]
(is (= 200 (:status response)))
(is (= 1 (count body-obj)))
(is (true? (-> body-obj first (get "active")))))))
(is (= {"email" nil,
"name" "john",
"active" true}
(-> body-obj
first
(dissoc "id")))))))

(deftest add-player-user-test
(with-redefs [env (assoc env :admin-password "admin-password")]
Expand Down Expand Up @@ -144,6 +151,22 @@
json/read-str
(get "id")))))))

(deftest enable-player-test
(testing "Enabling a player or disabling it"
(let [[p1-id _] (store-users!)
_response (write-api-call "/toggle-player" {:league_id sample-league-id
:player_id (:player-id p1-id)
:active false})

with-disabled (read-api-call "/api/players" {:league_id sample-league-id})
with-disabled-first-user (-> with-disabled
:body
json/read-str
first)]
;; now fetch the players
(is (= 200 (:status with-disabled)))
(is (false? (get with-disabled-first-user "active"))))))

(deftest auth-test
(testing "Should be able to check if a user is already authenticated"
(let [response (read-api-call "/authenticated")]
Expand Down

0 comments on commit 1007e83

Please sign in to comment.