PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/app/database/mongodb/User.scala

https://gitlab.com/Memoria/Memoria
Scala | 66 lines | 52 code | 11 blank | 3 comment | 2 complexity | 1b80572eed9724530c8809c2abe60b95 MD5 | raw file
  1. package database.mongodb
  2. import com.mongodb.casbah.Imports._
  3. import models.UserModel.UserModel
  4. import scala.collection.mutable
  5. /**
  6. * Created by lrbpi on 17/01/2016.
  7. */
  8. object User {
  9. val mongoClient = MongoClient("localhost", 27017)
  10. val db = mongoClient("Memoria")
  11. val coll = User.db("users")
  12. def findAll () : mutable.Set[UserModel] = {
  13. val result : mutable.Set[UserModel] = mutable.Set[UserModel]()
  14. val users = User.coll.find()
  15. for(user <- users) {
  16. result += UserModel(
  17. user.get("_id").toString,
  18. user.get("login").toString,
  19. user.get("password").toString,
  20. user.get("firstName").toString,
  21. user.get("lastName").toString,
  22. user.get("rank").toString
  23. )
  24. }
  25. result
  26. }
  27. def findUser (login : String, password : String) : Option[UserModel] = {
  28. var result : Option[UserModel] = None
  29. val q = MongoDBObject("login" -> login, "password" -> password )
  30. val cursor = User.coll.findOne(q)
  31. if (cursor.nonEmpty) {
  32. result = Some(UserModel(
  33. cursor.get("_id").toString,
  34. cursor.get.get("login").toString,
  35. cursor.get.get("password").toString,
  36. cursor.get.get("firstName").toString,
  37. cursor.get.get("lastName").toString,
  38. cursor.get.get("rank").toString
  39. ))
  40. }
  41. result
  42. }
  43. def createUser (userModel : UserModel) : Boolean = {
  44. val coll = User.db("users")
  45. val builder = MongoDBObject.newBuilder
  46. builder += "login" -> userModel.login
  47. builder += "password" -> userModel.password
  48. builder += "firstName" -> userModel.firstName
  49. builder += "lastName" -> userModel.lastName
  50. builder += "rank" -> userModel.rank
  51. val user = builder.result
  52. coll.insert(user)
  53. true
  54. }
  55. }