-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
120 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 6 additions & 9 deletions
15
zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/greet/GreetingApp.scala
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
...uickstart-restful-webservice/src/main/scala/dev/zio/quickstart/greet/GreetingRoutes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dev.zio.quickstart.greet | ||
|
||
import zio.http.* | ||
|
||
/** Collection of routes that: | ||
* - Accept a `Request` and return a `Response` | ||
* - Do not fail | ||
* - Do not use the environment | ||
*/ | ||
object GreetingRoutes: | ||
def apply(): Routes[Any, Nothing] = | ||
Routes( | ||
// GET /greet?name=:name | ||
Method.GET / "greet" -> handler { (req: Request) => | ||
if (req.url.queryParams.nonEmpty) | ||
Response.text( | ||
s"Hello ${req.url.queryParams("name").map(_.mkString(" and "))}!" | ||
) | ||
else Response.badRequest("The name query parameter is missing!") | ||
}, | ||
|
||
// GET /greet | ||
Method.GET / "greet" -> handler(Response.text(s"Hello World!")), | ||
|
||
// GET /greet/:name | ||
Method.GET / "greet" / string("name") -> handler {( name: String, _: Request) => | ||
Response.text(s"Hello $name!") | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/users/UserApp.scala
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/users/UserRoutes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dev.zio.quickstart.users | ||
|
||
import zio.* | ||
import zio.http.* | ||
import zio.schema.codec.JsonCodec.schemaBasedBinaryCodec | ||
|
||
/** Collection of routes that: | ||
* - Accept a `Request` and returns a `Response` | ||
* - May fail with type of `Response` | ||
* - Require a `UserRepo` from the environment | ||
*/ | ||
object UserRoutes: | ||
def apply(): Routes[UserRepo, Response] = | ||
Routes( | ||
// POST /users -d '{"name": "John", "age": 35}' | ||
Method.POST / "users" -> handler { (req: Request) => | ||
for { | ||
u <- req.body.to[User].orElseFail(Response.badRequest) | ||
r <- | ||
UserRepo | ||
.register(u) | ||
.mapBoth( | ||
_ => | ||
Response | ||
.internalServerError(s"Failed to register the user: $u"), | ||
id => Response.text(id) | ||
) | ||
} yield r | ||
}, | ||
|
||
// GET /users/:id | ||
Method.GET / "users" / string("id") -> handler { | ||
(id: String, _: Request) => | ||
UserRepo | ||
.lookup(id) | ||
.mapBoth( | ||
_ => Response.internalServerError(s"Cannot retrieve user $id"), | ||
{ | ||
case Some(user) => | ||
Response(body = Body.from(user)) | ||
case None => | ||
Response.notFound(s"User $id not found!") | ||
} | ||
) | ||
}, | ||
// GET /users | ||
Method.GET / "users" -> handler { | ||
UserRepo.users.mapBoth( | ||
_ => Response.internalServerError("Cannot retrieve users!"), | ||
users => Response(body =Body.from(users)) | ||
) | ||
} | ||
) |