Skip to content

Commit e6ef127

Browse files
authored
Merge pull request #101 from mikulskibartosz/intro-to-play-scala
Introduction to the Play Framework in Scala
2 parents 939454f + e7be61a commit e6ef127

File tree

15 files changed

+170
-0
lines changed

15 files changed

+170
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package controllers
2+
3+
import javax.inject._
4+
import play.api._
5+
import play.api.mvc._
6+
7+
@Singleton class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
8+
9+
def index() = Action { implicit request: Request[AnyContent] =>
10+
Ok(views.html.firstexample())
11+
}
12+
13+
def printSum(first: Long, second: Long) = Action { implicit request: Request[AnyContent] =>
14+
val sum = first + second
15+
Ok(views.html.index(sum))
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@()
2+
3+
@main("Welcome to Introduction to Play Framework!") {
4+
<h1>Welcome to Introduction to Play Framework!</h1>
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@(sum: Long)
2+
3+
@main("Add two numbers") {
4+
<h1>The sum is @sum!</h1>
5+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@*
2+
* This template is called from the `index` template. This template
3+
* handles the rendering of the page header and body tags. It takes
4+
* two arguments, a `String` for the title of the page and an `Html`
5+
* object to insert into the body of the page.
6+
*@
7+
@(title: String)(content: Html)
8+
9+
<!DOCTYPE html>
10+
<html lang="en">
11+
<head>
12+
@* Here's where we render the page title `String`. *@
13+
<title>@title</title>
14+
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned(" stylesheets
15+
/main.css")">
16+
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned(" images
17+
/favicon.png")">
18+
19+
</head>
20+
<body>
21+
@* And here's where we render the `Html` object containing
22+
* the page content. *@
23+
@content
24+
25+
<script src="@routes.Assets.versioned(" javascripts
26+
/main.js")" type="text/javascript"></script>
27+
</body>
28+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name := """baeldung-play-framework""" organization := "com.baeldung"
2+
3+
version := "1.0-SNAPSHOT"
4+
5+
lazy val root = (project in file(".")).enablePlugins(PlayScala)
6+
7+
scalaVersion := "2.13.2"
8+
9+
libraryDependencies += guice libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# https://www.playframework.com/documentation/latest/Configuration
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!-- https://www.playframework.com/documentation/latest/SettingsLogger -->
2+
<configuration>
3+
4+
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel"/>
5+
6+
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
7+
<file>${application.home:-.}/logs/application.log</file>
8+
<encoder>
9+
<charset>UTF-8</charset>
10+
<pattern>
11+
%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{akkaSource}) %msg%n
12+
</pattern>
13+
</encoder>
14+
</appender>
15+
16+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
17+
<withJansi>true</withJansi>
18+
<encoder>
19+
<charset>UTF-8</charset>
20+
<pattern>
21+
%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{akkaSource}) %msg%n
22+
</pattern>
23+
</encoder>
24+
</appender>
25+
26+
<appender name="ASYNCFILE" class="ch.qos.logback.classic.AsyncAppender">
27+
<appender-ref ref="FILE"/>
28+
</appender>
29+
30+
<appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
31+
<appender-ref ref="STDOUT"/>
32+
</appender>
33+
34+
<logger name="play" level="INFO"/>
35+
<logger name="application" level="DEBUG"/>
36+
37+
<root level="WARN">
38+
<!--<appender-ref ref="ASYNCFILE" />-->
39+
<appender-ref ref="ASYNCSTDOUT"/>
40+
</root>
41+
42+
</configuration>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# https://www.playframework.com/documentation/latest/ScalaI18N
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Routes
2+
# This file defines all application routes (Higher priority routes first)
3+
# https://www.playframework.com/documentation/latest/ScalaRouting
4+
# ~~~~
5+
6+
# An example controller showing a sample home page
7+
GET / controllers.HomeController.index
8+
GET /sum/:first/:second controllers.HomeController.printSum(first: Long, second: Long)
9+
10+
# Map static resources from the /public folder to the /assets URL path
11+
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.3.10
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.2")
2+
addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.11.0")
Loading

play-scala/introduction-to-play/public/javascripts/main.js

Whitespace-only changes.

play-scala/introduction-to-play/public/stylesheets/main.css

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package controllers
2+
3+
import org.scalatestplus.play._
4+
import org.scalatestplus.play.guice._
5+
import play.api.test._
6+
import play.api.test.Helpers._
7+
8+
class HomeControllerSpec extends PlaySpec with GuiceOneAppPerTest with Injecting {
9+
10+
"HomeController GET" should {
11+
12+
"render the index page from a new instance of controller" in {
13+
val controller = new HomeController(stubControllerComponents())
14+
val home = controller.index().apply(FakeRequest(GET, "/"))
15+
16+
status(home) mustBe OK
17+
contentType(home) mustBe Some("text/html")
18+
contentAsString(home) must include("Welcome to Introduction to Play Framework")
19+
}
20+
21+
"render the index page from the application" in {
22+
val controller = inject[HomeController]
23+
val home = controller.index().apply(FakeRequest(GET, "/"))
24+
25+
status(home) mustBe OK
26+
contentType(home) mustBe Some("text/html")
27+
contentAsString(home) must include("Welcome to Introduction to Play Framework")
28+
}
29+
30+
"render the index page from the router" in {
31+
val request = FakeRequest(GET, "/")
32+
val home = route(app, request).get
33+
34+
status(home) mustBe OK
35+
contentType(home) mustBe Some("text/html")
36+
contentAsString(home) must include("Welcome to Introduction to Play Framework")
37+
}
38+
39+
"render a page that prints the sum of two numbers" in {
40+
val request = FakeRequest(GET, "/sum/10/20")
41+
val sumOfNumbers = route(app, request).get
42+
43+
status(sumOfNumbers) mustBe OK
44+
contentType(sumOfNumbers) mustBe Some("text/html")
45+
contentAsString(sumOfNumbers) must include("The sum is 30!")
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)