-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathApplication.scala
More file actions
43 lines (35 loc) · 1.63 KB
/
Application.scala
File metadata and controls
43 lines (35 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package controllers
import javax.inject._
import play.api._
import play.api.db.Database
import play.api.mvc._
import org.jscience.physics.amount.Amount
import org.jscience.physics.model.RelativisticModel
import javax.measure.unit.SI
@Singleton
class Application @Inject()(val controllerComponents: ControllerComponents, val database: Database) extends BaseController {
def index(): Action[AnyContent] = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index())
}
def convert(): Action[AnyContent] = Action { implicit request: Request[AnyContent] =>
RelativisticModel.select()
val energy = Amount.valueOf("12 GeV");
Ok("E=mc^2: " + energy + " = " + energy.to(SI.KILOGRAM))
}
def db(): Action[AnyContent] = Action { implicit request: Request[AnyContent] =>
// In this getting started app, we don't use a custom execution context to keep the code and configuration simple.
// For real-world apps, consult the Play documentation on how to configure custom contexts and how to use them:
// https://www.playframework.com/documentation/2.8.19/AccessingAnSQLDatabase#Using-a-CustomExecutionContext
database.withConnection { connection =>
val statement = connection.createStatement()
statement.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)")
statement.executeUpdate("INSERT INTO ticks VALUES (now())")
val output = new StringBuilder();
val resultSet = statement.executeQuery("SELECT tick FROM ticks")
while (resultSet.next()) {
output.append("Read from DB: " + resultSet.getTimestamp("tick") + "\n")
}
Ok(output.toString())
}
}
}