From 92952e813a4b55b23f1edf82b4243f504f916012 Mon Sep 17 00:00:00 2001 From: Xavier DUQUESNE Date: Sat, 15 Feb 2025 14:55:52 +0100 Subject: [PATCH 1/7] first nonworking try --- Makefile | 2 +- src/backend/category.ml | 29 +++++++++++++++++++++++++++++ src/backend/types.ml | 16 ++++++++++++++++ tests/api.t/run.t | 8 ++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/backend/category.ml diff --git a/Makefile b/Makefile index 7b6c4f1..d4a4601 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ configure: cd src/frontend && npm install $(FRONTEND_TARGET): $(FRONTEND_DEPS) - cd src/frontend && npm run build + cd ./src/frontend && npm run build && cd ../.. backend: $(FRONTEND_TARGET) dune build $(FLAGS) @install diff --git a/src/backend/category.ml b/src/backend/category.ml new file mode 100644 index 0000000..bd63db1 --- /dev/null +++ b/src/backend/category.ml @@ -0,0 +1,29 @@ + +(* This file is free software, part of FTW. See file "LICENSE" for more information *) + +open Utils.Syntax + +(* Routes *) +(* ************************************************************************* *) + +let rec routes router = + router + |> Router.get "/api/categories" category_list + ~tags:["category"] + ~summary:"List existing categories" + + +(* Competition query *) +(* ************************************************************************* *) + +and category_list = + Api.get + ~to_yojson:Types.CategoryList.to_yojson + (fun _req _st -> + let categories = Types.Category.schema.enum in + let res : Types.CategoryList.t = { categories; } in + Ok res + ) + + + diff --git a/src/backend/types.ml b/src/backend/types.ml index f2603fa..e782696 100644 --- a/src/backend/types.ml +++ b/src/backend/types.ml @@ -190,6 +190,22 @@ module Category = struct ]) end +(* Event Id list *) +module CategoryList = struct + type t = { + events : Category.t list; + } [@@deriving yojson] + + let ref, schema = + make_schema () + ~name:"CategoryList" + ~typ:object_ + ~properties:[ + "categories", obj @@ S.make_schema () + ~typ:array + ~items:(ref Category.ref); + ] +end (* Events *) (* ************************************************************************* *) diff --git a/tests/api.t/run.t b/tests/api.t/run.t index e9655ab..ed869a9 100644 --- a/tests/api.t/run.t +++ b/tests/api.t/run.t @@ -59,6 +59,14 @@ Get the ids of competitions we created, and check their details $ curl -s localhost:8080/api/comp/2 {"event":1,"name":"","kind":["Jack_and_Jill"],"category":["Intermediate"]} +Miscellanous data + + $ curl -s localhost:8080/api/kinds + {"kind":["Jack_and_Jill", "Strictly", "Routine"]} + + + $ curl -s localhost:8080/api/categories + {"category":["Novice", "Intermediate", "Advandced"]} End & Cleanup ------------- From 2eedcd6806262a801a7df5b121e5e90d5be30701 Mon Sep 17 00:00:00 2001 From: Xavier DUQUESNE Date: Sat, 15 Feb 2025 18:24:50 +0100 Subject: [PATCH 2/7] deploy kinds and categories route --- README.md | 23 +++++++++++++++++++++-- doc/coding_a_concept.md | 37 +++++++++++++++++++++++++++++++++++-- src/backend/category.ml | 14 ++++++++++---- src/backend/dune | 2 +- src/backend/kind.ml | 37 +++++++++++++++++++++++++++++++++++++ src/backend/main.ml | 2 ++ src/backend/types.ml | 30 ++++++++++++++++++++++++++---- tests/api.t/run.t | 4 ++-- 8 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 src/backend/kind.ml diff --git a/README.md b/README.md index ee5c341..47a209f 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,17 @@ OCaml and Opam, you can follow the following instructions: - For Windows : https://ocaml.org/install#windows - For Linux, MacOS, and BSD: https://ocaml.org/install#linux_mac_bsd +Install dev packages + +```sh +opam install ocaml-lsp-server ocamlformat +``` + Once you have a working OCaml and Opam installation, you'll need to install the OCaml dependencies, which can be done using the following command: ```sh opam install . --deps-only -cd src/frontend && npm install ``` You also need to install the various npm dependencies, you can do so with @@ -48,6 +53,20 @@ And you can run a test instance of the server using the following command: make run ``` -### Installation +Deploy +------ + +### Deploy locally TODO + +### Deploy on a server + +TODO + +Develop +------- + +Read the [documentation about concepts](doc/concepts.md) to know what should be developped. +Then read how to [add a new concept](doc/coding_a_concept.md). + diff --git a/doc/coding_a_concept.md b/doc/coding_a_concept.md index 514f438..0021b7a 100644 --- a/doc/coding_a_concept.md +++ b/doc/coding_a_concept.md @@ -61,10 +61,43 @@ The `:id` in the `GET` request for competition `2` must also be mapped to an Oca The `Types.CompetitionId` type is declared in `src/backend/types.ml`. It is mapped to a database definition by `Ftw.Competition.id`. +Add the API routes for competitions to the router variable in `src/backend/main.ml`. +```ocaml +let router = + router + |> Event.routes + (* add the next line *) + |> Competition.routes +``` +Test interactively that it works by running it manually from terminals +```sh +# in a first terminal +make run +# in a second terminal +curl -s localhost:8080/api/comp/2 +``` + ## tests/api.t directory -The `run.t` file will do some automated tests on the API. -It initialise a new database, create an event, create two competition and check that the data is correctly defined. +The `run.t` file will do some automated tests on the API with [Cram tests](https://dune.readthedocs.io/en/stable/reference/cram.html). +It initialises a new database, create an event, create two competition and check that the data is correctly defined. + +Automate your tests here by adding the following lines. + +```cram +create a competition + $ curl -s -X PUT localhost:8080/api/comp \ + > -H "Content-Type: application/json" \ + > -d '{"event":1,"name":"","kind":["Jack_and_Jill"],"category":["Intermediate"]}' + +consult data for a competition + $ curl -s localhost:8080/api/comp/2 +``` +Analyse the diff and iterate until you get what you expect. +Then run the following command to store it in the `run.t` file. +```sh +dune promote +``` diff --git a/src/backend/category.ml b/src/backend/category.ml index bd63db1..c67f1a8 100644 --- a/src/backend/category.ml +++ b/src/backend/category.ml @@ -1,16 +1,22 @@ (* This file is free software, part of FTW. See file "LICENSE" for more information *) -open Utils.Syntax - (* Routes *) (* ************************************************************************* *) - + let rec routes router = router |> Router.get "/api/categories" category_list ~tags:["category"] ~summary:"List existing categories" + ~responses:[ + "200", Types.obj @@ Spec.make_response_object () + ~description:"Succesful operation" + ~content:[ + "application/json", + Spec.make_media_type_object () ~schema:Types.(ref CategoryList.ref); + ]; + ] (* Competition query *) @@ -20,7 +26,7 @@ and category_list = Api.get ~to_yojson:Types.CategoryList.to_yojson (fun _req _st -> - let categories = Types.Category.schema.enum in + let categories = Types.Category.all in let res : Types.CategoryList.t = { categories; } in Ok res ) diff --git a/src/backend/dune b/src/backend/dune index ba30141..49844b5 100644 --- a/src/backend/dune +++ b/src/backend/dune @@ -3,7 +3,7 @@ (executable (name main) (public_name ftw) - (preprocess (pps lwt_ppx ppx_deriving.show ppx_deriving_yojson)) + (preprocess (pps lwt_ppx ppx_deriving.show ppx_deriving.enum ppx_deriving_yojson)) (libraries ftw openapi_router diff --git a/src/backend/kind.ml b/src/backend/kind.ml new file mode 100644 index 0000000..317e1eb --- /dev/null +++ b/src/backend/kind.ml @@ -0,0 +1,37 @@ + +(* This file is free software, part of FTW. See file "LICENSE" for more information *) + +(*open Utils.Syntax*) + +(* Routes *) +(* ************************************************************************* *) + +let rec routes router = + router + |> Router.get "/api/kinds" category_list + ~tags:["kind"] + ~summary:"List existing kind of competitions" + ~responses:[ + "200", Types.obj @@ Spec.make_response_object () + ~description:"Succesful operation" + ~content:[ + "application/json", + Spec.make_media_type_object () ~schema:Types.(ref KindList.ref); + ]; + ] + + +(* Competition query *) +(* ************************************************************************* *) + +and category_list = + Api.get + ~to_yojson:Types.KindList.to_yojson + (fun _req _st -> + let kinds = Types.Kind.all in + let res : Types.KindList.t = { kinds; } in + Ok res + ) + + + diff --git a/src/backend/main.ml b/src/backend/main.ml index d5d8c55..ea1aaa5 100644 --- a/src/backend/main.ml +++ b/src/backend/main.ml @@ -43,6 +43,8 @@ let () = router |> Event.routes |> Competition.routes + |> Category.routes + |> Kind.routes in (* Setup the dream server and run it *) Dream.run diff --git a/src/backend/types.ml b/src/backend/types.ml index e782696..fad7666 100644 --- a/src/backend/types.ml +++ b/src/backend/types.ml @@ -105,7 +105,9 @@ module Kind = struct | Strictly | JJ_Strictly | Jack_and_Jill - [@@deriving yojson] + [@@deriving yojson, enum, show] + + let all = List.filter_map of_enum (List.init (to_enum Jack_and_Jill + 1) Fun.id) let ref, schema = make_schema () @@ -122,6 +124,24 @@ module Kind = struct ]) end + +(* Category list *) +module KindList = struct + type t = { + kinds : Kind.t list; + } [@@deriving yojson] + + let ref, schema = + make_schema () + ~name:"KindList" + ~typ:object_ + ~properties:[ + "kinds", obj @@ S.make_schema () + ~typ:array + ~items:(ref Kind.ref); + ] +end + (* Competition Division *) module Division = struct type t = Ftw.Division.t = @@ -153,7 +173,9 @@ module Category = struct | Regular | Qualifying | Invited - [@@deriving yojson] + [@@deriving yojson, enum] + + let all = List.filter_map of_enum (List.init (to_enum Invited + 1) Fun.id) let of_ftw cat : t = match (cat : Ftw.Category.t) with @@ -190,10 +212,10 @@ module Category = struct ]) end -(* Event Id list *) +(* Category list *) module CategoryList = struct type t = { - events : Category.t list; + categories : Category.t list; } [@@deriving yojson] let ref, schema = diff --git a/tests/api.t/run.t b/tests/api.t/run.t index ed869a9..bf10933 100644 --- a/tests/api.t/run.t +++ b/tests/api.t/run.t @@ -62,11 +62,11 @@ Get the ids of competitions we created, and check their details Miscellanous data $ curl -s localhost:8080/api/kinds - {"kind":["Jack_and_Jill", "Strictly", "Routine"]} + {"kinds":[["Routine"],["Strictly"],["JJ_Strictly"],["Jack_and_Jill"]]} $ curl -s localhost:8080/api/categories - {"category":["Novice", "Intermediate", "Advandced"]} + {"categories":[["Novice"],["Intermediate"],["Advanced"],["Regular"],["Qualifying"],["Invited"]]} End & Cleanup ------------- From f50a38bb17d8bd741f4006f59c147ddb41918518 Mon Sep 17 00:00:00 2001 From: But2ene Date: Sun, 2 Mar 2025 23:44:52 +0100 Subject: [PATCH 3/7] Update README.md Co-authored-by: Guillaume Bury --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 47a209f..1c19508 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,9 @@ OCaml and Opam, you can follow the following instructions: - For Windows : https://ocaml.org/install#windows - For Linux, MacOS, and BSD: https://ocaml.org/install#linux_mac_bsd -Install dev packages +### Dev Dependencies + +Here are some common and useful development dependencies (these are mainly useful for vscode, other setups may need different deps). ```sh opam install ocaml-lsp-server ocamlformat From 3cb623354b2eb5ad8a47d0296bfd710097f5c3ca Mon Sep 17 00:00:00 2001 From: Xavier DUQUESNE Date: Mon, 3 Mar 2025 00:16:07 +0100 Subject: [PATCH 4/7] reset readme --- Makefile | 2 +- README.md | 27 ++------------------------- 2 files changed, 3 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index d4a4601..7b6c4f1 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ configure: cd src/frontend && npm install $(FRONTEND_TARGET): $(FRONTEND_DEPS) - cd ./src/frontend && npm run build && cd ../.. + cd src/frontend && npm run build backend: $(FRONTEND_TARGET) dune build $(FLAGS) @install diff --git a/README.md b/README.md index 1c19508..f7d06ba 100644 --- a/README.md +++ b/README.md @@ -15,19 +15,12 @@ OCaml and Opam, you can follow the following instructions: - For Windows : https://ocaml.org/install#windows - For Linux, MacOS, and BSD: https://ocaml.org/install#linux_mac_bsd -### Dev Dependencies - -Here are some common and useful development dependencies (these are mainly useful for vscode, other setups may need different deps). - -```sh -opam install ocaml-lsp-server ocamlformat -``` - Once you have a working OCaml and Opam installation, you'll need to install the OCaml dependencies, which can be done using the following command: ```sh opam install . --deps-only +cd src/frontend && npm install ``` You also need to install the various npm dependencies, you can do so with @@ -55,20 +48,4 @@ And you can run a test instance of the server using the following command: make run ``` -Deploy ------- - -### Deploy locally - -TODO - -### Deploy on a server - -TODO - -Develop -------- - -Read the [documentation about concepts](doc/concepts.md) to know what should be developped. -Then read how to [add a new concept](doc/coding_a_concept.md). - +### Installation From 68af838eaf7dd270c925e9e7f8dd15baac928514 Mon Sep 17 00:00:00 2001 From: Xavier DUQUESNE Date: Mon, 17 Mar 2025 00:14:26 +0100 Subject: [PATCH 5/7] undo changes for category and kind --- src/backend/category.ml | 35 ---------------------------------- src/backend/dune | 2 +- src/backend/kind.ml | 37 ------------------------------------ src/backend/main.ml | 2 -- src/backend/types.ml | 42 ++--------------------------------------- tests/api.t/run.t | 9 --------- 6 files changed, 3 insertions(+), 124 deletions(-) delete mode 100644 src/backend/category.ml delete mode 100644 src/backend/kind.ml diff --git a/src/backend/category.ml b/src/backend/category.ml deleted file mode 100644 index c67f1a8..0000000 --- a/src/backend/category.ml +++ /dev/null @@ -1,35 +0,0 @@ - -(* This file is free software, part of FTW. See file "LICENSE" for more information *) - -(* Routes *) -(* ************************************************************************* *) - -let rec routes router = - router - |> Router.get "/api/categories" category_list - ~tags:["category"] - ~summary:"List existing categories" - ~responses:[ - "200", Types.obj @@ Spec.make_response_object () - ~description:"Succesful operation" - ~content:[ - "application/json", - Spec.make_media_type_object () ~schema:Types.(ref CategoryList.ref); - ]; - ] - - -(* Competition query *) -(* ************************************************************************* *) - -and category_list = - Api.get - ~to_yojson:Types.CategoryList.to_yojson - (fun _req _st -> - let categories = Types.Category.all in - let res : Types.CategoryList.t = { categories; } in - Ok res - ) - - - diff --git a/src/backend/dune b/src/backend/dune index 49844b5..ba30141 100644 --- a/src/backend/dune +++ b/src/backend/dune @@ -3,7 +3,7 @@ (executable (name main) (public_name ftw) - (preprocess (pps lwt_ppx ppx_deriving.show ppx_deriving.enum ppx_deriving_yojson)) + (preprocess (pps lwt_ppx ppx_deriving.show ppx_deriving_yojson)) (libraries ftw openapi_router diff --git a/src/backend/kind.ml b/src/backend/kind.ml deleted file mode 100644 index 317e1eb..0000000 --- a/src/backend/kind.ml +++ /dev/null @@ -1,37 +0,0 @@ - -(* This file is free software, part of FTW. See file "LICENSE" for more information *) - -(*open Utils.Syntax*) - -(* Routes *) -(* ************************************************************************* *) - -let rec routes router = - router - |> Router.get "/api/kinds" category_list - ~tags:["kind"] - ~summary:"List existing kind of competitions" - ~responses:[ - "200", Types.obj @@ Spec.make_response_object () - ~description:"Succesful operation" - ~content:[ - "application/json", - Spec.make_media_type_object () ~schema:Types.(ref KindList.ref); - ]; - ] - - -(* Competition query *) -(* ************************************************************************* *) - -and category_list = - Api.get - ~to_yojson:Types.KindList.to_yojson - (fun _req _st -> - let kinds = Types.Kind.all in - let res : Types.KindList.t = { kinds; } in - Ok res - ) - - - diff --git a/src/backend/main.ml b/src/backend/main.ml index ea1aaa5..d5d8c55 100644 --- a/src/backend/main.ml +++ b/src/backend/main.ml @@ -43,8 +43,6 @@ let () = router |> Event.routes |> Competition.routes - |> Category.routes - |> Kind.routes in (* Setup the dream server and run it *) Dream.run diff --git a/src/backend/types.ml b/src/backend/types.ml index fad7666..f2603fa 100644 --- a/src/backend/types.ml +++ b/src/backend/types.ml @@ -105,9 +105,7 @@ module Kind = struct | Strictly | JJ_Strictly | Jack_and_Jill - [@@deriving yojson, enum, show] - - let all = List.filter_map of_enum (List.init (to_enum Jack_and_Jill + 1) Fun.id) + [@@deriving yojson] let ref, schema = make_schema () @@ -124,24 +122,6 @@ module Kind = struct ]) end - -(* Category list *) -module KindList = struct - type t = { - kinds : Kind.t list; - } [@@deriving yojson] - - let ref, schema = - make_schema () - ~name:"KindList" - ~typ:object_ - ~properties:[ - "kinds", obj @@ S.make_schema () - ~typ:array - ~items:(ref Kind.ref); - ] -end - (* Competition Division *) module Division = struct type t = Ftw.Division.t = @@ -173,9 +153,7 @@ module Category = struct | Regular | Qualifying | Invited - [@@deriving yojson, enum] - - let all = List.filter_map of_enum (List.init (to_enum Invited + 1) Fun.id) + [@@deriving yojson] let of_ftw cat : t = match (cat : Ftw.Category.t) with @@ -212,22 +190,6 @@ module Category = struct ]) end -(* Category list *) -module CategoryList = struct - type t = { - categories : Category.t list; - } [@@deriving yojson] - - let ref, schema = - make_schema () - ~name:"CategoryList" - ~typ:object_ - ~properties:[ - "categories", obj @@ S.make_schema () - ~typ:array - ~items:(ref Category.ref); - ] -end (* Events *) (* ************************************************************************* *) diff --git a/tests/api.t/run.t b/tests/api.t/run.t index bf10933..4e7cdaa 100644 --- a/tests/api.t/run.t +++ b/tests/api.t/run.t @@ -59,15 +59,6 @@ Get the ids of competitions we created, and check their details $ curl -s localhost:8080/api/comp/2 {"event":1,"name":"","kind":["Jack_and_Jill"],"category":["Intermediate"]} -Miscellanous data - - $ curl -s localhost:8080/api/kinds - {"kinds":[["Routine"],["Strictly"],["JJ_Strictly"],["Jack_and_Jill"]]} - - - $ curl -s localhost:8080/api/categories - {"categories":[["Novice"],["Intermediate"],["Advanced"],["Regular"],["Qualifying"],["Invited"]]} - End & Cleanup ------------- From f079f3c6faa678cfee2b6001d23ef52834f657b9 Mon Sep 17 00:00:00 2001 From: Xavier DUQUESNE Date: Mon, 17 Mar 2025 00:16:03 +0100 Subject: [PATCH 6/7] finish revert --- README.md | 2 ++ tests/api.t/run.t | 1 + 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index f7d06ba..06c9ea0 100644 --- a/README.md +++ b/README.md @@ -49,3 +49,5 @@ make run ``` ### Installation + +TODO \ No newline at end of file diff --git a/tests/api.t/run.t b/tests/api.t/run.t index 4e7cdaa..e9655ab 100644 --- a/tests/api.t/run.t +++ b/tests/api.t/run.t @@ -59,6 +59,7 @@ Get the ids of competitions we created, and check their details $ curl -s localhost:8080/api/comp/2 {"event":1,"name":"","kind":["Jack_and_Jill"],"category":["Intermediate"]} + End & Cleanup ------------- From 5303e164210558e1eb4ae883dca5075c61be6682 Mon Sep 17 00:00:00 2001 From: Xavier DUQUESNE Date: Mon, 17 Mar 2025 00:18:53 +0100 Subject: [PATCH 7/7] test readme change --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 06c9ea0..ee5c341 100644 --- a/README.md +++ b/README.md @@ -50,4 +50,4 @@ make run ### Installation -TODO \ No newline at end of file +TODO