Cards.scala 805 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. var cardPool = CardPool.get()
  26. def random = Action {
  27. if(!cardPool.hasNext) {
  28. cardPool = CardPool.get()
  29. }
  30. Ok(Json.toJson(cardPool.next()))
  31. }
  32. }