Skip to content

Commit

Permalink
refactor: structure
Browse files Browse the repository at this point in the history
  • Loading branch information
abyssparanoia committed Dec 29, 2019
1 parent 0ff67bb commit f9a08d5
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 380 deletions.
18 changes: 9 additions & 9 deletions app/Module.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import com.google.inject.AbstractModule
import java.time.Clock

import services.{ApplicationTimer, AtomicCounter, Counter}
import service.{ApplicationTimer, AtomicCounter, Counter}

/**
* This class is a Guice module that tells Guice how to bind several
* different types. This Guice module is created when the Play
* application starts.
* This class is a Guice module that tells Guice how to bind several
* different types. This Guice module is created when the Play
* application starts.
* Play will automatically use any class called `Module` that is in
* the root package. You can create modules in other locations by
* adding `play.modules.enabled` settings to the `application.conf`
* configuration file.
*/
* Play will automatically use any class called `Module` that is in
* the root package. You can create modules in other locations by
* adding `play.modules.enabled` settings to the `application.conf`
* configuration file.
*/
class Module extends AbstractModule {

override def configure() = {
Expand Down
54 changes: 54 additions & 0 deletions app/controller/AsyncController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package controller

import javax.inject._

import akka.actor.ActorSystem
import play.api.mvc._

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future, Promise}

/**
* This controller creates an `Action` that demonstrates how to write
* simple asynchronous code in a controller. It uses a timer to
* asynchronously delay sending a response for 1 second.
*
* @param cc standard controller components
* @param actorSystem We need the `ActorSystem`'s `Scheduler` to
* run code after a delay.
* @param exec We need an `ExecutionContext` to execute our
* asynchronous code. When rendering content, you should use Play's
* default execution context, which is dependency injected. If you are
* using blocking operations, such as database or network access, then you should
* use a different custom execution context that has a thread pool configured for
* a blocking API.
*/
@Singleton
class AsyncController @Inject()(
cc: ControllerComponents,
actorSystem: ActorSystem)(implicit exec: ExecutionContext)
extends AbstractController(cc) {

/**
* Creates an Action that returns a plain text message after a delay
* of 1 second.
*
* The configuration in the `routes` file means that this method
* will be called when the application receives a `GET` request with
* a path of `/message`.
*/
def message = Action.async {
getFutureMessage(1.second).map { msg =>
Ok(msg)
}
}

private def getFutureMessage(delayTime: FiniteDuration): Future[String] = {
val promise: Promise[String] = Promise[String]()
actorSystem.scheduler.scheduleOnce(delayTime) {
promise.success("Hi!")
}(actorSystem.dispatcher) // run scheduled tasks using the actor system's dispatcher
promise.future
}

}
25 changes: 25 additions & 0 deletions app/controller/CountController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package controller

import javax.inject._

import play.api.mvc._
import service.Counter

/**
* This controller demonstrates how to use dependency injection to
* bind a component into a controller class. The class creates an
* `Action` that shows an incrementing count to users. The [[Counter]]
* object is injected by the Guice dependency injection system.
*/
@Singleton
class CountController @Inject()(cc: ControllerComponents, counter: Counter)
extends AbstractController(cc) {

/**
* Create an action that responds with the [[Counter]]'s current
* count. The result is plain text. This `Action` is mapped to
* `GET /count` requests by an entry in the `routes` config file.
*/
def count = Action { Ok(counter.nextCount().toString) }

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package controller

import javax.inject._
import play.api.mvc._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package controllers
package controller

import models._
import domain.model.Post
import javax.inject._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import play.api.libs.json.{JsResult, JsValue, Json}
import services._
import service.PostService

class PostController @Inject()(mcc: MessagesControllerComponents,
postService: services.PostService)
postService: PostService)
extends MessagesAbstractController(mcc) {

def get(postID: Int) = Action {
def get(postID: Long) = Action {
implicit request: MessagesRequest[AnyContent] =>
val post: Option[Post] = postService.get(postID)
post match {
Expand Down Expand Up @@ -44,7 +44,7 @@ class PostController @Inject()(mcc: MessagesControllerComponents,

implicit val updatePoseRequestReads = Json.reads[UpdatePoseRequest]

def update(postID: Int): Action[JsValue] = Action(parse.json) { request =>
def update(postID: Long): Action[JsValue] = Action(parse.json) { request =>
val result: JsResult[UpdatePoseRequest] =
request.body.validate[UpdatePoseRequest]
val dst: UpdatePoseRequest = result.get
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package controller

import javax.inject.Inject
import play.api.mvc.{
Expand All @@ -9,9 +9,10 @@ import play.api.mvc.{
}
import domain.model
import play.api.libs.json.{JsResult, JsValue, Json}
import service.UserService

class UserController @Inject()(mcc: MessagesControllerComponents,
userService: services.UserService)
userService: UserService)
extends MessagesAbstractController(mcc) {
def get(id: String) = Action {
implicit request: MessagesRequest[AnyContent] =>
Expand Down
49 changes: 0 additions & 49 deletions app/controllers/AsyncController.scala

This file was deleted.

25 changes: 0 additions & 25 deletions app/controllers/CountController.scala

This file was deleted.

4 changes: 2 additions & 2 deletions app/domain/repository/PostRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ trait PostRepository {
description: String,
text: String,
createdAt: Long,
updatedAt: Long): Long
updatedAt: Long): Int
def update(id: Long,
title: String,
description: String,
text: String,
updatedAt: Long): Long
updatedAt: Long): Int
}
34 changes: 0 additions & 34 deletions app/entities/Post.scala

This file was deleted.

27 changes: 0 additions & 27 deletions app/entities/User.scala

This file was deleted.

4 changes: 2 additions & 2 deletions app/infrastructure/repository/PostRepositoryImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PostRepositoryImpl extends PostRepository {
description: String,
text: String,
createdAt: Long,
updatedAt: Long): Long = {
updatedAt: Long): Int = {
val future = database.run(
Tables.Posts.map(
post =>
Expand All @@ -61,7 +61,7 @@ class PostRepositoryImpl extends PostRepository {
title: String,
description: String,
text: String,
updatedAt: Long): Long = {
updatedAt: Long): Int = {

val future = database.run(
Tables.Posts
Expand Down
2 changes: 1 addition & 1 deletion app/infrastructure/repository/UserRepositoryImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import scala.concurrent.Await._

import com.google.inject.{Singleton}
@Singleton
class UserRepositoryImpl extends repository.User {
class UserRepositoryImpl extends repository.UserRepository {
private val database = Database.forConfig("db.default")

override def get(id: String): Option[User] = {
Expand Down
34 changes: 0 additions & 34 deletions app/models/Post.scala

This file was deleted.

15 changes: 0 additions & 15 deletions app/models/User.scala

This file was deleted.

Loading

0 comments on commit f9a08d5

Please sign in to comment.