diff --git a/README b/README index 96a8d1d..62df9c9 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -This is your new Play 2.1 application +This is your new Play 2.5 application ===================================== This file will be packaged with your application, when using `play dist`. diff --git a/app/controllers/Messages.scala b/app/controllers/Messages.scala index cc85a0d..f295a2d 100644 --- a/app/controllers/Messages.scala +++ b/app/controllers/Messages.scala @@ -1,18 +1,17 @@ package controllers -import play.api.mvc.{AsyncResult, Action, Controller} -import play.api.data._ -import play.api.data.Forms._ -import helpers.Global.messagesRepository +import javax.inject.Inject + import com.github.mauricio.async.db.util.ExecutorServiceUtils.CachedExecutionContext -import models.Message +import models.{Message, MessageRepository} +import play.api.data.Forms._ +import play.api.data._ +import play.api.i18n.{I18nSupport, MessagesApi} +import play.api.mvc.{Action, Controller} + +import scala.concurrent.Future -/** - * User: mauricio - * Date: 4/27/13 - * Time: 4:17 PM - */ -object Messages extends Controller { +object Messages { val messageForm = Form( mapping( @@ -22,44 +21,47 @@ object Messages extends Controller { )(Message.apply)(Message.unapply) ) - def index = Action { - AsyncResult( messagesRepository.list.map { +} + +class Messages @Inject() (val messagesRepository: MessageRepository)(implicit val messagesApi: MessagesApi) extends Controller with I18nSupport { + + import Messages._ + + def index = Action.async { + messagesRepository.list.map { messages => Ok(views.html.messages.index(messages)) - } ) + } } def form = Action { Ok(views.html.messages.form(messageForm)) } - def edit( id : Long ) = Action { - AsyncResult { - messagesRepository.find(id).map { - messageOption => - messageOption match { - case Some(message) => { - Ok( views.html.messages.form( messageForm.fill(message) ) ) - } - case None => Ok( views.html.messages.form( messageForm ) ) + def edit(id: Long) = Action.async { + messagesRepository.find(id).map { + messageOption => + messageOption match { + case Some(message) => { + Ok(views.html.messages.form(messageForm.fill(message))) } - } + case None => Ok(views.html.messages.form(messageForm)) + } } + } - def update = Action { implicit request => + def update = Action.async { implicit request => messageForm.bindFromRequest().fold( form => { - BadRequest( views.html.messages.form(form) ) + Future(BadRequest(views.html.messages.form(form))) }, message => { - AsyncResult { - messagesRepository.save(message).map { - message => - Redirect(routes.Messages.index()) - } + messagesRepository.save(message).map { + message => + Redirect(routes.Messages.index()) } - } ) + }) } } diff --git a/app/helpers/ConnectionPoolProvider.scala b/app/helpers/ConnectionPoolProvider.scala new file mode 100644 index 0000000..da6786b --- /dev/null +++ b/app/helpers/ConnectionPoolProvider.scala @@ -0,0 +1,22 @@ +package helpers + +import javax.inject.{Inject, Provider} + +import com.github.mauricio.async.db.pool.{ConnectionPool, PoolConfiguration} +import com.github.mauricio.async.db.postgresql.PostgreSQLConnection +import com.github.mauricio.async.db.postgresql.pool.PostgreSQLConnectionFactory +import play.api.inject.ApplicationLifecycle + +import scala.concurrent.{ExecutionContext, Future} + + +class ConnectionPoolProvider @Inject()(factory: PostgreSQLConnectionFactory, + applicationLifecycle: ApplicationLifecycle) + (implicit executionContext: ExecutionContext) extends Provider[ConnectionPool[PostgreSQLConnection]] { + + applicationLifecycle.addStopHook(() => Future(get.close)) + + lazy val get: ConnectionPool[PostgreSQLConnection] = new ConnectionPool(factory, PoolConfiguration.Default) + + +} diff --git a/app/helpers/DatabaseConfigurationProvider.scala b/app/helpers/DatabaseConfigurationProvider.scala new file mode 100644 index 0000000..e337381 --- /dev/null +++ b/app/helpers/DatabaseConfigurationProvider.scala @@ -0,0 +1,19 @@ +package helpers + +import javax.inject.Provider + +import com.github.mauricio.async.db.Configuration +import com.github.mauricio.async.db.postgresql.util.URLParser + + +class DatabaseConfigurationProvider extends Provider[Configuration] { + private val databaseConfiguration : Configuration = System.getenv("DATABASE_URL") match { + case url : String => URLParser.parse(url) + case _ => new Configuration( + username = "postgres" , + database = Some("postgresql_async_app_development") + ) + } + + lazy val get: Configuration = databaseConfiguration +} diff --git a/app/helpers/Global.scala b/app/helpers/Global.scala deleted file mode 100644 index a16fd6b..0000000 --- a/app/helpers/Global.scala +++ /dev/null @@ -1,33 +0,0 @@ -package helpers - -import com.github.mauricio.async.db.Configuration -import com.github.mauricio.async.db.postgresql.pool.PostgreSQLConnectionFactory -import models.MessageRepository -import play.api.{Application, GlobalSettings} -import com.github.mauricio.async.db.pool.{PoolConfiguration, ConnectionPool} -import com.github.mauricio.async.db.postgresql.util.URLParser - -/** - * User: mauricio - * Date: 4/26/13 - * Time: 11:20 PM - */ -object Global extends GlobalSettings { - - private val databaseConfiguration = System.getenv("DATABASE_URL") match { - case url : String => URLParser.parse(url) - case _ => new Configuration( - username = "postgres" , - database = Some("postgresql_async_app_development") - ) - } - - private val factory = new PostgreSQLConnectionFactory( databaseConfiguration ) - private val pool = new ConnectionPool(factory, PoolConfiguration.Default) - val messagesRepository = new MessageRepository( pool ) - - override def onStop(app: Application) { - pool.close - } - -} \ No newline at end of file diff --git a/app/helpers/PostgreSQLConnectionFactoryProvider.scala b/app/helpers/PostgreSQLConnectionFactoryProvider.scala new file mode 100644 index 0000000..07dc90e --- /dev/null +++ b/app/helpers/PostgreSQLConnectionFactoryProvider.scala @@ -0,0 +1,14 @@ +package helpers + +import javax.inject.{Inject, Provider} + +import com.github.mauricio.async.db.Configuration +import com.github.mauricio.async.db.postgresql.pool.PostgreSQLConnectionFactory +import com.github.mauricio.async.db.postgresql.util.URLParser + + +class PostgreSQLConnectionFactoryProvider @Inject() (conf: Configuration) extends Provider[PostgreSQLConnectionFactory] { + + lazy val get: PostgreSQLConnectionFactory = new PostgreSQLConnectionFactory( conf ) + +} diff --git a/app/helpers/PostgresModule.scala b/app/helpers/PostgresModule.scala new file mode 100644 index 0000000..949d588 --- /dev/null +++ b/app/helpers/PostgresModule.scala @@ -0,0 +1,20 @@ +package helpers + +import com.github.mauricio.async.db.{Configuration, Connection} +import com.github.mauricio.async.db.pool.ConnectionPool +import com.github.mauricio.async.db.postgresql.PostgreSQLConnection +import com.github.mauricio.async.db.postgresql.pool.PostgreSQLConnectionFactory +import com.google.inject.AbstractModule + +/** + * Created by domdorn on 19/03/17. + */ +class PostgresModule extends AbstractModule { + override def configure(): Unit = { + bind(classOf[Configuration]).toProvider(classOf[DatabaseConfigurationProvider]) + bind(classOf[PostgreSQLConnectionFactory]).toProvider(classOf[PostgreSQLConnectionFactoryProvider]) + bind(classOf[ConnectionPool[PostgreSQLConnection]]).toProvider(classOf[ConnectionPoolProvider]) + bind(classOf[Connection]).toProvider(classOf[ConnectionPoolProvider]) + + } +} diff --git a/app/models/MessageRepository.scala b/app/models/MessageRepository.scala index bc23c1a..5cf5a61 100644 --- a/app/models/MessageRepository.scala +++ b/app/models/MessageRepository.scala @@ -1,8 +1,10 @@ package models +import javax.inject.Inject + import scala.concurrent.Future import org.joda.time.LocalDate -import com.github.mauricio.async.db.{RowData, Connection} +import com.github.mauricio.async.db.{Connection, RowData} import com.github.mauricio.async.db.util.ExecutorServiceUtils.CachedExecutionContext /** @@ -18,7 +20,7 @@ object MessageRepository { val SelectOne = "SELECT id, content, moment FROM messages WHERE id = ?" } -class MessageRepository(pool: Connection) { +class MessageRepository @Inject() (pool: Connection) { import MessageRepository._ diff --git a/app/views/main.scala.html b/app/views/main.scala.html index 6c72d80..ecb341f 100644 --- a/app/views/main.scala.html +++ b/app/views/main.scala.html @@ -1,4 +1,5 @@ -@(title: String)(content: Html) +@import play.api.i18n.Messages +@(title: String)(content: Html)(implicit messages: Messages) diff --git a/app/views/messages/form.scala.html b/app/views/messages/form.scala.html index bb59229..b152002 100644 --- a/app/views/messages/form.scala.html +++ b/app/views/messages/form.scala.html @@ -1,4 +1,5 @@ -@(messageForm: Form[Message]) +@import play.api.i18n.Messages +@(messageForm: Form[Message])(implicit messages: Messages) @main("Message Form") { @if( messageForm.hasErrors ) { diff --git a/app/views/messages/index.scala.html b/app/views/messages/index.scala.html index b6bd9af..70097fc 100644 --- a/app/views/messages/index.scala.html +++ b/app/views/messages/index.scala.html @@ -1,4 +1,5 @@ -@(messages : IndexedSeq[Message]) +@import play.api.i18n.Messages +@(messages : IndexedSeq[Message])(implicit msg: Messages) @main("Messages!") { diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..968aeea --- /dev/null +++ b/build.sbt @@ -0,0 +1,17 @@ +import sbt.Keys._ +import sbt._ +import play.sbt.PlayImport._ + + +scalaVersion in ThisBuild := "2.11.8" + +name := "postgresql-async-app" + +version := "2.5.13-SNAPSHOT" + +lazy val root = (project in file(".")).enablePlugins(PlayScala) + +libraryDependencies ++= Seq( + "com.github.mauricio" %% "postgresql-async" % "0.2.20" +) + diff --git a/conf/application.conf b/conf/application.conf index 3c464d3..3d622a2 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -11,12 +11,7 @@ application.secret="pVYurJ`B6sDd_OF@fon?Kd_XdTIsIT454Du1l4G6i6^v<55FSc`UN59_3bOs # ~~~~~ application.langs="en" -# Global object class -# ~~~~~ -# Define the Global object class for this application. -# Default to Global in the root package. - -application.global=helpers.Global +play.modules.enabled += helpers.PostgresModule # Router # ~~~~~ diff --git a/project/Build.scala b/project/Build.scala deleted file mode 100644 index 54bd417..0000000 --- a/project/Build.scala +++ /dev/null @@ -1,20 +0,0 @@ -import sbt._ -import Keys._ -import play.Project._ - -object ApplicationBuild extends Build { - - val appName = "postgresql-async-app" - val appVersion = "1.0-SNAPSHOT" - - val appDependencies = Seq( - // Add your project dependencies here - "com.github.mauricio" %% "postgresql-async" % "0.2.6" - ) - - - val main = play.Project(appName, appVersion, appDependencies).settings( - // Add your own project settings here - ) - -} diff --git a/project/build.properties b/project/build.properties index 66ad72c..27e88aa 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.12.2 +sbt.version=0.13.13 diff --git a/project/plugins.sbt b/project/plugins.sbt index a5baf7a..db2148f 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -5,4 +5,4 @@ logLevel := Level.Warn resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/" // Use the Play sbt plugin for Play projects -addSbtPlugin("play" % "sbt-plugin" % "2.1.1") \ No newline at end of file +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.13")