Created
December 17, 2013 23:51
-
-
Save ctcarrier/8014990 to your computer and use it in GitHub Desktop.
Basic ReactiveMongo DAO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blrest.dao | |
import scala.concurrent.Future | |
import akka.actor.ActorSystem | |
import reactivemongo.bson.BSONDocument | |
import reactivemongo.api.{QueryOpts, DB} | |
import reactivemongo.core.commands.Count | |
import reactivemongo.api.collections.default._ | |
import com.typesafe.scalalogging.slf4j.Logging | |
import com.blrest._ | |
import com.blrest.model.ImageMeta | |
import scala.util.Random | |
/** | |
* Created by ccarrier for bl-rest. | |
* at 10:00 PM on 12/14/13 | |
*/ | |
trait ImageDirectoryDao { | |
def getImageMetaData(key: Long): Future[Option[ImageMeta]] | |
def getRandomImageMetaData: Future[Option[ImageMeta]] | |
} | |
class ImageDirectoryReactiveDao(db: DB, imageCollection: BSONCollection, system: ActorSystem) extends ImageDirectoryDao with Logging { | |
implicit val context = system.dispatcher | |
def getImageMetaData(key: Long): Future[Option[ImageMeta]] = { | |
logger.debug("Getting image: %s".format(key)) | |
val query = BSONDocument("flickr.flickr_id" -> key) | |
imageCollection.find(query).one[ImageMeta] | |
} | |
def getRandomImageMetaData: Future[Option[ImageMeta]] = { | |
val futureCount = db.command(Count(imageCollection.name)) | |
futureCount.flatMap { count => | |
val skip = Random.nextInt(count) | |
imageCollection.find(BSONDocument()).options(QueryOpts(skipN = skip)).one[ImageMeta] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment