You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package controllers
  2. import javax.inject.{Inject, Singleton}
  3. import com.example.user.UserDAO
  4. import play.api.mvc._
  5. import scala.concurrent.ExecutionContext
  6. import com.example.user.CarDAO
  7. import com.example.services.LibraryService
  8. import play.api.libs.json.Json
  9. import com.example.services.TaskLibraryService
  10. case class UserForm(name: String)
  11. object UserForm {
  12. implicit val userFormFormat = Json.format[UserForm]
  13. }
  14. @Singleton
  15. class HomeController @Inject() (
  16. userDAO: UserDAO,
  17. carDAO: CarDAO,
  18. libraryService: LibraryService,
  19. taskLibraryService: TaskLibraryService,
  20. cc: ControllerComponents
  21. )(implicit ec: ExecutionContext)
  22. extends AbstractController(cc) {
  23. def index = Action.async {
  24. userDAO.all.map { users => Ok(views.html.index(users)) }
  25. }
  26. def cars = Action.async {
  27. carDAO.all.map { cars => Ok(views.html.cars(cars)) }
  28. }
  29. def book = Action.async {
  30. // libraryService.findBookById(1).map(e => Ok(Json.toJson(e)))
  31. // libraryService.insertBookAndAuthor(Book("new book"), Author(2, "Some retard"))
  32. for {
  33. maybeBook <- libraryService.findBookById(1)
  34. } yield (Ok(Json.toJson(maybeBook)))
  35. }
  36. def authors(bookId: Long) = Action.async {
  37. libraryService
  38. .getAuthorsForBook(bookId)
  39. .map(t => {
  40. Ok(Json.toJson(t))
  41. })
  42. }
  43. def user = Action {
  44. Ok(Json.toJson(UserForm("hello")))
  45. }
  46. }