Created
September 16, 2015 12:21
-
-
Save PuppetmasterDK/0f2bd8d1da2da5e29561 to your computer and use it in GitHub Desktop.
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 repositories | |
import reactivemongo.api.collections.default.BSONCollection | |
import reactivemongo.bson.{BSONDocument, BSONDocumentReader, BSONDocumentWriter, BSONObjectID} | |
import reactivemongo.core.errors.DatabaseException | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
import scala.util.{Failure, Success} | |
case class User(id: BSONObjectID, firstName: String, lastName: String, email: String, password: Option[String], state: String) | |
class UsersMongo(val collection: BSONCollection) { | |
implicit object UserWriter extends BSONDocumentWriter[User] { | |
def write(user: User): BSONDocument = { | |
BSONDocument( | |
"_id" -> user.id, | |
"firstName" -> user.firstName, | |
"lastName" -> user.lastName, | |
"email" -> user.email, | |
"password" -> user.password.getOrElse(""), | |
"state" -> user.state) | |
} | |
} | |
implicit object UserReader extends BSONDocumentReader[User] { | |
def read(rawUser: BSONDocument): User = { | |
User( | |
rawUser.getAs[BSONObjectID]("_id").get, | |
rawUser.getAs[String]("firstName").get, | |
rawUser.getAs[String]("lastName").get, | |
rawUser.getAs[String]("email").get, | |
rawUser.getAs[String]("password"), | |
rawUser.getAs[String]("state").get) | |
} | |
} | |
override def details(id: String): Future[Option[User]] = { | |
BSONObjectID.parse(id) match { | |
case Success(userId) => collection.find(BSONDocument("_id" -> userId)).one[User] | |
case Failure(_) => Future.successful(None) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment