PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/Blog.scala

http://github.com/leodagdag/persistance
Scala | 169 lines | 145 code | 19 blank | 5 comment | 0 complexity | c25bdb52c87cbb8b8d747b533d32ee6b MD5 | raw file
  1. package controllers
  2. import play.api.Play.current
  3. import play.api._
  4. import libs.json.Json._
  5. import play.api.mvc._
  6. import play.api.data._
  7. import play.api.data.Forms._
  8. import org.bson.types.ObjectId
  9. import com.mongodb.casbah.commons.MongoDBObject
  10. import models._
  11. import views._
  12. /**
  13. * User: leodagdag
  14. * Date: 21/03/12
  15. * Time: 09:48
  16. */
  17. object Blog extends Controller with Secured {
  18. lazy val config: Configuration = Application.config.get.getConfig("blog").getOrElse {
  19. current.configuration.globalError("app.blog config is missing")
  20. Configuration.empty
  21. }
  22. implicit lazy val dao = Post
  23. val postForm =
  24. Form(
  25. mapping(
  26. "_id" -> optional(text),
  27. "title" -> nonEmptyText,
  28. "content" -> nonEmptyText,
  29. "featured" -> boolean)
  30. ((_id, title, content, featured) =>
  31. Post(
  32. _id = ObjectId.massageToObjectId(_id),
  33. title = title,
  34. content = content,
  35. featured = featured))
  36. ((post: Post) =>
  37. Some(
  38. Some(post._id.toString),
  39. post.title,
  40. post.content,
  41. post.featured))
  42. )
  43. def addComment(id: String) = Logging {
  44. IsAuthenticated {
  45. username => {
  46. Action {
  47. implicit request => {
  48. Form("content" -> nonEmptyText).bindFromRequest.fold(
  49. formWithErrors => BadRequest,
  50. newComment => {
  51. val comment: Comment = new Comment(user = user.get, content = newComment)
  52. Post.addComment(ObjectId.massageToObjectId(id), comment);
  53. Ok
  54. }
  55. )
  56. }
  57. }
  58. }
  59. }
  60. }
  61. def index = Logging {
  62. Action {
  63. implicit request =>
  64. val count = Post.count()
  65. val featured = Post.featured
  66. val posts = models.Post.byPage(1)
  67. Ok(html.blog.index(count, featured, posts))
  68. }
  69. }
  70. def show(id: String) = Logging {
  71. Action {
  72. implicit request =>
  73. val post = Post.findOneByID(ObjectId.massageToObjectId(id))
  74. post match {
  75. case Some(post) => Ok(html.blog.show(post))
  76. case None => NotFound
  77. }
  78. }
  79. }
  80. def showJson(id: String) = Logging {
  81. Action {
  82. implicit request =>
  83. val post = Post.findOneByID(ObjectId.massageToObjectId(id))
  84. post match {
  85. case Some(post) => Ok(toJson(Post.writes(post)))
  86. case None => NotFound
  87. }
  88. }
  89. }
  90. def create = Logging {
  91. IsAuthenticated {
  92. username =>
  93. Action {
  94. implicit request => {
  95. Ok(html.blog.create(postForm))
  96. }
  97. }
  98. }
  99. }
  100. def save = Logging {
  101. IsAuthenticated {
  102. username =>
  103. Action {
  104. implicit request =>
  105. var p = postForm.bindFromRequest
  106. p.fold(
  107. formWithErrors => BadRequest(html.blog.create(formWithErrors)),
  108. newPost => {
  109. val id = Post.insert(newPost.copy(authorId = Some(user.get._id)))
  110. Redirect(routes.Blog.show(id.get.toString)).flashing("success" -> "Post %s has been created".format(newPost.title))
  111. }
  112. )
  113. }
  114. }
  115. }
  116. def edit(id: String) = Logging {
  117. IsAuthenticated {
  118. username =>
  119. Action {
  120. implicit request =>
  121. val post = Post.findOneByID(ObjectId.massageToObjectId(id))
  122. post match {
  123. case Some(post) => Ok(html.blog.edit(post._id.toString(), postForm.fill(post)))
  124. case None => NotFound
  125. }
  126. }
  127. }
  128. }
  129. def update(id: String) = Logging {
  130. IsAuthenticated {
  131. username =>
  132. Action {
  133. implicit request =>
  134. var p = postForm.bindFromRequest
  135. p.fold(
  136. formWithErrors => BadRequest(html.blog.edit(id, formWithErrors)),
  137. updPost => {
  138. val post = Post.findOneByID(ObjectId.massageToObjectId(id))
  139. post match {
  140. case Some(post) =>
  141. Post.update(MongoDBObject("_id" -> Some(post._id)), post.simpleCopy(updPost))
  142. Redirect(routes.Blog.show(id)).flashing("success" -> "Post %s has been updated".format(post.title))
  143. case None => NotFound
  144. }
  145. }
  146. )
  147. }
  148. }
  149. }
  150. }