HomeController.scala 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2020 Prasoon Joshi
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. package controllers
  18. import java.util.UUID
  19. import javax.inject._
  20. import models._
  21. import play.api.libs.json.{JsObject, JsString, JsValue, Json}
  22. import play.api.mvc._
  23. import configuration.QoreComponentRegistry._
  24. /**
  25. * This controller creates an `Action` to handle HTTP requests to the
  26. * application's home page.
  27. */
  28. @Singleton
  29. class HomeController @Inject()(messagesAction: MessagesActionBuilder,
  30. cc: ControllerComponents) extends AbstractController(cc) {
  31. /**
  32. * Create an Action to render an HTML page.
  33. *
  34. * The configuration in the `routes` file means that this method
  35. * will be called when the application receives a `GET` request with
  36. * a path of `/`.
  37. */
  38. def index() = messagesAction { implicit request: MessagesRequest[AnyContent] =>
  39. Ok(views.html.index
  40. (FormItem.itemForm))
  41. }
  42. // import sangria.renderer.SchemaRenderer
  43. // def graphqlSchema() = Action {implicit request: Request[AnyContent] =>
  44. // Ok(SchemaRenderer.renderSchema(SchemaDefinition.schema))
  45. // }
  46. def itemForm() = messagesAction { implicit request: MessagesRequest[AnyContent] =>
  47. //val itemData = itemForm.bindFromRequest().get
  48. FormItem.itemForm.bindFromRequest.fold(
  49. formWithErrors => {
  50. //TODO: Add a BadRequest(views.html.user(formWithErrors))
  51. // This would ensure that users is redirected to the same form with errors highlighted.
  52. BadRequest
  53. },
  54. itemData => {
  55. val newItem = ItemData(UUID.randomUUID(), itemData.title)
  56. //TODO: Save the 'newItem'
  57. //TODO: RedirectAfterPOST - solves the duplicate form submission issue.
  58. }
  59. )
  60. Ok(views.html.index(FormItem.itemForm))
  61. }
  62. def parseVariables(vars: String) =
  63. if (vars.trim == "" || vars.trim == "null") Json.obj() else Json.parse(vars).as[JsObject]
  64. def graphQl = Action.async(parse.json) { implicit request: Request[JsValue] =>
  65. import scala.concurrent.ExecutionContext.Implicits.global
  66. queryServer.fetchQueryResult(request.body)
  67. }
  68. }