I have upgrade to 0.9 and I see that there have been a lot of changes.
I have a controller where I'm calling my mongo instance to get some leadCategories:
val db = ReactiveMongoPlugin.db
lazy val collection = db("leadCategories")
def list(id: Long) = Action {
implicit request => {
Async {
implicit val reader = LeadCategory.LeadCategoryReader
val query = if (id > 0) BSONDocument("caregoryId" -> BSONLong(id)) else BSONDocument()
val found = collection.find(query)
found.toList.map {
lc =>
Ok(views.html.list(lc))
}
}
}
1. Handlers
2. Plugin
No longer works as before.
I looked at the provided example and it looks nice but pretty basic. I have also read the docs for the changes
but I can't figure it out how to change my controller to work with my current case classes. Maybe
I need to change them too? So my question is how should i implement the controller so that it works with 0.9.
package models
import org.joda.time.DateTime
import reactivemongo.bson._
import reactivemongo.bson.BSONLong
case class LeadCategory(
id: Option[BSONObjectID],
categoryId: Long,
categoryName: String,
creationDate: Option[DateTime],
updateDate: Option[DateTime],
words: List[Word]
)
case class Word(id: Option[BSONObjectID], word: String)
object LeadCategory {
implicit object LeadCategoryWriter extends BSONDocumentWriter[LeadCategory] {
def write(leadCategory: LeadCategory): BSONDocument = BSONDocument(
"_id" -> leadCategory.id.getOrElse(BSONObjectID.generate),
"caregoryId" -> BSONLong(leadCategory.categoryId),
"categoryName" -> BSONString(leadCategory.categoryName),
"creationDate" -> leadCategory.creationDate.map(date => BSONDateTime(date.getMillis)),
"updateDate" -> leadCategory.updateDate.map(date => BSONDateTime(date.getMillis)),
"words" -> leadCategory.words
)
}
implicit object LeadCategoryReader extends BSONDocumentReader[LeadCategory] {
def read(doc: BSONDocument): LeadCategory = {
LeadCategory(
doc.getAs[BSONObjectID]("_id"),
doc.getAs[BSONLong]("caregoryId").get.value,
doc.getAs[BSONString]("categoryName").get.value,
doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)),
doc.getAs[List[Word]]("words").toList.flatten)
}
}
}
object Word {
implicit object Word extends BSONDocumentWriter[Word] {
def write(word: Word): BSONDocument = BSONDocument(
"_id" -> word.id.getOrElse(BSONObjectID.generate),
"word" -> BSONString(word.word)
)
}
implicit object LeadCategoryReader extends BSONDocumentReader[LeadCategory] {
def read(doc: BSONDocument): Word = {
Word(
doc.getAs[BSONObjectID]("_id"),
doc.getAs[BSONString]("word").get.value
)
}
}
}