/backend/sosmessage-admin/app/controllers/Messages.scala

https://github.com/troger/sosmessagedecarte · Scala · 114 lines · 99 code · 15 blank · 0 comment · 3 complexity · c7719ebb28f3cba457177b4a25abbfbf MD5 · raw file

  1. package controllers
  2. import _root_.net.liftweb.json.JsonParser._
  3. import play.api._
  4. import data._
  5. import play.api.mvc._
  6. import com.mongodb.casbah.commons.MongoDBObject
  7. import com.mongodb.casbah.MongoConnection
  8. import org.bson.types.ObjectId
  9. import java.util.Date
  10. import com.mongodb.DBObject
  11. import com.mongodb.casbah.Imports._
  12. object Messages extends Controller {
  13. val DataBaseName = "sosmessage"
  14. val MessagesCollectionName = "messages"
  15. val CategoriesCollectionName = "categories"
  16. val mongo = MongoConnection()
  17. val messagesCollection = mongo(DataBaseName)(MessagesCollectionName)
  18. val categoriesCollection = mongo(DataBaseName)(CategoriesCollectionName)
  19. val messageForm = Form(
  20. of(
  21. "category" -> requiredText,
  22. "text" -> requiredText,
  23. "approved" -> optional(text)
  24. )
  25. )
  26. def index(categoryId: String = "none") = Action { implicit request =>
  27. val categoryOrder = MongoDBObject("name" -> 1)
  28. val categories = categoriesCollection.find().sort(categoryOrder).foldLeft(List[DBObject]())((l, a) =>
  29. a :: l
  30. ).reverse
  31. val selectedCategoryId = if (categoryId == "none") {
  32. categories(0).get("_id").toString
  33. } else {
  34. categoryId
  35. }
  36. val messageOrder = MongoDBObject("createdAt" -> -1)
  37. val q = MongoDBObject("categoryId" -> new ObjectId(selectedCategoryId), "state" -> "approved")
  38. val messages = messagesCollection.find(q).sort(messageOrder).foldLeft(List[DBObject]())((l, a) =>
  39. a :: l
  40. ).map(addRating(_))reverse
  41. Ok(views.html.messages.index(categories, selectedCategoryId, messages, messageForm))
  42. }
  43. def addRating(message: DBObject) = {
  44. val r = """
  45. function(doc, out) {
  46. for (var prop in doc.ratings) {
  47. out.count++;
  48. out.total += doc.ratings[prop];
  49. }
  50. }
  51. """
  52. val f = """
  53. function(out) {
  54. out.avg = out.total / out.count;
  55. }
  56. """
  57. val rating = messagesCollection.group(MongoDBObject("ratings" -> 1),
  58. MongoDBObject("_id" -> message.get("_id")), MongoDBObject("count" -> 0, "total" -> 0), r, f)
  59. val j = parse(rating.mkString)
  60. message.put("rating", j \ "avg" values)
  61. message.put("ratingCount", (j \ "count" values).asInstanceOf[Double].toInt)
  62. message
  63. }
  64. def save(selectedCategoryId: String) = Action { implicit request =>
  65. messageForm.bindFromRequest().fold(
  66. f => {
  67. println("bad: " + f)
  68. Redirect(routes.Messages.index(selectedCategoryId))
  69. },
  70. v => {
  71. val oid = new ObjectId(v._1)
  72. val o = MongoDBObject("_id" -> oid)
  73. val category = categoriesCollection.findOne(o).get
  74. val builder = MongoDBObject.newBuilder
  75. builder += "categoryId" -> category.get("_id")
  76. builder += "category" -> category.get("name")
  77. builder += "text" -> v._2
  78. val actionDone = v._3 match {
  79. case None =>
  80. builder += "state" -> "waiting"
  81. "messageWaiting"
  82. case Some(s) =>
  83. builder += "state" -> "approved"
  84. "messageAdded"
  85. }
  86. builder += "createdAt" -> new Date()
  87. builder += "random" -> scala.math.random
  88. messagesCollection += builder.result
  89. Redirect(routes.Messages.index(category.get("_id").toString)).flashing("actionDone" -> actionDone)
  90. }
  91. )
  92. }
  93. def delete(selectedCategoryId: String, messageId: String) = Action { implicit request =>
  94. val oid = new ObjectId(messageId)
  95. val o = MongoDBObject("_id" -> oid)
  96. messagesCollection.remove(o)
  97. Redirect(routes.Messages.index(selectedCategoryId)).flashing("actionDone" -> "messageDeleted")
  98. }
  99. }