1
0

Cards.scala 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 exists(word: String) = Action {
  23. Ok(Json.obj(
  24. "word" -> word,
  25. "exists" -> Card.exists(word)
  26. ))
  27. }
  28. def dump = Action {
  29. Ok(Json.toJson(Card.list()))
  30. }
  31. var cardPool = CardPool.get()
  32. def random = Action {
  33. if(!cardPool.hasNext) {
  34. cardPool = CardPool.get()
  35. }
  36. Ok(Json.toJson(cardPool.next()))
  37. }
  38. }