Here are the codes:
def find(loginInfo: LoginInfo): Future[Option[UserProfile]] = {
val res = DB.withSession { implicit session =>
//if loginInfo.providerID == "waylens"
userProfileTable.filter(u => u.userName === loginInfo.providerKey).list
}
val size = res.size
if (size <= 1)
Future(res.headOption.map(userProfileRecordToUserProfile))
else
throw new Exception("db error")
}
def findByEmail(providerID: String, email: String): Future[Option[UserProfile]] = {
val res = DB.withSession { implicit session =>
//if loginInfo.providerID == "waylens"
userProfileTable.filter(u => u.email === email && u.status === 1).list
}
val size = res.size
if (size <= 1)
Future(res.headOption.map(userProfileRecordToUserProfile))
else
throw new Exception("db error")
}
def findByProfileID(profileID: Long): Future[Option[UserProfile]] = {
val res = DB.withSession { implicit session =>
userProfileTable.filter(u => u.id === profileID).list
}
val size = res.size
if (size <= 1)
Future(res.headOption.map(userProfileRecordToUserProfile))
else
throw new Exception("db error")
}
These codes appeared many times, which is really annoying
val size = res.size
if (size <= 1)
Future(res.headOption.map(userProfileRecordToUserProfile))
else
throw new Exception("db error")
Does anyone have ideas about refactoring this in Slick?