Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Update the README #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

caarmen
Copy link

@caarmen caarmen commented Dec 15, 2021

  • Update some of the referenced class names
  • Remove the /users path from the local server url
  • Update the reference to the sql file for the table creation
  • Add an example curl command to create a user (otherwise the example to fetch a user won't return any user). Update the example which fetches a user, to fetch the one just created
  • Fix a spelling error

* Update some of the referenced class names
* Remove the /users path from the local server url
* Update the reference to the sql file for the table creation
* Add an example curl command to create a user (otherwise the example to fetch a user won't return any user). Update the example which fetches a user, to fetch the one just created
* Fix a spelling error
6. `sbt test` (optional)
7. `sbt run`
8. `curl http://localhost:8080/users/USERNAME`
8. Create a user: `curl -X POST http://localhost:8080/ -H 'Content-Type: application/json' -d '{"username":"jdoe","email":"jdoe@gmail.com"}'`
9. Read a user `curl http://localhost:8080/jdoe`
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the current code, the endpoints are at the root /, not /users.

If it's desired to have them at /users, I think something like the following would be needed:

diff --git a/src/main/scala/co/ledger/template/http/UserHttpRoutes.scala b/src/main/scala/co/ledger/template/http/UserHttpRoutes.scala
index 4118217..e0a77d6 100644
--- a/src/main/scala/co/ledger/template/http/UserHttpRoutes.scala
+++ b/src/main/scala/co/ledger/template/http/UserHttpRoutes.scala
@@ -20,21 +20,21 @@ class UserHttpRoutes[F[_]: Sync](userService: UserService[F])(
   val routes: HttpRoutes[F] = HttpRoutes.of[F] {
 
     // Find all users
-    case GET -> Root =>
+    case GET -> Root  / "users" =>
       for {
         users <- userService.findAll
         response <- users.fold(H.handle, x => Ok(x.asJson))
       } yield response
 
     // Find user by username
-    case GET -> Root / username =>
+    case GET -> Root / "users" / username =>
       for {
         user <- userService.findUser(UserName(username))
         response <- user.fold(H.handle, x => Ok(x.asJson))
       } yield response
 
     // Create a user
-    case req @ POST -> Root =>
+    case req @ POST -> Root  / "users" =>
       req.decode[CreateUser] { createUser =>
         UserValidation
           .validateCreateUser(createUser)
@@ -48,7 +48,7 @@ class UserHttpRoutes[F[_]: Sync](userService: UserService[F])(
       }
 
     // Update a user
-    case req @ PUT -> Root / username =>
+    case req @ PUT -> Root / "users" / username =>
       req.decode[UpdateUser] { updateUser =>
         UserValidation
           .validateUpdateUser(updateUser)
@@ -63,7 +63,7 @@ class UserHttpRoutes[F[_]: Sync](userService: UserService[F])(
       }
 
     // Delete a user
-    case DELETE -> Root / username =>
+    case DELETE -> Root / "users" / username =>
       for {
         result <- userService.deleteUser(UserName(username))
         response <- result.fold(H.handle, _ => NoContent())
diff --git a/src/test/scala/co/ledger/template/http/UserHttpRoutesSpec.scala b/src/test/scala/co/ledger/template/http/UserHttpRoutesSpec.scala
index 99059e5..a64f348 100644
--- a/src/test/scala/co/ledger/template/http/UserHttpRoutesSpec.scala
+++ b/src/test/scala/co/ledger/template/http/UserHttpRoutesSpec.scala
@@ -23,7 +23,7 @@ class UserHttpEndpointSpec extends UserHttpRoutesFixture with AnyFlatSpecLike wi
 
   forAll(examples) { (username, expectedStatus, expectedBody) =>
     it should s"find the user with username: ${username.value}" in IOAssertion {
-      val request = Request[IO](uri = Uri(path = s"/${username.value}"))
+      val request = Request[IO](uri = Uri(path = s"/users/${username.value}"))
       httpRoutes.flatMap(_.run(request).value).flatMap { task =>
         task.fold(IO(fail("Empty response")) *> IO.unit) { response =>
           IO(response.status        should be (expectedStatus)) *>
@@ -34,7 +34,7 @@ class UserHttpEndpointSpec extends UserHttpRoutesFixture with AnyFlatSpecLike wi
   }
 
   it should "Create a user" in IOAssertion {
-    val req = Request[IO](method = Method.POST).withEntity(CreateUser("root", "root@unix.org"))
+    val req = Request[IO](method = Method.POST, uri = Uri(path="/users")).withEntity(CreateUser("root", "root@unix.org"))
     for {
       routes <- httpRoutes
       task  <- routes(req).value

(though probably with a constant somewhere, instead of the string literal repeated).

Maybe there's a way to configure this in one place though 🤔

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant