first commit

This commit is contained in:
2025-10-09 10:36:56 +02:00
commit 81a1fbfdb9
21 changed files with 347 additions and 0 deletions

View File

@@ -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))
}
)
}
}

View File

@@ -0,0 +1,12 @@
@($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"/>
}

12
.g8/form/conf/routes Normal file
View File

@@ -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)

View File

@@ -0,0 +1,2 @@
description = Generates a Controller with form handling
model = user

View File

@@ -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
}
}
}

9
.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
logs
target
/.bsp
/.idea
/.idea_modules
/.classpath
/.project
/.settings
/RUNNING_PID

View File

@@ -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())
}
}

View File

@@ -0,0 +1,5 @@
@()
@main("Welcome to Play") {
<h1>Welcome to Play!</h1>
}

25
app/views/main.scala.html Normal file
View File

@@ -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)
<!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>

17
build.sbt Normal file
View File

@@ -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._"

11
build.sc Normal file
View File

@@ -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
}

1
conf/application.conf Normal file
View File

@@ -0,0 +1 @@
# https://www.playframework.com/documentation/latest/Configuration

50
conf/logback.xml Normal file
View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- https://www.playframework.com/documentation/latest/SettingsLogger -->
<!DOCTYPE configuration>
<configuration>
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
<import class="ch.qos.logback.classic.AsyncAppender"/>
<import class="ch.qos.logback.core.FileAppender"/>
<import class="ch.qos.logback.core.ConsoleAppender"/>
<appender name="FILE" class="FileAppender">
<file>${application.home:-.}/logs/application.log</file>
<encoder class="PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{pekkoSource}) %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ConsoleAppender">
<!--
On Windows, enabling Jansi is recommended to benefit from color code interpretation on DOS command prompts,
which otherwise risk being sent ANSI escape sequences that they cannot interpret.
See https://logback.qos.ch/manual/layouts.html#coloring
-->
<!-- <withJansi>true</withJansi> -->
<encoder class="PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %cyan(%logger{36}) %magenta(%X{pekkoSource}) %msg%n</pattern>
</encoder>
</appender>
<appender name="ASYNCFILE" class="AsyncAppender">
<appender-ref ref="FILE"/>
</appender>
<appender name="ASYNCSTDOUT" class="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>

1
conf/messages Normal file
View File

@@ -0,0 +1 @@
# https://www.playframework.com/documentation/latest/ScalaI18N

10
conf/routes Normal file
View File

@@ -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)

1
project/build.properties Normal file
View File

@@ -0,0 +1 @@
sbt.version=1.11.7

2
project/plugins.sbt Normal file
View File

@@ -0,0 +1,2 @@
addSbtPlugin("org.playframework" % "sbt-plugin" % "3.0.9")
addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.18.0")

BIN
public/images/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 687 B

View File

View File

View File

@@ -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")
}
}
}