PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Global.scala

http://github.com/leodagdag/persistance
Scala | 54 lines | 45 code | 9 blank | 0 comment | 0 complexity | 4c8a384727165bf526dfb675b96e0afe MD5 | raw file
  1. import play.api._
  2. import models._
  3. import com.mongodb.casbah.commons.MongoDBObject
  4. object Global extends GlobalSettings {
  5. def createUser {
  6. User.collection.drop()
  7. User.save(User(email = "admin@b.com", username = "admin", password = "admin", firstName = Some("user"), lastName = Some("admin"), admin = true))
  8. User.save(User(email = "user@b.com", username = "user", password = "user", firstName = Some("first"), lastName = Some("last")))
  9. }
  10. def createPost {
  11. Post.collection.drop
  12. var comments: List[Comment] = List.empty
  13. (1 to 1).foreach {
  14. i =>
  15. comments = comments :+ Comment(content = "Comment's content " + i, user = User.findOne(MongoDBObject("username" -> "user")).get)
  16. }
  17. comments.foreach(c => Comment.save(c))
  18. val id = User.findOne(MongoDBObject("username" -> "admin")).get._id
  19. (1 to 5).foreach {
  20. i =>
  21. Post.save(Post(title = "titre " + i, featured = 3.equals(i), content = ("content " + i), authorId = Some(id), comments = comments))
  22. }
  23. }
  24. override def onStart(app: Application) {
  25. Logger.info("Application start")
  26. app.mode match {
  27. case Mode.Dev => {
  28. createUser
  29. createPost
  30. }
  31. case Mode.Test => {
  32. }
  33. case Mode.Prod => {
  34. val admin = User.findOne(MongoDBObject("admin" -> true))
  35. admin match {
  36. case None => User.save(User(email = "admin@b.com", username = "admin", password = "admin", admin = true))
  37. case Some(admin) =>
  38. }
  39. }
  40. }
  41. }
  42. override def onStop(app: Application) {
  43. Logger.info("Application stop")
  44. }
  45. }