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 756b852..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,18 +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); + 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 3a510b7..6226c20 100644 --- a/dev-resources/logback-test.xml +++ b/dev-resources/logback-test.xml @@ -1,13 +1,15 @@ - - - %-5level %logger - %msg%n - - - - - - + + + %-5level %logger - %msg%n + + + + + + + + diff --git a/dev-resources/my/clojure_game_geek/test_utils.clj b/dev-resources/my/clojure_game_geek/test_utils.clj new file mode 100644 index 0000000..ec5e11e --- /dev/null +++ b/dev-resources/my/clojure_game_geek/test_utils.clj @@ -0,0 +1,20 @@ +(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 + 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)) diff --git a/dev-resources/user.clj b/dev-resources/user.clj index f62170f..3a3388b 100644 --- a/dev-resources/user.clj +++ b/dev-resources/user.clj @@ -1,30 +1,12 @@ (ns user - (:require - [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)) - -(defonce system nil) + (: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 (system/new-system)) (defn q [query-string] @@ -36,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 c3f857f..0000000 --- a/project.clj +++ /dev/null @@ -1,12 +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.8.0"] - [org.clojure/core.async "0.3.443"] - [com.stuartsierra/component "0.3.2"] - [com.walmartlabs/lacinia "0.25.0"] - [com.walmartlabs/lacinia-pedestal "0.7.0"] - [alaisi/postgres.async "0.8.0"] - [io.aviso/logging "0.2.0"]]) diff --git a/resources/cgg-schema.edn b/resources/cgg-schema.edn index 95e294c..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)} - :member_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 66fa888..0000000 --- a/src/clojure_game_geek/db.clj +++ /dev/null @@ -1,100 +0,0 @@ -(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! - [ch] - (let [v ( (query! (:conn 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)) - -(defn find-member-by-id - [component member-id] - (->> component - :db - deref - :members - (filter #(= member-id (:id %))) - 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 %)))))) - -(defn list-games-for-designer - [component designer-id] - (->> component - :db - deref - :games - (filter #(-> % :designers (contains? designer-id))))) - -(defn list-ratings-for-game - [component game-id] - (->> component - :db - deref - :ratings - (filter #(= game-id (: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}))) - -(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))) 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/clojure_game_geek/schema.clj b/src/my/clojure_game_geek/schema.clj similarity index 54% rename from src/clojure_game_geek/schema.clj rename to src/my/clojure_game_geek/schema.clj index 36c7991..6bba06e 100644 --- a/src/clojure_game_geek/schema.clj +++ b/src/my/clojure_game_geek/schema.clj @@ -1,13 +1,12 @@ -(ns clojure-game-geek.schema +(ns my.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])) + (: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] @@ -19,32 +18,6 @@ (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] @@ -60,7 +33,7 @@ (fn [_ _ board-game] (let [ratings (map :rating (db/list-ratings-for-game db (:id board-game))) n (count ratings)] - {:count n + {:count n :average (if (zero? n) 0 (/ (apply + ratings) @@ -74,29 +47,55 @@ (defn game-rating->game [db] (fn [_ _ game-rating] - (db/find-game-by-id db (:game_id 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 [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)})) + (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/attach-resolvers (resolver-map component)) + (util/inject-resolvers (resolver-map component)) schema/compile)) -(defrecord SchemaProvider [schema] +(defrecord SchemaProvider [db schema] component/Lifecycle @@ -105,9 +104,3 @@ (stop [this] (assoc this :schema nil))) - -(defn new-schema-provider - [] - {:schema-provider (-> {} - map->SchemaProvider - (component/using [:db]))}) diff --git a/src/clojure_game_geek/server.clj b/src/my/clojure_game_geek/server.clj similarity index 55% rename from src/clojure_game_geek/server.clj rename to src/my/clojure_game_geek/server.clj index 68f5f74..b6e99ab 100644 --- a/src/clojure_game_geek/server.clj +++ b/src/my/clojure_game_geek/server.clj @@ -1,25 +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] +(defrecord Server [schema-provider server port] component/Lifecycle + (start [this] (assoc this :server (-> schema-provider :schema - (lp/service-map {:graphiql true}) + (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 {}) - [: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/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)))))