PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/scala/tivua/model/Article.scala

https://github.com/ngocdaothanh/tivua
Scala | 125 lines | 98 code | 25 blank | 2 comment | 11 complexity | 9d16f8573b88e3d02278520763bde1d3 MD5 | raw file
  1. package tivua.model
  2. import java.util.Date
  3. import scala.collection.mutable.ArrayBuffer
  4. import com.mongodb.casbah.Imports._
  5. import org.bson.types.ObjectId
  6. class Article(
  7. var id: String,
  8. var title: String,
  9. var teaser: String,
  10. var body: String,
  11. var userId: String,
  12. var sticky: Boolean,
  13. var hits: Int,
  14. var createdAt: Date,
  15. var updatedAt: Date,
  16. var threadUpdatedAt: Date,
  17. var categories: Iterable[Category]) {
  18. def this() = this(null, null, "", "", "", false, 1, null, null, null, null)
  19. }
  20. object ArticleColl {
  21. val COLL = "articles"
  22. val ID = "_id"
  23. val TITLE = "title"
  24. val TEASER = "teaser"
  25. val BODY = "body"
  26. val USER_ID = "user_id"
  27. val STICKY = "sticky"
  28. val HITS = "hits"
  29. val CREATED_AT = "created_at"
  30. val UPDATED_AT = "updated_at"
  31. val THREAD_UPDATED_AT = "thread_updated_at"
  32. }
  33. object ArticleCategoryColl {
  34. val COLL = "articles_categories"
  35. val ID = "_id"
  36. val ARTICLE_ID = "article_id"
  37. val CATEGORY_ID = "category_id"
  38. val THREAD_UPDATED_AT = "thread_updated_at"
  39. }
  40. object Article {
  41. private val ITEMS_PER_PAGE = 10
  42. private val articleColl = DB.db(ArticleColl.COLL)
  43. private val articleCategoryColl = DB.db(ArticleCategoryColl.COLL)
  44. //----------------------------------------------------------------------------
  45. def page(p: Int): (Int, Iterable[Article]) = {
  46. val count = articleColl.count.toInt
  47. val numPages = (count / ITEMS_PER_PAGE) + (if (count % ITEMS_PER_PAGE == 0) 0 else 1)
  48. val cur = articleColl.find().sort(MongoDBObject("sticky" -> -1, "thread_updated_at" -> -1)).skip((p - 1) * ITEMS_PER_PAGE).limit(ITEMS_PER_PAGE)
  49. val buffer = ArrayBuffer[Article]()
  50. for (o <- cur) {
  51. val a = mongoToScala(o)
  52. buffer.append(a)
  53. }
  54. (numPages, buffer)
  55. }
  56. def page(p: Int, ids: Array[String]): (Int, Iterable[Article]) = {
  57. val count = ids.length
  58. val numPages = (count / ITEMS_PER_PAGE) + (if (count % ITEMS_PER_PAGE == 0) 0 else 1)
  59. val min = (p - 1) * ITEMS_PER_PAGE
  60. val max1 = p * ITEMS_PER_PAGE
  61. val max2 = if (max1 > count - 1) count - 1 else max1
  62. val articles = (min to max2).flatMap { id => first(ids(id)) }
  63. (numPages, articles)
  64. }
  65. def first(id: String): Option[Article] = articleColl.findOneByID(new ObjectId(id)).map(mongoToScala)
  66. def categoryPage(categoryId: String, page: Int): (Int, Iterable[Article]) = {
  67. val count = articleCategoryColl.count(MongoDBObject("category_id" -> categoryId)).toInt
  68. val numPages = (count / ITEMS_PER_PAGE) + (if (count % ITEMS_PER_PAGE == 0) 0 else 1)
  69. val cur = articleCategoryColl.find(MongoDBObject("category_id" -> categoryId)).sort(MongoDBObject("sticky" -> -1, "thread_updated_at" -> -1)).skip((page - 1) * ITEMS_PER_PAGE).limit(ITEMS_PER_PAGE)
  70. val buffer = ArrayBuffer[Article]()
  71. for (o <- cur) {
  72. val articleId = o.as[String](ArticleCategoryColl.ARTICLE_ID)
  73. first(articleId).foreach(buffer.append(_))
  74. }
  75. (numPages, buffer)
  76. }
  77. //----------------------------------------------------------------------------
  78. private def mongoToScala(mongo: DBObject) = {
  79. val id = mongo._id.get.toString
  80. val title = mongo.as[String] (ArticleColl.TITLE)
  81. val teaser = mongo.as[String] (ArticleColl.TEASER)
  82. val body = mongo.as[String] (ArticleColl.BODY)
  83. val userId = mongo.as[String] (ArticleColl.USER_ID)
  84. val sticky = mongo.as[Boolean](ArticleColl.STICKY)
  85. val hits = mongo.as[Int] (ArticleColl.HITS)
  86. val createdAt = mongo.as[Date] (ArticleColl.CREATED_AT)
  87. val updatedAt = mongo.as[Date] (ArticleColl.UPDATED_AT)
  88. val threadUpdatedAt = mongo.as[Date] (ArticleColl.THREAD_UPDATED_AT)
  89. val categories = {
  90. val cur = articleCategoryColl.find(MongoDBObject("article_id" -> id))
  91. val buffer = ArrayBuffer[Category]()
  92. for (o <- cur) {
  93. val categoryId = o.as[String](ArticleCategoryColl.CATEGORY_ID)
  94. Category.first(categoryId).foreach(buffer.append(_))
  95. }
  96. buffer.sortBy(_.position)
  97. }
  98. new Article(id, title, teaser, body, userId, sticky, hits, createdAt, updatedAt, threadUpdatedAt, categories)
  99. }
  100. }