Skip to content

Instantly share code, notes, and snippets.

@anuragsrivastava06
Last active July 23, 2017 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anuragsrivastava06/d20920047d6859d70067cbd0bc6e992e to your computer and use it in GitHub Desktop.
Save anuragsrivastava06/d20920047d6859d70067cbd0bc6e992e to your computer and use it in GitHub Desktop.
package com.knoldus.DAO.user
import com.google.inject.ImplementedBy
import com.knoldus.DAO.db.{DBComponent, PostgresDbComponent}
import com.knoldus.DAO.user.mappings.{User, UserMapping}
import scala.concurrent.Future
@ImplementedBy(classOf[UserPostgresComponent])
trait UserComponent extends UserMapping {
this: DBComponent =>
import driver.api._
/**
* Inserts user into database
*
* @param user
* @return
*/
def insert(user: User): Future[Int] = {
db.run(userInfo += user)
}
/**
* Fetches user detail using email
*
* @param email
* @return Future[Option[User]]
**/
def getUserByEmail(email: String): Future[Option[User]] = {
db.run(userInfo.filter(user => user.email === email).result.headOption)
}
/**
* Fetches user record with the help of userId
*
* @param userId
* @return Option[User]
*/
def getUserByUserId(userId: String): Future[Option[User]] = {
db.run(userInfo.filter(user => user.id === userId).result.headOption)
}
/**
* Fetches All user from DB
*
* @return
*/
def getAllUsers: Future[List[User]] = {
db.run(userInfo.to[List].result)
}
/**
* Checks if user with user id exists
*
* @param userId
* @return
*/
def isUserIdExists(userId: String): Future[Boolean] = {
val query = userInfo.filter(user => user.id === userId).exists
db.run(query.result)
}
}
class UserPostgresComponent extends UserComponent with PostgresDbComponent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment