Skip to content
Snippets Groups Projects
Commit 9a3cbc44 authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Create Scala Play project (#17)

parent bc6fda73
No related branches found
No related tags found
1 merge request!10Initialise a Scala Play project with a MongoDB controller for Feed Service
Showing
with 322 additions and 0 deletions
package controllers
import javax.inject._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
case class $model;format="Camel"$Data(name: String, age: Int)
// NOTE: Add the following to conf/routes to enable compilation of this class:
/*
GET /$model;format="camel"$ controllers.$model;format="Camel"$Controller.$model;format="camel"$Get()
POST /$model;format="camel"$ controllers.$model;format="Camel"$Controller.$model;format="camel"$Post()
*/
/**
* $model;format="Camel"$ form controller for Play Scala
*/
class $model;format="Camel"$Controller @Inject()(mcc: MessagesControllerComponents) extends MessagesAbstractController(mcc) {
val $model;format="camel"$Form = Form(
mapping(
"name" -> text,
"age" -> number
)($model;format="Camel"$Data.apply)($model;format="Camel"$Data.unapply)
)
def $model;format="camel"$Get() = Action { implicit request: MessagesRequest[AnyContent] =>
Ok(views.html.$model;format="camel"$.form($model;format="camel"$Form))
}
def $model;format="camel"$Post() = Action { implicit request: MessagesRequest[AnyContent] =>
$model;format="camel"$Form.bindFromRequest().fold(
formWithErrors => {
// binding failure, you retrieve the form containing errors:
BadRequest(views.html.$model;format="camel"$.form(formWithErrors))
},
$model;format="camel"$Data => {
/* binding success, you get the actual value. */
/* flashing uses a short lived cookie */
Redirect(routes.$model;format="Camel"$Controller.$model;format="camel"$Get()).flashing("success" -> ("Successful " + $model;format="camel"$Data.toString))
}
)
}
}
@($model;format="camel"$Form: Form[$model;format="Camel"$Data])(implicit request: MessagesRequestHeader)
<h1>$model;format="camel"$ form</h1>
@request.flash.get("success").getOrElse("")
@helper.form(action = routes.$model;format="Camel"$Controller.$model;format="camel"$Post()) {
@helper.CSRF.formField
@helper.inputText($model;format="camel"$Form("name"))
@helper.inputText($model;format="camel"$Form("age"))
<input type="submit" value="submit"/>
}
description = Generates a Controller with form handling
model = user
package controllers
import play.api.mvc._
import play.api.i18n._
import org.scalatestplus.play._
import org.scalatestplus.play.guice.GuiceOneAppPerTest
import play.api.http.FileMimeTypes
import play.api.test._
import play.api.test.Helpers._
import play.api.test.CSRFTokenHelper._
import scala.concurrent.ExecutionContext
/**
* $model;format="Camel"$ form controller specs
*/
class $model;format="Camel"$ControllerSpec extends PlaySpec with GuiceOneAppPerTest with Injecting {
// Provide stubs for components based off Helpers.stubControllerComponents()
class StubComponents(cc:ControllerComponents = stubControllerComponents()) extends MessagesControllerComponents {
override val parsers: PlayBodyParsers = cc.parsers
override val messagesApi: MessagesApi = cc.messagesApi
override val langs: Langs = cc.langs
override val fileMimeTypes: FileMimeTypes = cc.fileMimeTypes
override val executionContext: ExecutionContext = cc.executionContext
override val actionBuilder: ActionBuilder[Request, AnyContent] = cc.actionBuilder
override val messagesActionBuilder: MessagesActionBuilder = new DefaultMessagesActionBuilderImpl(parsers.default, messagesApi)(executionContext)
}
"$model;format="Camel"$Controller GET" should {
"render the index page from a new instance of controller" in {
val controller = new $model;format="Camel"$Controller(new StubComponents())
val request = FakeRequest().withCSRFToken
val home = controller.$model;format="camel"$Get().apply(request)
status(home) mustBe OK
contentType(home) mustBe Some("text/html")
}
"render the index page from the application" in {
val controller = inject[$model;format="Camel"$Controller]
val request = FakeRequest().withCSRFToken
val home = controller.$model;format="camel"$Get().apply(request)
status(home) mustBe OK
contentType(home) mustBe Some("text/html")
}
"render the index page from the router" in {
val request = CSRFTokenHelper.addCSRFToken(FakeRequest(GET, "/$model;format="camel"$"))
val home = route(app, request).get
status(home) mustBe OK
contentType(home) mustBe Some("text/html")
}
}
"$model;format="Camel"$Controller POST" should {
"process form" in {
val request = {
FakeRequest(POST, "/$model;format="camel"$")
.withFormUrlEncodedBody("name" -> "play", "age" -> "4")
}
val home = route(app, request).get
status(home) mustBe SEE_OTHER
}
}
}
logs
target
/.bsp
/.idea
/.idea_modules
/.classpath
/.project
/.settings
/RUNNING_PID
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
/**
* This controller creates an `Action` to handle HTTP requests to the
* application's home page.
*/
@Singleton
class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
/**
* Create an Action to render an HTML page.
*
* The configuration in the `routes` file means that this method
* will be called when the application receives a `GET` request with
* a path of `/`.
*/
def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index())
}
}
@()
@main("Welcome to Play") {
<h1>Welcome to Play!</h1>
}
@*
* This template is called from the `index` template. This template
* handles the rendering of the page header and body tags. It takes
* two arguments, a `String` for the title of the page and an `Html`
* object to insert into the body of the page.
*@
@(title: String)(content: Html)
<!DOCTYPE html>
<html lang="en">
<head>
@* Here's where we render the page title `String`. *@
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
</head>
<body>
@* And here's where we render the `Html` object containing
* the page content. *@
@content
<script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>
</body>
</html>
name := """feed-service"""
organization := "com.daily"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.10"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.daily.controllers._"
// Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.daily.binders._"
import mill._
import $ivy.`com.lihaoyi::mill-contrib-playlib:`, mill.playlib._
object feedservice extends PlayModule with SingleModule {
def scalaVersion = "2.13.10"
def playVersion = "2.8.19"
def twirlVersion = "1.5.1"
object test extends PlayTests
}
# https://www.playframework.com/documentation/latest/Configuration
<!-- https://www.playframework.com/documentation/latest/SettingsLogger -->
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${application.home:-.}/logs/application.log</file>
<encoder>
<charset>UTF-8</charset>
<pattern>
%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{akkaSource}) %msg%n
</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<charset>UTF-8</charset>
<pattern>
%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{akkaSource}) %msg%n
</pattern>
</encoder>
</appender>
<appender name="ASYNCFILE" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
</appender>
<appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="STDOUT" />
</appender>
<logger name="play" level="INFO" />
<logger name="application" level="DEBUG" />
<root level="WARN">
<appender-ref ref="ASYNCFILE" />
<appender-ref ref="ASYNCSTDOUT" />
</root>
</configuration>
# https://www.playframework.com/documentation/latest/ScalaI18N
# Routes
# This file defines all application routes (Higher priority routes first)
# https://www.playframework.com/documentation/latest/ScalaRouting
# ~~~~
# An example controller showing a sample home page
GET / controllers.HomeController.index()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
sbt.version=1.7.2
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.19")
addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.13.1")
backend-services/feed-service/public/images/favicon.png

687 B

package controllers
import org.scalatestplus.play._
import org.scalatestplus.play.guice._
import play.api.test._
import play.api.test.Helpers._
/**
* Add your spec here.
* You can mock out a whole application including requests, plugins etc.
*
* For more information, see https://www.playframework.com/documentation/latest/ScalaTestingWithScalaTest
*/
class HomeControllerSpec extends PlaySpec with GuiceOneAppPerTest with Injecting {
"HomeController GET" should {
"render the index page from a new instance of controller" in {
val controller = new HomeController(stubControllerComponents())
val home = controller.index().apply(FakeRequest(GET, "/"))
status(home) mustBe OK
contentType(home) mustBe Some("text/html")
contentAsString(home) must include ("Welcome to Play")
}
"render the index page from the application" in {
val controller = inject[HomeController]
val home = controller.index().apply(FakeRequest(GET, "/"))
status(home) mustBe OK
contentType(home) mustBe Some("text/html")
contentAsString(home) must include ("Welcome to Play")
}
"render the index page from the router" in {
val request = FakeRequest(GET, "/")
val home = route(app, request).get
status(home) mustBe OK
contentType(home) mustBe Some("text/html")
contentAsString(home) must include ("Welcome to Play")
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment