PageRenderTime 77ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/checklist/Template.scala

https://gitlab.com/williamhogman/checklist
Scala | 98 lines | 74 code | 24 blank | 0 comment | 2 complexity | 0086b316a605a9cff788f7384074406c MD5 | raw file
Possible License(s): Apache-2.0
  1. package checklist
  2. import scala.collection.mutable
  3. import com.mongodb.casbah.Implicits._
  4. import org.bson.types.ObjectId
  5. import com.mongodb.casbah.Imports._
  6. import com.mongodb.casbah.commons.{MongoDBObject,MongoDBList}
  7. import com.fasterxml.jackson.annotation.JsonProperty
  8. class TemplateItem(
  9. @JsonProperty("title")
  10. var title: String) {
  11. def this(obj: MongoDBObject) = this(
  12. title = obj.as[String]("title")
  13. )
  14. def getTitle = title
  15. def toMongoObject = MongoDBObject(
  16. "title" -> title
  17. )
  18. }
  19. class Template(
  20. var requireSignoff: Boolean,
  21. var description: String,
  22. var items: Seq[TemplateItem]) {
  23. def this(obj: MongoDBObject) = this(
  24. requireSignoff = obj.as[Boolean]("requireSignoff"),
  25. description = obj.as[String]("description"),
  26. items = obj.as[MongoDBList]("items").map(_.asInstanceOf[DBObject]).map(new TemplateItem(_))
  27. )
  28. def toMongoObject = MongoDBObject(
  29. "requireSignoff" -> requireSignoff,
  30. "description" -> description,
  31. "items" -> items.map(_.toMongoObject)
  32. )
  33. def getItems = items
  34. def getDescription = description
  35. def getRequireSignoff = requireSignoff
  36. }
  37. object Template {
  38. private val db = checklist.Database.db
  39. implicit object TemplateIsIdFindable extends IdFindable[Template] {
  40. def byId(id: ObjectId) = {
  41. val query = MongoDBObject("_id" -> id)
  42. val db = checklist.Database.db
  43. db("templates").findOne(query) map(new Template(_))
  44. }
  45. }
  46. private def makeObjectId(id: String): Option[ObjectId] =
  47. if (ObjectId.isValid(id)) Some(new ObjectId(id))
  48. else None
  49. private def objectIdQuery(id: ObjectId): MongoDBObject =
  50. MongoDBObject("_id" -> id)
  51. private def objectIdQuery(id: String): Option[MongoDBObject] =
  52. makeObjectId(id).map(objectIdQuery(_))
  53. def byId(id: String) = DBClient.byId[Template](id)
  54. def getUserTemplates(username: String) = {
  55. val fields = MongoDBObject("templates" -> 1, "_id" -> 0)
  56. db("users").findOne(MongoDBObject("username" -> username), fields) map {
  57. _.getAsOrElse[Seq[ObjectId]]("templates", Seq[ObjectId]()) map (_.toString)
  58. }
  59. }
  60. def addItem(id: String, item: TemplateItem) = {
  61. val col = db("templates")
  62. val upd = MongoDBObject("$push" -> MongoDBObject("items" -> item.toMongoObject))
  63. objectIdQuery(id) map(col.update(_, upd))
  64. }
  65. def delete(id: String) = {
  66. val col = db("templates")
  67. objectIdQuery(id) map(col.remove(_))
  68. }
  69. def deleteItem(id: String, itemid: Int) = {
  70. val col = db("templates")
  71. val upd = MongoDBObject("$pull" -> MongoDBObject("items" -> MongoDBObject("id" -> itemid)))
  72. objectIdQuery(id) map(col.update(_, upd))
  73. }
  74. }