Created
June 10, 2013 21:28
-
-
Save edofic/5752483 to your computer and use it in GitHub Desktop.
reactive mongo without play
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 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) | |
} |
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 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