PageRenderTime 145ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/scala/com/trailmagic/jumper/core/mongodb/MongoUserRepository.scala

https://github.com/cyrusinnovation/inquisition
Scala | 74 lines | 63 code | 10 blank | 1 comment | 2 complexity | 724b296e294ae1963e7f9ffc4bc1f4c2 MD5 | raw file
  1. package com.trailmagic.jumper.core.mongodb
  2. import org.springframework.stereotype.Repository
  3. import org.springframework.beans.factory.annotation.Autowired
  4. import com.mongodb.casbah.Imports._
  5. import com.novus.salat._
  6. import com.novus.salat.global._
  7. import com.mongodb.MongoException.DuplicateKey
  8. import com.trailmagic.jumper.core.{SavedUser, User, UserRepository}
  9. import com.trailmagic.jumper.core.security.NonUniqueEmailException
  10. import com.trailmagic.jumper.core.security.NonUniqueUsernameException
  11. @Repository
  12. class MongoUserRepository @Autowired()(db: MongoDB) extends UserRepository {
  13. private val users = db("users")
  14. users.ensureIndex(MongoDBObject("username" -> 1), "username_uniqueness", true)
  15. users.ensureIndex(MongoDBObject("email" -> 1), "email_uniqueness", true)
  16. def save(user: SavedUser): SavedUser = {
  17. try {
  18. users.update(MongoDBObject("_id" -> user.id), grater[User].asDBObject(user.user), false, false, WriteConcern.Safe)
  19. } catch {
  20. // this is shady since it could also be a username, but we won't let people change that and mongo doesn't seem to tell us, so...there it is
  21. case e: DuplicateKey => throw new NonUniqueEmailException(user.email + " is not unique")
  22. }
  23. user
  24. }
  25. def save(user: User): SavedUser = {
  26. val dbObject = grater[User].asDBObject(user)
  27. val newId = user.username.toLowerCase
  28. try {
  29. users.insert(MongoDBObject("_id" -> newId) ++ dbObject, WriteConcern.Safe)
  30. } catch {
  31. case e: DuplicateKey if e.getMessage.contains("_id") => throw new NonUniqueUsernameException(user.username + " is not unique")
  32. case e: DuplicateKey if e.getMessage.contains("email_uniqueness") => throw new NonUniqueEmailException(user.email + " is not unique")
  33. }
  34. SavedUser(newId, user)
  35. }
  36. def findById(id: String): Option[SavedUser] = {
  37. val lowerCaseId = id.toLowerCase
  38. val query = MongoDBObject("_id" -> lowerCaseId)
  39. users.findOne(query) match {
  40. case None => None
  41. case Some(obj: DBObject) => Some(SavedUser(lowerCaseId, grater[User].asObject(obj)))
  42. }
  43. }
  44. def dbObj2SavedUser(obj: DBObject): SavedUser = {
  45. SavedUser(obj("_id").toString, grater[User].asObject(obj))
  46. }
  47. def findByUsername(username: String) = {
  48. users.findOne(MongoDBObject("_id" -> username.toLowerCase)) match {
  49. case None => None
  50. case Some(obj: DBObject) => Some(dbObj2SavedUser(obj))
  51. }
  52. }
  53. def findUsers(usernames: List[String]): Map[String, SavedUser] = {
  54. users.find("_id" $in usernames.map(_.toLowerCase)).foldLeft(Map[String, SavedUser]())(
  55. (usernameUserMap, obj) => {
  56. val user = dbObj2SavedUser(obj)
  57. usernameUserMap + (user.username -> user)
  58. }
  59. )
  60. }
  61. def findWithFacebookIds(ids: Set[String]): Set[User] = {
  62. users.find("facebookId" $in ids).map(dbObj2SavedUser(_)).toSet
  63. }
  64. }