Scala-Play-Slick-Demo/app/controllers/HomeController.scala
Rohan Sircar cb9524eac7 Many changes
Rewrote slick db service using monix task instead of future
Added cats IO to controller to make use of above db service
Added action methods that support effect wrappers other than future
Added play-flyway to auto-manage migrations
Moved migration files to appropriate directory as a result of above
Added cors filter to work with separately hosted frontend
Made logger log at debug
2020-08-22 16:18:11 +05:30

59 lines
1.4 KiB
Scala

package controllers
import javax.inject.{Inject, Singleton}
import com.example.user.UserDAO
import play.api.mvc._
import scala.concurrent.ExecutionContext
import com.example.user.CarDAO
import com.example.services.LibraryService
import play.api.libs.json.Json
import com.example.services.TaskLibraryService
case class UserForm(name: String)
object UserForm {
implicit val userFormFormat = Json.format[UserForm]
}
@Singleton
class HomeController @Inject() (
userDAO: UserDAO,
carDAO: CarDAO,
libraryService: LibraryService,
taskLibraryService: TaskLibraryService,
cc: ControllerComponents
)(implicit ec: ExecutionContext)
extends AbstractController(cc) {
def index = Action.async {
userDAO.all.map { users => Ok(views.html.index(users)) }
}
def cars = Action.async {
carDAO.all.map { cars => Ok(views.html.cars(cars)) }
}
def book = Action.async {
// libraryService.findBookById(1).map(e => Ok(Json.toJson(e)))
// libraryService.insertBookAndAuthor(Book("new book"), Author(2, "Some retard"))
for {
maybeBook <- libraryService.findBookById(1)
} yield (Ok(Json.toJson(maybeBook)))
}
def authors(bookId: Long) = Action.async {
libraryService
.getAuthorsForBook(bookId)
.map(t => {
Ok(Json.toJson(t))
})
}
def user = Action {
Ok(Json.toJson(UserForm("hello")))
}
}