commit 81a1fbfdb972417c57e60299e7a2f72c7db23238 Author: Janis Date: Thu Oct 9 10:36:56 2025 +0200 first commit diff --git a/.g8/form/app/controllers/$model__Camel$Controller.scala b/.g8/form/app/controllers/$model__Camel$Controller.scala new file mode 100644 index 0000000..92a5f1d --- /dev/null +++ b/.g8/form/app/controllers/$model__Camel$Controller.scala @@ -0,0 +1,49 @@ +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) +object $model;format="Camel"$Data { + def unapply(data: $model;format="Camel"$Data): Option[(String, Int)] = Some((data.name, data.age)) +} + +// 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)) + } + ) + } +} diff --git a/.g8/form/app/views/$model__camel$/form.scala.html b/.g8/form/app/views/$model__camel$/form.scala.html new file mode 100644 index 0000000..14674ba --- /dev/null +++ b/.g8/form/app/views/$model__camel$/form.scala.html @@ -0,0 +1,12 @@ +@($model;format="camel"$Form: Form[$model;format="Camel"$Data])(implicit request: MessagesRequestHeader) + +

$model;format="camel"$ form

+ +@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")) + +} diff --git a/.g8/form/conf/routes b/.g8/form/conf/routes new file mode 100644 index 0000000..8bbbe14 --- /dev/null +++ b/.g8/form/conf/routes @@ -0,0 +1,12 @@ +# 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() +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() + +# Map static resources from the /public folder to the /assets URL path +GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) diff --git a/.g8/form/default.properties b/.g8/form/default.properties new file mode 100644 index 0000000..32090f3 --- /dev/null +++ b/.g8/form/default.properties @@ -0,0 +1,2 @@ +description = Generates a Controller with form handling +model = user diff --git a/.g8/form/test/controllers/$model__Camel$ControllerSpec.scala b/.g8/form/test/controllers/$model__Camel$ControllerSpec.scala new file mode 100644 index 0000000..d251743 --- /dev/null +++ b/.g8/form/test/controllers/$model__Camel$ControllerSpec.scala @@ -0,0 +1,71 @@ +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 + } + } + +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dce7303 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +logs +target +/.bsp +/.idea +/.idea_modules +/.classpath +/.project +/.settings +/RUNNING_PID diff --git a/app/controllers/HomeController.scala b/app/controllers/HomeController.scala new file mode 100644 index 0000000..272ed2d --- /dev/null +++ b/app/controllers/HomeController.scala @@ -0,0 +1,24 @@ +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()) + } +} diff --git a/app/views/index.scala.html b/app/views/index.scala.html new file mode 100644 index 0000000..68d37fb --- /dev/null +++ b/app/views/index.scala.html @@ -0,0 +1,5 @@ +@() + +@main("Welcome to Play") { +

Welcome to Play!

+} diff --git a/app/views/main.scala.html b/app/views/main.scala.html new file mode 100644 index 0000000..808a8b8 --- /dev/null +++ b/app/views/main.scala.html @@ -0,0 +1,25 @@ +@* + * 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) + + + + + @* Here's where we render the page title `String`. *@ + @title + + + + + + @* And here's where we render the `Html` object containing + * the page content. *@ + @content + + + + diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..d9bacdf --- /dev/null +++ b/build.sbt @@ -0,0 +1,17 @@ +name := """KnockOutWhist-Web""" +organization := "com.example" + +version := "1.0-SNAPSHOT" + +lazy val root = (project in file(".")).enablePlugins(PlayScala) + +scalaVersion := "2.13.17" + +libraryDependencies += guice +libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.2" % Test + +// Adds additional packages into Twirl +//TwirlKeys.templateImports += "com.example.controllers._" + +// Adds additional packages into conf/routes +// play.sbt.routes.RoutesKeys.routesImport += "com.example.binders._" diff --git a/build.sc b/build.sc new file mode 100644 index 0000000..cc5a226 --- /dev/null +++ b/build.sc @@ -0,0 +1,11 @@ +import mill._ +import $ivy.`com.lihaoyi::mill-contrib-playlib:`, mill.playlib._ + +object knockoutwhistweb extends RootModule with PlayModule { + + def scalaVersion = "2.13.17" + def playVersion = "3.0.9" + def twirlVersion = "2.0.9" + + object test extends PlayTests +} diff --git a/conf/application.conf b/conf/application.conf new file mode 100644 index 0000000..cb94680 --- /dev/null +++ b/conf/application.conf @@ -0,0 +1 @@ +# https://www.playframework.com/documentation/latest/Configuration diff --git a/conf/logback.xml b/conf/logback.xml new file mode 100644 index 0000000..ab6c2b1 --- /dev/null +++ b/conf/logback.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + ${application.home:-.}/logs/application.log + + UTF-8 + %d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{pekkoSource}) %msg%n + + + + + + + + UTF-8 + %d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{pekkoSource}) %msg%n + + + + + + + + + + + + + + + + + + + + diff --git a/conf/messages b/conf/messages new file mode 100644 index 0000000..0226738 --- /dev/null +++ b/conf/messages @@ -0,0 +1 @@ +# https://www.playframework.com/documentation/latest/ScalaI18N diff --git a/conf/routes b/conf/routes new file mode 100644 index 0000000..60e8169 --- /dev/null +++ b/conf/routes @@ -0,0 +1,10 @@ +# 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) diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..01a16ed --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.11.7 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..42328bb --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1,2 @@ +addSbtPlugin("org.playframework" % "sbt-plugin" % "3.0.9") +addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.18.0") diff --git a/public/images/favicon.png b/public/images/favicon.png new file mode 100644 index 0000000..c7d92d2 Binary files /dev/null and b/public/images/favicon.png differ diff --git a/public/javascripts/main.js b/public/javascripts/main.js new file mode 100644 index 0000000..e69de29 diff --git a/public/stylesheets/main.css b/public/stylesheets/main.css new file mode 100644 index 0000000..e69de29 diff --git a/test/controllers/HomeControllerSpec.scala b/test/controllers/HomeControllerSpec.scala new file mode 100644 index 0000000..9794755 --- /dev/null +++ b/test/controllers/HomeControllerSpec.scala @@ -0,0 +1,45 @@ +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") + } + } +}