Skip to content

Instantly share code, notes, and snippets.

@edofic
Created June 10, 2013 21:28
Show Gist options
  • Save edofic/5752483 to your computer and use it in GitHub Desktop.
Save edofic/5752483 to your computer and use it in GitHub Desktop.
reactive mongo without play
package models
import com.typesafe.config.ConfigFactory
import reactivemongo.api.{DefaultDB, DB, MongoDriver}
import akka.event.slf4j.Logger
import reactivemongo.api.collections.default.BSONCollection
import scala.util.Try
object MongoDb {
private val logger = Logger("MongoDB")
private val config = ConfigFactory.load
private val server = config.getString("mongodb.server")
private val dbName = config.getString("mongodb.db")
private val driver = new MongoDriver
private val db = DB(dbName, driver.connection(List(server)))
def getCollection(name: String): BSONCollection = db(name)
}
package models
import ua.t3hnar.bcrypt.Password
import reactivemongo.bson._
import DefaultBSONHandlers._
import scala.concurrent.ExecutionContext.Implicits.global
import concurrent.Future
case class User(username: String, password: String) {
def checkPassword(cleartext: String): Boolean =
cleartext isBcrypted password
}
object User {
lazy val collection = MongoDb.getCollection("users")
implicit val userFormat = Macros.handler[User]
def find(username: String): Future[Option[User]] = {
val query = BSONDocument("username" -> username)
collection.find(query).one[User]
}
def create(username: String, passwordClearText: String): Future[User] = {
val user = User(username, passwordClearText.bcrypt)
collection insert user map { error =>
if(error.ok)
user
else
throw error
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment