PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/app/models/User.scala

http://github.com/leodagdag/persistance
Scala | 35 lines | 28 code | 7 blank | 0 comment | 0 complexity | 71f647e99b1b2c4b0d0c4a6d2694232c MD5 | raw file
  1. package models
  2. import _root_.plugin._
  3. import com.novus.salat.dao._
  4. import com.mongodb.casbah.commons.Imports._
  5. case class User(_id: ObjectId = new ObjectId,
  6. username: String,
  7. password: String,
  8. email: String,
  9. admin: Boolean = false,
  10. firstName: Option[String] = None,
  11. lastName: Option[String] = None,
  12. bio: Option[String] = None) {
  13. lazy val fullname = {
  14. val fullname = List(this.firstName.getOrElse(""), this.lastName.getOrElse("")).filter(!"".equals(_)).map(_.capitalize)
  15. fullname.isEmpty match {
  16. case false => fullname.mkString(" ").trim()
  17. case _ => ""
  18. }
  19. }
  20. }
  21. object User extends SalatDAO[User, ObjectId](collection = DB.connection("User")) with Model[User, ObjectId] {
  22. def authenticate(username: String, password: String): Option[User] = {
  23. this.findOne(MongoDBObject("username" -> username, "password" -> password))
  24. }
  25. def byUsername(username: String): Option[User] = {
  26. this.findOne(MongoDBObject("username" -> username))
  27. }
  28. }