From 4b9aec3656a5b4daa760709d50c26f9fe2b65608 Mon Sep 17 00:00:00 2001 From: "Howard M. Lewis Ship" Date: Fri, 12 Oct 2018 11:50:12 -0700 Subject: [PATCH 01/11] Add a doctring from take! --- src/clojure_game_geek/db.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/clojure_game_geek/db.clj b/src/clojure_game_geek/db.clj index 66fa888..fb01ae7 100644 --- a/src/clojure_game_geek/db.clj +++ b/src/clojure_game_geek/db.clj @@ -1,6 +1,5 @@ (ns clojure-game-geek.db (:require - [clojure.java.io :as io] [com.stuartsierra.component :as component] [postgres.async :refer [open-db query! close-db!]] [clojure.core.async :refer [ClojureGameGeekDb {})}) (defn ^:private take! + "Takes a value from a channel and rethrows an exception if that is the + conveyed value." [ch] (let [v ( Date: Fri, 12 Oct 2018 13:16:45 -0700 Subject: [PATCH 02/11] Update dependencies to latest --- project.clj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/project.clj b/project.clj index c3f857f..210a901 100644 --- a/project.clj +++ b/project.clj @@ -3,10 +3,10 @@ :url "https://github.com/walmartlabs/clojure-game-geek" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} - :dependencies [[org.clojure/clojure "1.8.0"] - [org.clojure/core.async "0.3.443"] + :dependencies [[org.clojure/clojure "1.9.0"] + [org.clojure/core.async "0.4.474"] [com.stuartsierra/component "0.3.2"] - [com.walmartlabs/lacinia "0.25.0"] - [com.walmartlabs/lacinia-pedestal "0.7.0"] + [com.walmartlabs/lacinia "0.30.0"] + [com.walmartlabs/lacinia-pedestal "0.10.0"] [alaisi/postgres.async "0.8.0"] - [io.aviso/logging "0.2.0"]]) + [io.aviso/logging "0.3.1"]]) From 7783822f9b2606d95b5db79391e60a444fd05dac Mon Sep 17 00:00:00 2001 From: "Howard M. Lewis Ship" Date: Fri, 12 Oct 2018 16:50:21 -0700 Subject: [PATCH 03/11] Setup for first test --- dev-resources/user.clj | 22 ++-------------- src/clojure_game_geek/server.clj | 7 ++--- test/clojure_game_geek/system_tests.clj | 35 +++++++++++++++++++++++++ test/clojure_game_geek/test_utils.clj | 23 ++++++++++++++++ 4 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 test/clojure_game_geek/system_tests.clj create mode 100644 test/clojure_game_geek/test_utils.clj diff --git a/dev-resources/user.clj b/dev-resources/user.clj index f62170f..ad6be99 100644 --- a/dev-resources/user.clj +++ b/dev-resources/user.clj @@ -3,26 +3,8 @@ [com.walmartlabs.lacinia :as lacinia] [clojure.java.browse :refer [browse-url]] [clojure-game-geek.system :as system] - [clojure.walk :as walk] - [com.stuartsierra.component :as component]) - (:import (clojure.lang IPersistentMap))) - -(defn simplify - "Converts all ordered maps nested within the map into standard hash maps, and - sequences into vectors, which makes for easier constants in the tests, and eliminates ordering problems." - [m] - (walk/postwalk - (fn [node] - (cond - (instance? IPersistentMap node) - (into {} node) - - (seq? node) - (vec node) - - :else - node)) - m)) + [clojure-game-geek.test-utils :refer [simplify]] + [com.stuartsierra.component :as component])) (defonce system nil) diff --git a/src/clojure_game_geek/server.clj b/src/clojure_game_geek/server.clj index 68f5f74..3497e0e 100644 --- a/src/clojure_game_geek/server.clj +++ b/src/clojure_game_geek/server.clj @@ -3,13 +3,14 @@ [com.walmartlabs.lacinia.pedestal :as lp] [io.pedestal.http :as http])) -(defrecord Server [schema-provider server] +(defrecord Server [schema-provider server port] component/Lifecycle (start [this] (assoc this :server (-> schema-provider :schema - (lp/service-map {:graphiql true}) + (lp/service-map {:graphiql true + :port port}) http/create-server http/start))) @@ -19,7 +20,7 @@ (defn new-server [] - {:server (component/using (map->Server {}) + {:server (component/using (map->Server {:port 8888}) [:schema-provider])}) diff --git a/test/clojure_game_geek/system_tests.clj b/test/clojure_game_geek/system_tests.clj new file mode 100644 index 0000000..883af2a --- /dev/null +++ b/test/clojure_game_geek/system_tests.clj @@ -0,0 +1,35 @@ +(ns clojure-game-geek.system-tests + (:require + [clojure.test :refer [deftest is]] + [clojure-game-geek.system :as system] + [clojure-game-geek.test-utils :refer [simplify]] + [com.stuartsierra.component :as component] + [com.walmartlabs.lacinia :as lacinia])) + +(defn ^:private test-system + "Creates a new system suitable for testing, and ensures that + the HTTP port won't conflict with a default running system." + [] + (-> (system/new-system) + (assoc-in [:server :port] 8989))) + +(defn ^:private q + "Extracts the compiled schema and executes a query." + [system query variables] + (-> system + (get-in [:schema-provider :schema]) + (lacinia/execute query variables nil) + simplify)) + +(deftest can-read-board-game + (let [system (component/start-system (test-system)) + results (q system + "{ game_by_id(id: 1234) { name summary min_players max_players play_time }}" + nil)] + (is (= {:data {:game_by_id {:max_players 2 + :min_players 2 + :name "Zertz" + :play_time nil + :summary "Two player abstract with forced moves and shrinking board"}}} + results)) + (component/stop-system system))) diff --git a/test/clojure_game_geek/test_utils.clj b/test/clojure_game_geek/test_utils.clj new file mode 100644 index 0000000..1d2ad90 --- /dev/null +++ b/test/clojure_game_geek/test_utils.clj @@ -0,0 +1,23 @@ +(ns clojure-game-geek.test-utils + (:require + [clojure.walk :as walk]) + (:import + (clojure.lang IPersistentMap))) + +(defn simplify + "Converts all ordered maps nested within the map into standard hash maps, and + sequences into vectors, which makes for easier constants in the tests, and eliminates ordering problems." + [m] + (walk/postwalk + (fn [node] + (cond + (instance? IPersistentMap node) + (into {} node) + + (seq? node) + (vec node) + + :else + node)) + m)) + From 4388d0d7974e498fb600da77a2d7915c8e8a0812 Mon Sep 17 00:00:00 2001 From: "Howard M. Lewis Ship" Date: Fri, 12 Oct 2018 17:27:36 -0700 Subject: [PATCH 04/11] Move test utilities code around --- {test => dev-resources}/clojure_game_geek/test_utils.clj | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {test => dev-resources}/clojure_game_geek/test_utils.clj (100%) diff --git a/test/clojure_game_geek/test_utils.clj b/dev-resources/clojure_game_geek/test_utils.clj similarity index 100% rename from test/clojure_game_geek/test_utils.clj rename to dev-resources/clojure_game_geek/test_utils.clj From c0dd4dd6affc5acf3c28834cdad76fc975a7a745 Mon Sep 17 00:00:00 2001 From: "Howard M. Lewis Ship" Date: Fri, 19 Oct 2018 13:27:51 -0700 Subject: [PATCH 05/11] Refactor test code --- src/clojure_game_geek/db.clj | 77 ++++++++++++------------- test/clojure_game_geek/system_tests.clj | 34 ++++++----- 2 files changed, 57 insertions(+), 54 deletions(-) diff --git a/src/clojure_game_geek/db.clj b/src/clojure_game_geek/db.clj index fb01ae7..05a32c5 100644 --- a/src/clojure_game_geek/db.clj +++ b/src/clojure_game_geek/db.clj @@ -44,58 +44,55 @@ (defn find-member-by-id [component member-id] - (->> component - :db - deref - :members - (filter #(= member-id (:id %))) - first)) + (-> (query! (:conn component) + ["select member_id, name, created_at, updated_at + from member + where member_id = $1" member-id]) + take! + first)) (defn list-designers-for-game [component game-id] - (let [designers (:designers (find-game-by-id component game-id))] - (->> component - :db - deref - :designers - (filter #(contains? designers (:id %)))))) + (take! + (query! (:conn component) + ["select d.designer_id, d.name, d.uri, d.created_at, d.updated_at + from designer d + inner join designer_to_game j on (d.designer_id = j.designer_id) + where j.game_id = $1 + order by d.name" game-id]))) (defn list-games-for-designer [component designer-id] - (->> component - :db - deref - :games - (filter #(-> % :designers (contains? designer-id))))) + (take! + (query! (:conn component) + ["select g.game_id, g.name, g.summary, g.min_players, g.max_players, g.created_at, g.updated_at + from board_game g + inner join designer_to_game j on (g.game_id = j.game_id) + where j.designer_id = $1 + order by g.name" designer-id]))) (defn list-ratings-for-game [component game-id] - (->> component - :db - deref - :ratings - (filter #(= game-id (:game_id %))))) + (take! + (query! (:conn component) + ["select game_id, member_id, rating, created_at, updated_at + from game_rating + where game_id = $1" game-id]))) (defn list-ratings-for-member [component member-id] - (->> component - :db - deref - :ratings - (filter #(= member-id (:member_id %))))) - -(defn ^:private apply-game-rating - [game-ratings game-id member-id rating] - (->> game-ratings - (remove #(and (= game-id (:game_id %)) - (= member-id (:member_id %)))) - (cons {:game_id game-id - :member_id member-id - :rating rating}))) + (take! + (query! (:conn component) + ["select game_id, member_id, rating, created_at, updated_at + from game_rating + where member_id = $1" member-id]))) (defn upsert-game-rating "Adds a new game rating, or changes the value of an existing game rating." - [db game-id member-id rating] - (-> db - :db - (swap! update :ratings apply-game-rating game-id member-id rating))) + [component game-id member-id rating] + (take! + (query! (:conn component) + ["insert into game_rating (game_id, member_id, rating) + values ($1, $2, $3) + on conflict (game_id, member_id) do update set rating = $3" + game-id member-id rating]))) diff --git a/test/clojure_game_geek/system_tests.clj b/test/clojure_game_geek/system_tests.clj index 883af2a..10c7d00 100644 --- a/test/clojure_game_geek/system_tests.clj +++ b/test/clojure_game_geek/system_tests.clj @@ -1,6 +1,6 @@ (ns clojure-game-geek.system-tests (:require - [clojure.test :refer [deftest is]] + [clojure.test :refer [deftest is use-fixtures]] [clojure-game-geek.system :as system] [clojure-game-geek.test-utils :refer [simplify]] [com.stuartsierra.component :as component] @@ -13,23 +13,29 @@ (-> (system/new-system) (assoc-in [:server :port] 8989))) +(def ^:dynamic ^:private *system*) + +(use-fixtures :once + (fn [test-fn] + (binding [*system* (component/start-system (test-system))] + (try + (test-fn) + (finally + (component/stop-system *system*)))))) + (defn ^:private q "Extracts the compiled schema and executes a query." - [system query variables] - (-> system + [query variables] + (-> *system* (get-in [:schema-provider :schema]) (lacinia/execute query variables nil) simplify)) (deftest can-read-board-game - (let [system (component/start-system (test-system)) - results (q system - "{ game_by_id(id: 1234) { name summary min_players max_players play_time }}" - nil)] - (is (= {:data {:game_by_id {:max_players 2 - :min_players 2 - :name "Zertz" - :play_time nil - :summary "Two player abstract with forced moves and shrinking board"}}} - results)) - (component/stop-system system))) + (is (= {:data {:game_by_id {:max_players 2 + :min_players 2 + :name "Zertz" + :play_time nil + :summary "Two player abstract with forced moves and shrinking board"}}} + (q "{ game_by_id(id: 1234) { name summary min_players max_players play_time }}" + nil)))) From 9d588edac8afa44afd9a5ae095f88332fdba6c25 Mon Sep 17 00:00:00 2001 From: "Howard M. Lewis Ship" Date: Fri, 19 Oct 2018 13:56:58 -0700 Subject: [PATCH 06/11] Add logging --- dev-resources/logback-test.xml | 2 ++ src/clojure_game_geek/db.clj | 27 ++++++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/dev-resources/logback-test.xml b/dev-resources/logback-test.xml index 3a510b7..0e16d91 100644 --- a/dev-resources/logback-test.xml +++ b/dev-resources/logback-test.xml @@ -10,4 +10,6 @@ + + diff --git a/src/clojure_game_geek/db.clj b/src/clojure_game_geek/db.clj index 05a32c5..1f62f72 100644 --- a/src/clojure_game_geek/db.clj +++ b/src/clojure_game_geek/db.clj @@ -1,8 +1,10 @@ (ns clojure-game-geek.db (:require [com.stuartsierra.component :as component] - [postgres.async :refer [open-db query! close-db!]] - [clojure.core.async :refer [ (query! (:conn component) + (-> (query! component ["select game_id, name, summary, min_players, max_players, created_at, updated_at from board_game where game_id = $1" game-id]) take! @@ -44,7 +53,7 @@ (defn find-member-by-id [component member-id] - (-> (query! (:conn component) + (-> (query! component ["select member_id, name, created_at, updated_at from member where member_id = $1" member-id]) @@ -54,7 +63,7 @@ (defn list-designers-for-game [component game-id] (take! - (query! (:conn component) + (query! component ["select d.designer_id, d.name, d.uri, d.created_at, d.updated_at from designer d inner join designer_to_game j on (d.designer_id = j.designer_id) @@ -64,7 +73,7 @@ (defn list-games-for-designer [component designer-id] (take! - (query! (:conn component) + (query! component ["select g.game_id, g.name, g.summary, g.min_players, g.max_players, g.created_at, g.updated_at from board_game g inner join designer_to_game j on (g.game_id = j.game_id) @@ -74,7 +83,7 @@ (defn list-ratings-for-game [component game-id] (take! - (query! (:conn component) + (query! component ["select game_id, member_id, rating, created_at, updated_at from game_rating where game_id = $1" game-id]))) @@ -82,7 +91,7 @@ (defn list-ratings-for-member [component member-id] (take! - (query! (:conn component) + (query! component ["select game_id, member_id, rating, created_at, updated_at from game_rating where member_id = $1" member-id]))) @@ -91,7 +100,7 @@ "Adds a new game rating, or changes the value of an existing game rating." [component game-id member-id rating] (take! - (query! (:conn component) + (query! component ["insert into game_rating (game_id, member_id, rating) values ($1, $2, $3) on conflict (game_id, member_id) do update set rating = $3" From 587b2809386469bb30df347f971c72889cad9239 Mon Sep 17 00:00:00 2001 From: "Howard M. Lewis Ship" Date: Fri, 19 Oct 2018 14:48:48 -0700 Subject: [PATCH 07/11] Correct upsert --- src/clojure_game_geek/db.clj | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/clojure_game_geek/db.clj b/src/clojure_game_geek/db.clj index 1f62f72..05c2fdb 100644 --- a/src/clojure_game_geek/db.clj +++ b/src/clojure_game_geek/db.clj @@ -99,9 +99,10 @@ (defn upsert-game-rating "Adds a new game rating, or changes the value of an existing game rating." [component game-id member-id rating] - (take! - (query! component - ["insert into game_rating (game_id, member_id, rating) + (-> (query! component + ["insert into game_rating (game_id, member_id, rating) values ($1, $2, $3) on conflict (game_id, member_id) do update set rating = $3" - game-id member-id rating]))) + game-id member-id rating]) + take! + first)) From cb2d1187d6cd244ad3b7435ec6e94b127c37e69d Mon Sep 17 00:00:00 2001 From: "Howard M. Lewis Ship" Date: Fri, 19 Oct 2018 19:59:02 -0700 Subject: [PATCH 08/11] Complete transition to org.clojure/java.jdbc --- project.clj | 5 +- src/clojure_game_geek/db.clj | 122 ++++++++++++++++------------------- 2 files changed, 60 insertions(+), 67 deletions(-) diff --git a/project.clj b/project.clj index 210a901..b7d8800 100644 --- a/project.clj +++ b/project.clj @@ -4,9 +4,10 @@ :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.9.0"] - [org.clojure/core.async "0.4.474"] [com.stuartsierra/component "0.3.2"] [com.walmartlabs/lacinia "0.30.0"] [com.walmartlabs/lacinia-pedestal "0.10.0"] - [alaisi/postgres.async "0.8.0"] + [org.clojure/java.jdbc "0.7.8"] + [org.postgresql/postgresql "42.2.5.jre7"] + [com.mchange/c3p0 "0.9.5.2"] [io.aviso/logging "0.3.1"]]) diff --git a/src/clojure_game_geek/db.clj b/src/clojure_game_geek/db.clj index 05c2fdb..bfebd93 100644 --- a/src/clojure_game_geek/db.clj +++ b/src/clojure_game_geek/db.clj @@ -1,108 +1,100 @@ (ns clojure-game-geek.db (:require [com.stuartsierra.component :as component] - [postgres.async :refer [open-db close-db!] :as pa] - [clojure.core.async :refer [ ds :datasource .close) + (assoc this :ds nil))) (defn new-db [] {:db (map->ClojureGameGeekDb {})}) -(defn ^:private take! - "Takes a value from a channel and rethrows an exception if that is the - conveyed value." - [ch] - (let [v ( (query! component - ["select game_id, name, summary, min_players, max_players, created_at, updated_at - from board_game where game_id = $1" game-id]) - take! - first)) + (first + (query component + ["select game_id, name, summary, min_players, max_players, created_at, updated_at + from board_game where game_id = ?" game-id]))) (defn find-member-by-id [component member-id] - (-> (query! component - ["select member_id, name, created_at, updated_at - from member - where member_id = $1" member-id]) - take! - first)) + (first + (query component + ["select member_id, name, created_at, updated_at + from member + where member_id = $1" member-id]))) (defn list-designers-for-game [component game-id] - (take! - (query! component - ["select d.designer_id, d.name, d.uri, d.created_at, d.updated_at - from designer d - inner join designer_to_game j on (d.designer_id = j.designer_id) - where j.game_id = $1 - order by d.name" game-id]))) + (query component + ["select d.designer_id, d.name, d.uri, d.created_at, d.updated_at + from designer d + inner join designer_to_game j on (d.designer_id = j.designer_id) + where j.game_id = $1 + order by d.name" game-id])) (defn list-games-for-designer [component designer-id] - (take! - (query! component - ["select g.game_id, g.name, g.summary, g.min_players, g.max_players, g.created_at, g.updated_at - from board_game g - inner join designer_to_game j on (g.game_id = j.game_id) - where j.designer_id = $1 - order by g.name" designer-id]))) + (query component + ["select g.game_id, g.name, g.summary, g.min_players, g.max_players, g.created_at, g.updated_at + from board_game g + inner join designer_to_game j on (g.game_id = j.game_id) + where j.designer_id = $1 + order by g.name" designer-id])) (defn list-ratings-for-game [component game-id] - (take! - (query! component - ["select game_id, member_id, rating, created_at, updated_at - from game_rating - where game_id = $1" game-id]))) + (query component + ["select game_id, member_id, rating, created_at, updated_at + from game_rating + where game_id = $1" game-id])) (defn list-ratings-for-member [component member-id] - (take! - (query! component - ["select game_id, member_id, rating, created_at, updated_at - from game_rating - where member_id = $1" member-id]))) + (query component + ["select game_id, member_id, rating, created_at, updated_at + from game_rating + where member_id = $1" member-id])) (defn upsert-game-rating - "Adds a new game rating, or changes the value of an existing game rating." + "Adds a new game rating, or changes the value of an existing game rating. + + Returns nil" [component game-id member-id rating] - (-> (query! component - ["insert into game_rating (game_id, member_id, rating) - values ($1, $2, $3) - on conflict (game_id, member_id) do update set rating = $3" - game-id member-id rating]) - take! - first)) + (query component + ["insert into game_rating (game_id, member_id, rating) + values ($1, $2, $3) + on conflict (game_id, member_id) do update set rating = $3" + game-id member-id rating]) + + nil) From d4182e033ca9c978aa6a6eba24b0c886efedc220 Mon Sep 17 00:00:00 2001 From: brdloush Date: Sun, 29 Dec 2019 17:00:50 +0100 Subject: [PATCH 09/11] Fix post-jdbc.core-migration problems (col. names, upsert, pos. params, constraint) --- bin/setup-db.sh | 3 +++ resources/cgg-schema.edn | 2 +- src/clojure_game_geek/db.clj | 25 ++++++++++++++++--------- src/clojure_game_geek/schema.clj | 8 ++++---- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/bin/setup-db.sh b/bin/setup-db.sh index 756b852..09327dd 100755 --- a/bin/setup-db.sh +++ b/bin/setup-db.sh @@ -62,6 +62,9 @@ create table game_rating ( created_at timestamp not null default current_timestamp, updated_at timestamp not null default current_timestamp); +alter table game_rating add constraint cost_unique__game_rating_game_id_member_id +unique (game_id, member_id); + create trigger game_rating_updated_at before update on game_rating for each row execute procedure mantain_updated_at(); diff --git a/resources/cgg-schema.edn b/resources/cgg-schema.edn index 95e294c..a810e09 100644 --- a/resources/cgg-schema.edn +++ b/resources/cgg-schema.edn @@ -32,7 +32,7 @@ {:description "A member of Clojure Game Geek. Members can rate games." :fields {:member_id {:type (non-null Int)} - :member_name {:type (non-null String) + :name {:type (non-null String) :description "Unique name of member."} :ratings {:type (list :GameRating) :description "List of games and ratings provided by this member." diff --git a/src/clojure_game_geek/db.clj b/src/clojure_game_geek/db.clj index bfebd93..0a3c1e7 100644 --- a/src/clojure_game_geek/db.clj +++ b/src/clojure_game_geek/db.clj @@ -39,6 +39,13 @@ :params params)) (jdbc/query (:ds component) statement)) +(defn ^:private execute! + [component statement] + (let [[sql & params] statement] + (log/debug :sql (str/replace sql #"\s+" " ") + :params params)) + (jdbc/execute! (:ds component) statement)) + (defn find-game-by-id [component game-id] (first @@ -52,7 +59,7 @@ (query component ["select member_id, name, created_at, updated_at from member - where member_id = $1" member-id]))) + where member_id = ?" member-id]))) (defn list-designers-for-game [component game-id] @@ -60,7 +67,7 @@ ["select d.designer_id, d.name, d.uri, d.created_at, d.updated_at from designer d inner join designer_to_game j on (d.designer_id = j.designer_id) - where j.game_id = $1 + where j.game_id = ? order by d.name" game-id])) (defn list-games-for-designer @@ -69,7 +76,7 @@ ["select g.game_id, g.name, g.summary, g.min_players, g.max_players, g.created_at, g.updated_at from board_game g inner join designer_to_game j on (g.game_id = j.game_id) - where j.designer_id = $1 + where j.designer_id = ? order by g.name" designer-id])) (defn list-ratings-for-game @@ -77,24 +84,24 @@ (query component ["select game_id, member_id, rating, created_at, updated_at from game_rating - where game_id = $1" game-id])) + where game_id = ?" game-id])) (defn list-ratings-for-member [component member-id] (query component ["select game_id, member_id, rating, created_at, updated_at from game_rating - where member_id = $1" member-id])) + where member_id = ?" member-id])) (defn upsert-game-rating "Adds a new game rating, or changes the value of an existing game rating. Returns nil" [component game-id member-id rating] - (query component + (execute! component ["insert into game_rating (game_id, member_id, rating) - values ($1, $2, $3) - on conflict (game_id, member_id) do update set rating = $3" - game-id member-id rating]) + values (?, ?, ?) + on conflict (game_id, member_id) do update set rating = ?" + game-id member-id rating rating]) nil) diff --git a/src/clojure_game_geek/schema.clj b/src/clojure_game_geek/schema.clj index 36c7991..c0bf768 100644 --- a/src/clojure_game_geek/schema.clj +++ b/src/clojure_game_geek/schema.clj @@ -48,17 +48,17 @@ (defn board-game-designers [db] (fn [_ _ board-game] - (db/list-designers-for-game db (:id board-game)))) + (db/list-designers-for-game db (:game_id board-game)))) (defn designer-games [db] (fn [_ _ designer] - (db/list-games-for-designer db (:id designer)))) + (db/list-games-for-designer db (:designer_id designer)))) (defn rating-summary [db] (fn [_ _ board-game] - (let [ratings (map :rating (db/list-ratings-for-game db (:id board-game))) + (let [ratings (map :rating (db/list-ratings-for-game db (:game_id board-game))) n (count ratings)] {:count n :average (if (zero? n) @@ -69,7 +69,7 @@ (defn member-ratings [db] (fn [_ _ member] - (db/list-ratings-for-member db (:id member)))) + (db/list-ratings-for-member db (:member_id member)))) (defn game-rating->game [db] From 8266612a8ed018f6e377c7600cdb96b47bcb510f Mon Sep 17 00:00:00 2001 From: Chandan Kumar Date: Fri, 12 Mar 2021 14:35:11 +0530 Subject: [PATCH 10/11] #2 Add org.flatland/ordered dependency to fix local repl issue --- project.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/project.clj b/project.clj index b7d8800..20689b9 100644 --- a/project.clj +++ b/project.clj @@ -10,4 +10,5 @@ [org.clojure/java.jdbc "0.7.8"] [org.postgresql/postgresql "42.2.5.jre7"] [com.mchange/c3p0 "0.9.5.2"] + [org.flatland/ordered "1.5.7"] [io.aviso/logging "0.3.1"]]) From 62b5fcf7b15e348eb7cda878097b40f4934f0f91 Mon Sep 17 00:00:00 2001 From: "Howard M. Lewis Ship" Date: Tue, 17 Jan 2023 11:49:32 -0800 Subject: [PATCH 11/11] Bring up to date with the new (Jan 2023) Lacinia tutorial --- CHANGELOG.md | 8 +- LICENSE | 221 +++++++++++++++++- README.md | 76 +++++- bin/docker-down.sh | 3 - bin/docker-up.sh | 4 - bin/psql.sh | 2 +- bin/setup-db.sh | 29 ++- build.clj | 17 ++ deps.edn | 25 ++ dev-resources/cgg-data.edn | 55 +++++ dev-resources/logback-test.xml | 18 +- .../{ => my}/clojure_game_geek/test_utils.clj | 9 +- dev-resources/user.clj | 37 +-- doc/intro.md | 3 + docker-compose.yml | 7 +- project.clj | 14 -- resources/cgg-schema.edn | 118 +++++----- src/clojure_game_geek/db.clj | 107 --------- src/clojure_game_geek/schema.clj | 113 --------- src/clojure_game_geek/system.clj | 13 -- src/my/clojure_game_geek.clj | 12 + src/my/clojure_game_geek/db.clj | 133 +++++++++++ src/my/clojure_game_geek/schema.clj | 106 +++++++++ src/{ => my}/clojure_game_geek/server.clj | 15 +- src/my/clojure_game_geek/system.clj | 19 ++ test/clojure_game_geek/system_tests.clj | 41 ---- test/my/clojure_game_geek/system_test.clj | 34 +++ 27 files changed, 804 insertions(+), 435 deletions(-) delete mode 100755 bin/docker-down.sh delete mode 100755 bin/docker-up.sh create mode 100644 build.clj create mode 100644 deps.edn create mode 100644 dev-resources/cgg-data.edn rename dev-resources/{ => my}/clojure_game_geek/test_utils.clj (76%) create mode 100644 doc/intro.md delete mode 100644 project.clj delete mode 100644 src/clojure_game_geek/db.clj delete mode 100644 src/clojure_game_geek/schema.clj delete mode 100644 src/clojure_game_geek/system.clj create mode 100644 src/my/clojure_game_geek.clj create mode 100644 src/my/clojure_game_geek/db.clj create mode 100644 src/my/clojure_game_geek/schema.clj rename src/{ => my}/clojure_game_geek/server.clj (56%) create mode 100644 src/my/clojure_game_geek/system.clj delete mode 100644 test/clojure_game_geek/system_tests.clj create mode 100644 test/my/clojure_game_geek/system_test.clj diff --git a/CHANGELOG.md b/CHANGELOG.md index ca6d732..50c8bf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. This change ### Changed - Add a new arity to `make-widget-async` to provide a different widget shape. -## [0.1.1] - 2017-10-18 +## [0.1.1] - 2022-12-16 ### Changed - Documentation on how to make the widgets. @@ -15,10 +15,10 @@ All notable changes to this project will be documented in this file. This change ### Fixed - Fixed widget maker to keep working when daylight savings switches over. -## 0.1.0 - 2017-10-18 +## 0.1.0 - 2022-12-16 ### Added - Files from the new template. - Widget maker public API - `make-widget-sync`. -[Unreleased]: https://github.com/your-name/clojure-game-geek/compare/0.1.1...HEAD -[0.1.1]: https://github.com/your-name/clojure-game-geek/compare/0.1.0...0.1.1 +[Unreleased]: https://github.com/my/clojure-game-geek/compare/0.1.1...HEAD +[0.1.1]: https://github.com/my/clojure-game-geek/compare/0.1.0...0.1.1 diff --git a/LICENSE b/LICENSE index 2f2dae8..d921d3d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,13 +1,214 @@ -Copyright 2017 WalmartLabs +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at +1. DEFINITIONS - http://www.apache.org/licenses/LICENSE-2.0 +"Contribution" means: -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. \ No newline at end of file +a) in the case of the initial Contributor, the initial code and +documentation distributed under this Agreement, and + +b) in the case of each subsequent Contributor: + +i) changes to the Program, and + +ii) additions to the Program; + +where such changes and/or additions to the Program originate from and are +distributed by that particular Contributor. A Contribution 'originates' from +a Contributor if it was added to the Program by such Contributor itself or +anyone acting on such Contributor's behalf. Contributions do not include +additions to the Program which: (i) are separate modules of software +distributed in conjunction with the Program under their own license +agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + +a) Subject to the terms of this Agreement, each Contributor hereby grants +Recipient a non-exclusive, worldwide, royalty-free copyright license to +reproduce, prepare derivative works of, publicly display, publicly perform, +distribute and sublicense the Contribution of such Contributor, if any, and +such derivative works, in source code and object code form. + +b) Subject to the terms of this Agreement, each Contributor hereby grants +Recipient a non-exclusive, worldwide, royalty-free patent license under +Licensed Patents to make, use, sell, offer to sell, import and otherwise +transfer the Contribution of such Contributor, if any, in source code and +object code form. This patent license shall apply to the combination of the +Contribution and the Program if, at the time the Contribution is added by the +Contributor, such addition of the Contribution causes such combination to be +covered by the Licensed Patents. The patent license shall not apply to any +other combinations which include the Contribution. No hardware per se is +licensed hereunder. + +c) Recipient understands that although each Contributor grants the licenses +to its Contributions set forth herein, no assurances are provided by any +Contributor that the Program does not infringe the patent or other +intellectual property rights of any other entity. Each Contributor disclaims +any liability to Recipient for claims brought by any other entity based on +infringement of intellectual property rights or otherwise. As a condition to +exercising the rights and licenses granted hereunder, each Recipient hereby +assumes sole responsibility to secure any other intellectual property rights +needed, if any. For example, if a third party patent license is required to +allow Recipient to distribute the Program, it is Recipient's responsibility +to acquire that license before distributing the Program. + +d) Each Contributor represents that to its knowledge it has sufficient +copyright rights in its Contribution, if any, to grant the copyright license +set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + +a) it complies with the terms and conditions of this Agreement; and + +b) its license agreement: + +i) effectively disclaims on behalf of all Contributors all warranties and +conditions, express and implied, including warranties or conditions of title +and non-infringement, and implied warranties or conditions of merchantability +and fitness for a particular purpose; + +ii) effectively excludes on behalf of all Contributors all liability for +damages, including direct, indirect, special, incidental and consequential +damages, such as lost profits; + +iii) states that any provisions which differ from this Agreement are offered +by that Contributor alone and not by any other party; and + +iv) states that source code for the Program is available from such +Contributor, and informs licensees how to obtain it in a reasonable manner on +or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + +a) it must be made available under this Agreement; and + +b) a copy of this Agreement must be included with each copy of the Program. + +Contributors may not remove or alter any copyright notices contained within +the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if any, in a manner that reasonably allows subsequent Recipients to identify +the originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a +manner which does not create potential liability for other Contributors. +Therefore, if a Contributor includes the Program in a commercial product +offering, such Contributor ("Commercial Contributor") hereby agrees to defend +and indemnify every other Contributor ("Indemnified Contributor") against any +losses, damages and costs (collectively "Losses") arising from claims, +lawsuits and other legal actions brought by a third party against the +Indemnified Contributor to the extent caused by the acts or omissions of such +Commercial Contributor in connection with its distribution of the Program in +a commercial product offering. The obligations in this section do not apply +to any claims or Losses relating to any actual or alleged intellectual +property infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim +at its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON +AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER +EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR +CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A +PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the +appropriateness of using and distributing the Program and assumes all risks +associated with its exercise of rights under this Agreement , including but +not limited to the risks and costs of program errors, compliance with +applicable laws, damage to or loss of data, programs or equipment, and +unavailability or interruption of operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and +does not cure such failure in a reasonable period of time after becoming +aware of such noncompliance. If all Recipient's rights under this Agreement +terminate, Recipient agrees to cease use and distribution of the Program as +soon as reasonably practicable. However, Recipient's obligations under this +Agreement and any licenses granted by Recipient relating to the Program shall +continue and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of +the Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this +Agreement, whether expressly, by implication, estoppel or otherwise. All +rights in the Program not expressly granted under this Agreement are +reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial +in any resulting litigation. diff --git a/README.md b/README.md index 0deacbb..eaa4ec9 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,80 @@ # clojure-game-geek -Example code for the [Lacinia](https://github.com/walmartlabs/lacinia) GraphQL framework tutorial. +FIXME: my new application. -The tutorial is available at http://lacinia.readthedocs.io/en/latest/tutorial/ +## Installation + +Download from https://github.com/my/clojure-game-geek + +## Usage + +FIXME: explanation + +Run the project directly, via `:exec-fn`: + + $ clojure -X:run-x + Hello, Clojure! + +Run the project, overriding the name to be greeted: + + $ clojure -X:run-x :name '"Someone"' + Hello, Someone! + +Run the project directly, via `:main-opts` (`-m my.clojure-game-geek`): + + $ clojure -M:run-m + Hello, World! + +Run the project, overriding the name to be greeted: + + $ clojure -M:run-m Via-Main + Hello, Via-Main! + +Run the project's tests (they'll fail until you edit them): + + $ clojure -T:build test + +Run the project's CI pipeline and build an uberjar (this will fail until you edit the tests to pass): + + $ clojure -T:build ci + +This will produce an updated `pom.xml` file with synchronized dependencies inside the `META-INF` +directory inside `target/classes` and the uberjar in `target`. You can update the version (and SCM tag) +information in generated `pom.xml` by updating `build.clj`. + +If you don't want the `pom.xml` file in your project, you can remove it. The `ci` task will +still generate a minimal `pom.xml` as part of the `uber` task, unless you remove `version` +from `build.clj`. + +Run that uberjar: + + $ java -jar target/clojure-game-geek-0.1.0-SNAPSHOT.jar + +If you remove `version` from `build.clj`, the uberjar will become `target/clojure-game-geek-standalone.jar`. + +## Options + +FIXME: listing of options this app accepts. + +## Examples + +... + +### Bugs + +... + +### Any Other Sections +### That You Think +### Might be Useful ## License -Copyright © 2017 Walmart +Copyright © 2022 Howard.lewisship -Released under the terms of the Apache Software License 2.0. +_EPLv1.0 is just the default for projects generated by `clj-new`: you are not_ +_required to open source this project, nor are you required to use EPLv1.0!_ +_Feel free to remove or change the `LICENSE` file and remove or update this_ +_section of the `README.md` file!_ +Distributed under the Eclipse Public License version 1.0. diff --git a/bin/docker-down.sh b/bin/docker-down.sh deleted file mode 100755 index 4a3644a..0000000 --- a/bin/docker-down.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -docker-compose -p cgg down diff --git a/bin/docker-up.sh b/bin/docker-up.sh deleted file mode 100755 index 5e9d24c..0000000 --- a/bin/docker-up.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash - -docker-compose -p cgg up -d - diff --git a/bin/psql.sh b/bin/psql.sh index a729c90..47c936b 100755 --- a/bin/psql.sh +++ b/bin/psql.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -docker exec -ti --user postgres cgg_db_1 psql -Ucgg_role cggdb +docker exec -ti --user postgres clojure-game-geek-db-1 psql -Ucgg_role cggdb diff --git a/bin/setup-db.sh b/bin/setup-db.sh index 09327dd..9db83ad 100755 --- a/bin/setup-db.sh +++ b/bin/setup-db.sh @@ -1,12 +1,13 @@ #!/usr/bin/env bash -docker exec -i --user postgres cgg_db_1 createdb cggdb +docker exec -i --user postgres clojure-game-geek-db-1 createdb cggdb -docker exec -i --user postgres cgg_db_1 psql cggdb -a <<__END +docker exec -i --user postgres clojure-game-geek-db-1 psql cggdb -a <<__END create user cgg_role password 'lacinia'; +grant create on schema public to cgg_role; __END -docker exec -i cgg_db_1 psql -Ucgg_role cggdb -a <<__END +docker exec -i clojure-game-geek-db-1 psql -Ucgg_role cggdb -a <<__END drop table if exists designer_to_game; drop table if exists game_rating; drop table if exists member; @@ -20,7 +21,7 @@ BEGIN RETURN NEW; END; \$\$ language 'plpgsql'; - + create table member ( member_id int generated by default as identity primary key, name text not null, @@ -28,9 +29,9 @@ create table member ( updated_at timestamp not null default current_timestamp); create trigger member_updated_at before update -on member for each row execute procedure +on member for each row execute procedure mantain_updated_at(); - + create table board_game ( game_id int generated by default as identity primary key, name text not null, @@ -41,9 +42,9 @@ create table board_game ( updated_at timestamp not null default current_timestamp); create trigger board_game_updated_at before update -on board_game for each row execute procedure +on board_game for each row execute procedure mantain_updated_at(); - + create table designer ( designer_id int generated by default as identity primary key, name text not null, @@ -52,21 +53,19 @@ create table designer ( updated_at timestamp not null default current_timestamp); create trigger designer_updated_at before update -on designer for each row execute procedure +on designer for each row execute procedure mantain_updated_at(); - + create table game_rating ( game_id int references board_game(game_id), member_id int references member(member_id), rating integer not null, created_at timestamp not null default current_timestamp, - updated_at timestamp not null default current_timestamp); - -alter table game_rating add constraint cost_unique__game_rating_game_id_member_id -unique (game_id, member_id); + updated_at timestamp not null default current_timestamp, + primary key (game_id, member_id)); create trigger game_rating_updated_at before update -on game_rating for each row execute procedure +on game_rating for each row execute procedure mantain_updated_at(); create table designer_to_game ( diff --git a/build.clj b/build.clj new file mode 100644 index 0000000..577a3e8 --- /dev/null +++ b/build.clj @@ -0,0 +1,17 @@ +(ns build + (:refer-clojure :exclude [test]) + (:require [org.corfield.build :as bb])) + +(def lib 'net.clojars.my/clojure-game-geek) +(def version "0.1.0-SNAPSHOT") +(def main 'my.clojure-game-geek) + +(defn test "Run the tests." [opts] + (bb/run-tests (assoc opts :aliases [:dev]))) + +(defn ci "Run the CI pipeline of tests (and build the uberjar)." [opts] + (-> opts + (assoc :lib lib :version version :main main) + (bb/run-tests) + (bb/clean) + (bb/uber))) diff --git a/deps.edn b/deps.edn new file mode 100644 index 0000000..397bd2b --- /dev/null +++ b/deps.edn @@ -0,0 +1,25 @@ +{:paths ["src" "resources"] + :deps {org.clojure/clojure {:mvn/version "1.11.1"} + com.walmartlabs/lacinia {:mvn/version "1.2-alpha-4"} + com.walmartlabs/lacinia-pedestal {:mvn/version "1.1"} + org.clojure/java.jdbc {:mvn/version "0.7.12"} + org.postgresql/postgresql {:mvn/version "42.5.1"} + com.mchange/c3p0 {:mvn/version "0.9.5.5"} + com.stuartsierra/component {:mvn/version "1.1.0"} + io.aviso/logging {:mvn/version "1.0"}} + :aliases + {:run-m {:main-opts ["-m" "my.clojure-game-geek"]} + :run-x {:ns-default my.clojure-game-geek + :exec-fn greet + :exec-args {:name "Clojure"}} + :build {:deps {io.github.seancorfield/build-clj + {:git/tag "v0.8.2" :git/sha "0ffdb4c" + ;; since we're building an app uberjar, we do not + ;; need deps-deploy for clojars.org deployment: + :deps/root "slim"}} + :ns-default build} + :dev {:extra-paths ["dev-resources"]} + :test {:extra-paths ["test"] + :extra-deps {org.clojure/test.check {:mvn/version "1.1.1"} + io.github.cognitect-labs/test-runner + {:git/tag "v0.5.0" :git/sha "48c3c67"}}}}} diff --git a/dev-resources/cgg-data.edn b/dev-resources/cgg-data.edn new file mode 100644 index 0000000..27c278d --- /dev/null +++ b/dev-resources/cgg-data.edn @@ -0,0 +1,55 @@ +{:games + [{:id "1234" + :name "Zertz" + :summary "Two player abstract with forced moves and shrinking board" + :designers #{"200"} + :minPlayers 2 + :maxPlayers 2} + {:id "1235" + :name "Dominion" + :summary "Created the deck-building genre; zillions of expansions" + :designers #{"204"} + :minPlayers 2} + {:id "1236" + :name "Tiny Epic Galaxies" + :summary "Fast dice-based sci-fi space game with a bit of chaos" + :designers #{"203"} + :minPlayers 1 + :maxPlayers 4} + {:id "1237" + :name "7 Wonders: Duel" + :summary "Tense, quick card game of developing civilizations" + :designers #{"201" "202"} + :minPlayers 2 + :maxPlayers 2}] + + :members + [{:id "37" + :name "curiousattemptbunny"} + {:id "1410" + :name "bleedingedge"} + {:id "2812" + :name "missyo"}] + + :ratings + [{:member-id "37" :game-id "1234" :rating 3} + {:member-id "1410" :game-id "1234" :rating 5} + {:member-id "1410" :game-id "1236" :rating 4} + {:member-id "1410" :game-id "1237" :rating 4} + {:member-id "2812" :game-id "1237" :rating 4} + {:member-id "37" :game-id "1237" :rating 5}] + + :designers + [{:id "200" + :name "Kris Burm" + :url "http://www.gipf.com/project_gipf/burm/burm.html"} + {:id "201" + :name "Antoine Bauza" + :url "http://www.antoinebauza.fr/"} + {:id "202" + :name "Bruno Cathala" + :url "http://www.brunocathala.com/"} + {:id "203" + :name "Scott Almes"} + {:id "204" + :name "Donald X. Vaccarino"}]} diff --git a/dev-resources/logback-test.xml b/dev-resources/logback-test.xml index 0e16d91..6226c20 100644 --- a/dev-resources/logback-test.xml +++ b/dev-resources/logback-test.xml @@ -1,15 +1,15 @@ - - - %-5level %logger - %msg%n - - + + + %-5level %logger - %msg%n + + - - - + + + - + diff --git a/dev-resources/clojure_game_geek/test_utils.clj b/dev-resources/my/clojure_game_geek/test_utils.clj similarity index 76% rename from dev-resources/clojure_game_geek/test_utils.clj rename to dev-resources/my/clojure_game_geek/test_utils.clj index 1d2ad90..ec5e11e 100644 --- a/dev-resources/clojure_game_geek/test_utils.clj +++ b/dev-resources/my/clojure_game_geek/test_utils.clj @@ -1,8 +1,6 @@ -(ns clojure-game-geek.test-utils - (:require - [clojure.walk :as walk]) - (:import - (clojure.lang IPersistentMap))) +(ns my.clojure-game-geek.test-utils + (:require [clojure.walk :as walk]) + (:import (clojure.lang IPersistentMap))) (defn simplify "Converts all ordered maps nested within the map into standard hash maps, and @@ -20,4 +18,3 @@ :else node)) m)) - diff --git a/dev-resources/user.clj b/dev-resources/user.clj index ad6be99..3a3388b 100644 --- a/dev-resources/user.clj +++ b/dev-resources/user.clj @@ -1,12 +1,12 @@ (ns user - (:require - [com.walmartlabs.lacinia :as lacinia] - [clojure.java.browse :refer [browse-url]] - [clojure-game-geek.system :as system] - [clojure-game-geek.test-utils :refer [simplify]] - [com.stuartsierra.component :as component])) + (:require [com.stuartsierra.component :as component] + [my.clojure-game-geek.db :as db] + [my.clojure-game-geek.system :as system] + [com.walmartlabs.lacinia :as lacinia] + [clojure.java.browse :refer [browse-url]] + [my.clojure-game-geek.test-utils :refer [simplify]])) -(defonce system nil) +(defonce system (system/new-system)) (defn q [query-string] @@ -18,20 +18,29 @@ (defn start [] - (alter-var-root #'system (fn [_] - (-> (system/new-system) - component/start-system))) - (browse-url "http://localhost:8888/") + (alter-var-root #'system component/start-system) + (browse-url "http://localhost:8888/ide") :started) (defn stop [] - (when (some? system) - (component/stop-system system) - (alter-var-root #'system (constantly nil))) + (alter-var-root #'system component/stop-system) :stopped) (comment (start) (stop) + + (def db (:db system)) + + (require '[my.clojure-game-geek.db :as db]) + + (db/find-member-by-id db 37) + (db/list-designers-for-game db 1237) + (db/list-games-for-designer db 201) + (db/list-ratings-for-game db 1234) + (db/list-ratings-for-member db 1410) + (db/upsert-game-rating db 1237 1410 3) + (db/upsert-game-rating db 1234 2812 4) + ) diff --git a/doc/intro.md b/doc/intro.md new file mode 100644 index 0000000..fa57f84 --- /dev/null +++ b/doc/intro.md @@ -0,0 +1,3 @@ +# Introduction to clojure-game-geek + +TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) diff --git a/docker-compose.yml b/docker-compose.yml index 059f729..452c267 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,7 @@ version: '3' services: db: ports: - - 25432:5432 - image: postgres:10.2-alpine - + - 25432:5432 + image: postgres:15.1-alpine + environment: + POSTGRES_PASSWORD: supersecret diff --git a/project.clj b/project.clj deleted file mode 100644 index 20689b9..0000000 --- a/project.clj +++ /dev/null @@ -1,14 +0,0 @@ -(defproject clojure-game-geek "0.1.0-SNAPSHOT" - :description "A tiny BoardGameGeek clone written in Clojure with Lacinia" - :url "https://github.com/walmartlabs/clojure-game-geek" - :license {:name "Eclipse Public License" - :url "http://www.eclipse.org/legal/epl-v10.html"} - :dependencies [[org.clojure/clojure "1.9.0"] - [com.stuartsierra/component "0.3.2"] - [com.walmartlabs/lacinia "0.30.0"] - [com.walmartlabs/lacinia-pedestal "0.10.0"] - [org.clojure/java.jdbc "0.7.8"] - [org.postgresql/postgresql "42.2.5.jre7"] - [com.mchange/c3p0 "0.9.5.2"] - [org.flatland/ordered "1.5.7"] - [io.aviso/logging "0.3.1"]]) diff --git a/resources/cgg-schema.edn b/resources/cgg-schema.edn index a810e09..b006d2e 100644 --- a/resources/cgg-schema.edn +++ b/resources/cgg-schema.edn @@ -2,87 +2,79 @@ {:BoardGame {:description "A physical or virtual board game." :fields - {:game_id {:type (non-null Int)} - :name {:type (non-null String)} - :rating_summary {:type (non-null :GameRatingSummary) - :resolve :BoardGame/rating-summary} - :summary {:type String - :description "A one-line summary of the game."} - :description {:type String - :description "A long-form description of the game."} - :designers {:type (non-null (list :Designer)) - :description "Designers who contributed to the game." - :resolve :BoardGame/designers} - :min_players {:type Int - :description "The minimum number of players the game supports."} - :max_players {:type Int - :description "The maximum number of players the game supports."} - :play_time {:type Int - :description "Play time, in minutes, for a typical game."}}} + {:id {:type (non-null Int)} + :name {:type (non-null String)} + :summary {:type String + :description "A one-line summary of the game."} + :ratingSummary {:type (non-null :GameRatingSummary) + :description "Summarizes member ratings for the game."} + :description {:type String + :description "A long-form description of the game."} + :designers {:type (non-null (list :Designer)) + :description "Designers who contributed to the game."} + :minPlayers {:type Int + :description "The minimum number of players the game supports."} + :maxPlayers {:type Int + :description "The maximum number of players the game supports."} + :playTime {:type Int + :description "Play time, in minutes, for a typical game."}}} :GameRatingSummary {:description "Summary of ratings for a single game." :fields - {:count {:type (non-null Int) - :description "Number of ratings provided for the game. Ratings are 1 to 5 stars."} - :average {:type (non-null Float) + {:count {:type (non-null Int) + :description "Number of ratings provided for the game. Ratings are 1 to 5 stars."} + :average {:type (non-null Float) :description "The average value of all ratings, or 0 if never rated."}}} :Member {:description "A member of Clojure Game Geek. Members can rate games." :fields - {:member_id {:type (non-null Int)} - :name {:type (non-null String) - :description "Unique name of member."} - :ratings {:type (list :GameRating) - :description "List of games and ratings provided by this member." - :resolve :Member/ratings}}} + {:id {:type (non-null Int)} + :name {:type (non-null String) + :description "Unique name of the member."} + :ratings {:type (list :GameRating) + :description "List of games and ratings provided by this member."}}} :GameRating {:description "A member's rating of a particular game." :fields - {:game {:type (non-null :BoardGame) - :description "The Game rated by the member." - :resolve :GameRating/game} - :rating {:type (non-null Int) + {:game {:type (non-null :BoardGame) + :description "The Game rated by the member."} + :rating {:type (non-null Int) :description "The rating as 1 to 5 stars."}}} :Designer {:description "A person who may have contributed to a board game design." :fields - {:designer_id {:type (non-null Int)} - :name {:type (non-null String)} - :url {:type String - :description "Home page URL, if known."} - :games {:type (non-null (list :BoardGame)) - :description "Games designed by this designer." - :resolve :Designer/games}}}} + {:id {:type (non-null Int)} + :name {:type (non-null String)} + :url {:type String + :description "Home page URL, if known."} + :games {:type (non-null (list :BoardGame)) + :description "Games designed by this designer."}}} - :queries - {:game_by_id - {:type :BoardGame - :description "Select a BoardGame by its unique id, if it exists." - :args - {:id {:type (non-null Int)}} - :resolve :query/game-by-id} + :Query + {:fields + {:gameById + {:type :BoardGame + :description "Access a BoardGame by its unique id, if it exists." + :args + {:id {:type Int}}} - :member_by_id - {:type :Member - :description "Select a ClojureGameGeek Member by their unique id, if it exists." - :args - {:id {:type (non-null Int)}} - :resolve :query/member-by-id}} + :memberById + {:type :Member + :description "Access a ClojureGameGeek Member by their unique id, if it exists." + :args + {:id {:type (non-null Int)}}}}} - :mutations - {:rate_game - {:type :BoardGame - :description "Establishes a rating of a board game, by a Member. - - On success (the game and member both exist), selects the BoardGame. - Otherwise, selects nil and an error." - :args - {:game_id {:type (non-null Int)} - :member_id {:type (non-null Int)} - :rating {:type (non-null Int) - :description "Game rating as a number between 1 and 5."}} - :resolve :mutation/rate-game}}} + :Mutation + {:fields + {:rateGame + {:type :BoardGame + :description "Establishes a rating of a board game, by a Member." + :args + {:gameId {:type (non-null Int)} + :memberId {:type (non-null Int)} + :rating {:type (non-null Int) + :description "Game rating as number between 1 and 5."}}}}}}} diff --git a/src/clojure_game_geek/db.clj b/src/clojure_game_geek/db.clj deleted file mode 100644 index 0a3c1e7..0000000 --- a/src/clojure_game_geek/db.clj +++ /dev/null @@ -1,107 +0,0 @@ -(ns clojure-game-geek.db - (:require - [com.stuartsierra.component :as component] - [io.pedestal.log :as log] - [clojure.java.jdbc :as jdbc] - [clojure.string :as str]) - (:import (com.mchange.v2.c3p0 ComboPooledDataSource))) - -(defn ^:private pooled-data-source - [host dbname user password port] - {:datasource - (doto (ComboPooledDataSource.) - (.setDriverClass "org.postgresql.Driver") - (.setJdbcUrl (str "jdbc:postgresql://" host ":" port "/" dbname)) - (.setUser user) - (.setPassword password))}) - -(defrecord ClojureGameGeekDb [ds] - - component/Lifecycle - - (start [this] - (assoc this - :ds (pooled-data-source "localhost" "cggdb" "cgg_role" "lacinia" 25432))) - - (stop [this] - (-> ds :datasource .close) - (assoc this :ds nil))) - -(defn new-db - [] - {:db (map->ClojureGameGeekDb {})}) - - -(defn ^:private query - [component statement] - (let [[sql & params] statement] - (log/debug :sql (str/replace sql #"\s+" " ") - :params params)) - (jdbc/query (:ds component) statement)) - -(defn ^:private execute! - [component statement] - (let [[sql & params] statement] - (log/debug :sql (str/replace sql #"\s+" " ") - :params params)) - (jdbc/execute! (:ds component) statement)) - -(defn find-game-by-id - [component game-id] - (first - (query component - ["select game_id, name, summary, min_players, max_players, created_at, updated_at - from board_game where game_id = ?" game-id]))) - -(defn find-member-by-id - [component member-id] - (first - (query component - ["select member_id, name, created_at, updated_at - from member - where member_id = ?" member-id]))) - -(defn list-designers-for-game - [component game-id] - (query component - ["select d.designer_id, d.name, d.uri, d.created_at, d.updated_at - from designer d - inner join designer_to_game j on (d.designer_id = j.designer_id) - where j.game_id = ? - order by d.name" game-id])) - -(defn list-games-for-designer - [component designer-id] - (query component - ["select g.game_id, g.name, g.summary, g.min_players, g.max_players, g.created_at, g.updated_at - from board_game g - inner join designer_to_game j on (g.game_id = j.game_id) - where j.designer_id = ? - order by g.name" designer-id])) - -(defn list-ratings-for-game - [component game-id] - (query component - ["select game_id, member_id, rating, created_at, updated_at - from game_rating - where game_id = ?" game-id])) - -(defn list-ratings-for-member - [component member-id] - (query component - ["select game_id, member_id, rating, created_at, updated_at - from game_rating - where member_id = ?" member-id])) - -(defn upsert-game-rating - "Adds a new game rating, or changes the value of an existing game rating. - - Returns nil" - [component game-id member-id rating] - (execute! component - ["insert into game_rating (game_id, member_id, rating) - values (?, ?, ?) - on conflict (game_id, member_id) do update set rating = ?" - game-id member-id rating rating]) - - nil) diff --git a/src/clojure_game_geek/schema.clj b/src/clojure_game_geek/schema.clj deleted file mode 100644 index c0bf768..0000000 --- a/src/clojure_game_geek/schema.clj +++ /dev/null @@ -1,113 +0,0 @@ -(ns clojure-game-geek.schema - "Contains custom resolvers and a function to provide the full schema." - (:require - [clojure.java.io :as io] - [com.walmartlabs.lacinia.util :as util] - [com.walmartlabs.lacinia.resolve :refer [resolve-as]] - [com.stuartsierra.component :as component] - [clojure-game-geek.db :as db] - [clojure.edn :as edn] - [com.walmartlabs.lacinia.schema :as schema])) - -(defn game-by-id - [db] - (fn [_ args _] - (db/find-game-by-id db (:id args)))) - -(defn member-by-id - [db] - (fn [_ args _] - (db/find-member-by-id db (:id args)))) - -(defn rate-game - [db] - (fn [_ args _] - (let [{game-id :game_id - member-id :member_id - rating :rating} args - game (db/find-game-by-id db game-id) - member (db/find-member-by-id db member-id)] - (cond - (nil? game) - (resolve-as nil {:message "Game not found." - :status 404}) - - (nil? member) - (resolve-as nil {:message "Member not found." - :status 404}) - - (not (<= 1 rating 5)) - (resolve-as nil {:message "Rating must be between 1 and 5." - :status 400}) - - :else - (do - (db/upsert-game-rating db game-id member-id rating) - game))))) - -(defn board-game-designers - [db] - (fn [_ _ board-game] - (db/list-designers-for-game db (:game_id board-game)))) - -(defn designer-games - [db] - (fn [_ _ designer] - (db/list-games-for-designer db (:designer_id designer)))) - -(defn rating-summary - [db] - (fn [_ _ board-game] - (let [ratings (map :rating (db/list-ratings-for-game db (:game_id board-game))) - n (count ratings)] - {:count n - :average (if (zero? n) - 0 - (/ (apply + ratings) - (float n)))}))) - -(defn member-ratings - [db] - (fn [_ _ member] - (db/list-ratings-for-member db (:member_id member)))) - -(defn game-rating->game - [db] - (fn [_ _ game-rating] - (db/find-game-by-id db (:game_id game-rating)))) - -(defn resolver-map - [component] - (let [db (:db component)] - {:query/game-by-id (game-by-id db) - :query/member-by-id (member-by-id db) - :mutation/rate-game (rate-game db) - :BoardGame/designers (board-game-designers db) - :BoardGame/rating-summary (rating-summary db) - :GameRating/game (game-rating->game db) - :Designer/games (designer-games db) - :Member/ratings (member-ratings db)})) - -(defn load-schema - [component] - (-> (io/resource "cgg-schema.edn") - slurp - edn/read-string - (util/attach-resolvers (resolver-map component)) - schema/compile)) - -(defrecord SchemaProvider [schema] - - component/Lifecycle - - (start [this] - (assoc this :schema (load-schema this))) - - (stop [this] - (assoc this :schema nil))) - -(defn new-schema-provider - [] - {:schema-provider (-> {} - map->SchemaProvider - (component/using [:db]))}) diff --git a/src/clojure_game_geek/system.clj b/src/clojure_game_geek/system.clj deleted file mode 100644 index 467ea4c..0000000 --- a/src/clojure_game_geek/system.clj +++ /dev/null @@ -1,13 +0,0 @@ -(ns clojure-game-geek.system - (:require - [com.stuartsierra.component :as component] - [clojure-game-geek.schema :as schema] - [clojure-game-geek.server :as server] - [clojure-game-geek.db :as db])) - -(defn new-system - [] - (merge (component/system-map) - (server/new-server) - (schema/new-schema-provider) - (db/new-db))) diff --git a/src/my/clojure_game_geek.clj b/src/my/clojure_game_geek.clj new file mode 100644 index 0000000..0a44dc4 --- /dev/null +++ b/src/my/clojure_game_geek.clj @@ -0,0 +1,12 @@ +(ns my.clojure-game-geek + (:gen-class)) + +(defn greet + "Callable entry point to the application." + [data] + (println (str "Hello, " (or (:name data) "World") "!"))) + +(defn -main + "I don't do a whole lot ... yet." + [& args] + (greet {:name (first args)})) diff --git a/src/my/clojure_game_geek/db.clj b/src/my/clojure_game_geek/db.clj new file mode 100644 index 0000000..5be797e --- /dev/null +++ b/src/my/clojure_game_geek/db.clj @@ -0,0 +1,133 @@ +(ns my.clojure-game-geek.db + (:require [clojure.java.jdbc :as jdbc] + [io.pedestal.log :as log] + [clojure.string :as string] + [clojure.set :as set] + [com.stuartsierra.component :as component]) + (:import (com.mchange.v2.c3p0 ComboPooledDataSource))) + +(defn- pooled-data-source + [host dbname user password port] + (doto (ComboPooledDataSource.) + (.setDriverClass "org.postgresql.Driver") + (.setJdbcUrl (str "jdbc:postgresql://" host ":" port "/" dbname)) + (.setUser user) + (.setPassword password))) + +(defrecord ClojureGameGeekDb [^ComboPooledDataSource datasource] + + component/Lifecycle + + (start [this] + (assoc this :datasource (pooled-data-source "localhost" "cggdb" "cgg_role" "lacinia" 25432))) + + (stop [this] + (.close datasource) + (assoc this :datasource nil))) + +(defn- query + [component statement] + (let [[sql & params] statement] + (log/debug :sql (string/replace sql #"\s+" " ") + :params params)) + (jdbc/query component statement)) + +(defn- execute! + [component statement] + (let [[sql & params] statement] + (log/debug :sql (string/replace sql #"\s+" " ") + :params params)) + (jdbc/execute! component statement)) + +(defn- remap-board-game + [row-data] + (set/rename-keys row-data {:game_id :id + :min_players :minPlayers + :max_players :maxPlayers + :created_at :createdAt + :updated_at :updatedAt})) + +(defn- remap-member + [row-data] + (set/rename-keys row-data {:member_id :id + :created_at :createdAt + :updated_at :updatedAt})) + +(defn- remap-designer + [row-data] + (set/rename-keys row-data {:designer_id :id + :created_at :createdAt + :updated_at :updatedAt})) + +(defn- remap-rating + [row-data] + (set/rename-keys row-data {:member_id :member-id + :game_id :game-id + :created_at :createdAt + :updated_at :updatedAt})) + +(defn find-game-by-id + [component game-id] + (-> (query component + ["select game_id, name, summary, min_players, max_players, created_at, updated_at + from board_game where game_id = ?" game-id]) + first + remap-board-game)) + +(defn find-member-by-id + [component member-id] + (-> (query component + ["select member_id, name, created_at, updated_at + from member + where member_id = ?" member-id]) + first + remap-member)) + +(defn list-designers-for-game + [component game-id] + (->> (query component + ["select d.designer_id, d.name, d.uri, d.created_at, d.updated_at + from designer d + inner join designer_to_game j on (d.designer_id = j.designer_id) + where j.game_id = ? + order by d.name" game-id]) + (map remap-designer))) + +(defn list-games-for-designer + [component designer-id] + (->> (query component + ["select g.game_id, g.name, g.summary, g.min_players, g.max_players, g.created_at, + g.updated_at + from board_game g + inner join designer_to_game j on (g.game_id = j.game_id) + where j.designer_id = ? + order by g.name" designer-id]) + (map remap-board-game))) + +(defn list-ratings-for-game + [component game-id] + (->> (query component + ["select game_id, member_id, rating, created_at, updated_at + from game_rating + where game_id = ?" game-id]) + (map remap-rating))) + +(defn list-ratings-for-member + [component member-id] + (->> (query component + ["select game_id, member_id, rating, created_at, updated_at + from game_rating + where member_id = ?" member-id]) + (map remap-rating))) + +(defn upsert-game-rating + "Adds a new game rating, or changes the value of an existing game rating. + + Returns nil." + [component game-id member-id rating] + (execute! component + ["insert into game_rating (game_id, member_id, rating) + values (?, ?, ?) + on conflict (game_id, member_id) do update set rating = ?" + game-id member-id rating rating]) + nil) diff --git a/src/my/clojure_game_geek/schema.clj b/src/my/clojure_game_geek/schema.clj new file mode 100644 index 0000000..6bba06e --- /dev/null +++ b/src/my/clojure_game_geek/schema.clj @@ -0,0 +1,106 @@ +(ns my.clojure-game-geek.schema + "Contains custom resolvers and a function to provide the full schema." + (:require [clojure.java.io :as io] + [com.stuartsierra.component :as component] + [com.walmartlabs.lacinia.util :as util] + [com.walmartlabs.lacinia.schema :as schema] + [com.walmartlabs.lacinia.resolve :refer [resolve-as]] + [my.clojure-game-geek.db :as db] + [clojure.edn :as edn])) + +(defn game-by-id + [db] + (fn [_ args _] + (db/find-game-by-id db (:id args)))) + +(defn member-by-id + [db] + (fn [_ args _] + (db/find-member-by-id db (:id args)))) + +(defn board-game-designers + [db] + (fn [_ _ board-game] + (db/list-designers-for-game db (:id board-game)))) + +(defn designer-games + [db] + (fn [_ _ designer] + (db/list-games-for-designer db (:id designer)))) + +(defn rating-summary + [db] + (fn [_ _ board-game] + (let [ratings (map :rating (db/list-ratings-for-game db (:id board-game))) + n (count ratings)] + {:count n + :average (if (zero? n) + 0 + (/ (apply + ratings) + (float n)))}))) + +(defn member-ratings + [db] + (fn [_ _ member] + (db/list-ratings-for-member db (:id member)))) + +(defn game-rating->game + [db] + (fn [_ _ game-rating] + (db/find-game-by-id db (:game-id game-rating)))) + +(defn rate-game + [db] + (fn [_ args _] + (let [{game-id :gameId + member-id :memberId + rating :rating} args + game (db/find-game-by-id db game-id) + member (db/find-member-by-id db member-id)] + (cond + (nil? game) + (resolve-as nil {:message "Game not found" + :status 404}) + + (nil? member) + (resolve-as nil {:message "Member not found" + :status 404}) + + (not (<= 1 rating 5)) + (resolve-as nil {:message "Rating must be between 1 and 5" + :status 400}) + + :else + (do + (db/upsert-game-rating db game-id member-id rating) + game))))) + +(defn resolver-map + [component] + (let [{:keys [db]} component] + {:Query/gameById (game-by-id db) + :Query/memberById (member-by-id db) + :Mutation/rateGame (rate-game db) + :BoardGame/designers (board-game-designers db) + :BoardGame/ratingSummary (rating-summary db) + :Designer/games (designer-games db) + :Member/ratings (member-ratings db) + :GameRating/game (game-rating->game db)})) + +(defn load-schema + [component] + (-> (io/resource "cgg-schema.edn") + slurp + edn/read-string + (util/inject-resolvers (resolver-map component)) + schema/compile)) + +(defrecord SchemaProvider [db schema] + + component/Lifecycle + + (start [this] + (assoc this :schema (load-schema this))) + + (stop [this] + (assoc this :schema nil))) diff --git a/src/clojure_game_geek/server.clj b/src/my/clojure_game_geek/server.clj similarity index 56% rename from src/clojure_game_geek/server.clj rename to src/my/clojure_game_geek/server.clj index 3497e0e..b6e99ab 100644 --- a/src/clojure_game_geek/server.clj +++ b/src/my/clojure_game_geek/server.clj @@ -1,26 +1,19 @@ -(ns clojure-game-geek.server +(ns my.clojure-game-geek.server (:require [com.stuartsierra.component :as component] - [com.walmartlabs.lacinia.pedestal :as lp] + [com.walmartlabs.lacinia.pedestal2 :as lp] [io.pedestal.http :as http])) (defrecord Server [schema-provider server port] component/Lifecycle + (start [this] (assoc this :server (-> schema-provider :schema - (lp/service-map {:graphiql true - :port port}) + (lp/default-service {:port port}) http/create-server http/start))) (stop [this] (http/stop server) (assoc this :server nil))) - -(defn new-server - [] - {:server (component/using (map->Server {:port 8888}) - [:schema-provider])}) - - diff --git a/src/my/clojure_game_geek/system.clj b/src/my/clojure_game_geek/system.clj new file mode 100644 index 0000000..9b7ff31 --- /dev/null +++ b/src/my/clojure_game_geek/system.clj @@ -0,0 +1,19 @@ +(ns my.clojure-game-geek.system + (:require [com.stuartsierra.component :as component] + [my.clojure-game-geek.schema :as schema] + [my.clojure-game-geek.server :as server] + [my.clojure-game-geek.db :as db])) + +(defn new-system + ([] + (new-system nil)) + ([opts] + (let [{:keys [port] + :or {port 8888}} opts] + (assoc (component/system-map) + :db (db/map->ClojureGameGeekDb {}) + :server (component/using (server/map->Server {:port port}) + [:schema-provider]) + :schema-provider (component/using + (schema/map->SchemaProvider {}) + [:db]))))) diff --git a/test/clojure_game_geek/system_tests.clj b/test/clojure_game_geek/system_tests.clj deleted file mode 100644 index 10c7d00..0000000 --- a/test/clojure_game_geek/system_tests.clj +++ /dev/null @@ -1,41 +0,0 @@ -(ns clojure-game-geek.system-tests - (:require - [clojure.test :refer [deftest is use-fixtures]] - [clojure-game-geek.system :as system] - [clojure-game-geek.test-utils :refer [simplify]] - [com.stuartsierra.component :as component] - [com.walmartlabs.lacinia :as lacinia])) - -(defn ^:private test-system - "Creates a new system suitable for testing, and ensures that - the HTTP port won't conflict with a default running system." - [] - (-> (system/new-system) - (assoc-in [:server :port] 8989))) - -(def ^:dynamic ^:private *system*) - -(use-fixtures :once - (fn [test-fn] - (binding [*system* (component/start-system (test-system))] - (try - (test-fn) - (finally - (component/stop-system *system*)))))) - -(defn ^:private q - "Extracts the compiled schema and executes a query." - [query variables] - (-> *system* - (get-in [:schema-provider :schema]) - (lacinia/execute query variables nil) - simplify)) - -(deftest can-read-board-game - (is (= {:data {:game_by_id {:max_players 2 - :min_players 2 - :name "Zertz" - :play_time nil - :summary "Two player abstract with forced moves and shrinking board"}}} - (q "{ game_by_id(id: 1234) { name summary min_players max_players play_time }}" - nil)))) diff --git a/test/my/clojure_game_geek/system_test.clj b/test/my/clojure_game_geek/system_test.clj new file mode 100644 index 0000000..da61e2e --- /dev/null +++ b/test/my/clojure_game_geek/system_test.clj @@ -0,0 +1,34 @@ +(ns my.clojure-game-geek.system-test + (:require [clojure.test :refer [deftest is]] + [com.stuartsierra.component :as component] + [com.walmartlabs.lacinia :as lacinia] + [my.clojure-game-geek.test-utils :refer [simplify]] + [my.clojure-game-geek.system :as system])) + +(defn- test-system + "Creates a new system suitable for testing, and ensures that + the HTTP port won't conflict with a default running system." + [] + (system/new-system {:port 8989})) + +(defn- q + "Extracts the compiled schema and executes a query." + [system query variables] + (-> system + (get-in [:schema-provider :schema]) + (lacinia/execute query variables nil) + simplify)) + +(deftest can-read-board-game + (let [system (component/start-system (test-system))] + (try + (is (= {:data {:gameById {:name "Zertz" + :summary "Two player abstract with forced moves and shrinking board" + :maxPlayers 2 + :minPlayers 2 + :playTime nil}}} + (q system + "{ gameById(id: 1234) { name summary minPlayers maxPlayers playTime }}" + nil))) + (finally + (component/stop-system system)))))