1
0

Cards.scala 707 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package controllers
  2. import play.api._
  3. import play.api.mvc._
  4. import play.api.data._
  5. import play.api.data.Forms._
  6. import play.api.libs.json._
  7. import models._
  8. object Cards extends Controller {
  9. val cardForm = Form(mapping(
  10. "word" -> text,
  11. "taboos" -> seq(text).transform(_.toSet, (set: Set[String]) => set.toSeq)
  12. )(Card.apply)(Card.unapply))
  13. def add = Action(parse.json) { implicit request =>
  14. cardForm.bindFromRequest.fold(
  15. { case error => BadRequest("invalid format") },
  16. { case card =>
  17. Card.add(card)
  18. Ok("ok")
  19. }
  20. )
  21. }
  22. def dump = Action {
  23. Ok(Json.toJson(Card.list()))
  24. }
  25. def random = Action {
  26. Ok(Json.toJson(Card.getRandom()))
  27. }
  28. }