Created
November 3, 2013 12:25
-
-
Save volgar1x/7289741 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.photon.login | |
import com.twitter.util.Future | |
import org.photon.common.Async | |
import java.sql.{PreparedStatement, Timestamp, ResultSet} | |
import org.joda.time.Instant | |
trait UserRepositoryComponentImpl extends UserRepositoryComponent { self: DatabaseComponent with ExecutorComponent => | |
implicit def timestamp2instant(t: java.sql.Timestamp) = new Instant(t.getTime) | |
implicit def instant2timestamp(i: org.joda.time.Instant) = new Timestamp(i.getMillis) | |
def createUser(rset: ResultSet) = User( | |
rset.getLong("id"), | |
rset.getString("name"), | |
rset.getString("password"), | |
rset.getString("nickname"), | |
rset.getString("secret_question"), | |
rset.getString("secret_answer"), | |
rset.getInt("community_id"), | |
rset.getTimestamp("subscription_end"), | |
persisted = true | |
) | |
def setUserValues(s: PreparedStatement, user: User) { | |
s.setString(1, user.name) | |
s.setString(2, user.password) | |
s.setString(3, user.nickname) | |
s.setString(4, user.secretQuestion) | |
s.setString(5, user.secretAnswer) | |
s.setInt(6, user.communityId) | |
s.setTimestamp(7, user.subscriptionEnd) | |
} | |
val columns = Seq("id", "name", "nickname", "secret_question", "secret_answer", "community_id", "subscription_end") | |
def selectUserQuery(query: String) = s"SELECT ${columns.mkString(", ")} FROM users $query" | |
val updateUserQuery = s"UPDATE users SET ${columns.tail.mkString("=?, ")} WHERE id=?}" | |
val insertUserQuery = s"INSERT INTO users(${columns.tail.mkString(", ")}}) VALUES(${columns.tail.map(_ => "?").mkString(", ")}) RETURNING id" | |
val deleteUserQuery = "DELETE FROM users WHERE id=?" | |
class UserRepositoryImpl extends UserRepository { | |
def find(id: PrimaryKey): Future[User] = Async { | |
statement(selectUserQuery("WHERE id=?")) { s => | |
s.setLong(1, id) | |
resultsOf(s)(createUser).headOption getOrElse (throw UnknownUserException()) | |
} | |
} | |
def find(name: String): Future[User] = Async { | |
statement(selectUserQuery("WHERE name=?")) { s => | |
s.setString(1, name) | |
resultsOf(s)(createUser).headOption getOrElse (throw UnknownUserException()) | |
} | |
} | |
def persist(user: User): Future[User] = Async { | |
if (user.persisted) { | |
statement(updateUserQuery) { s => | |
setUserValues(s, user) | |
s.setLong(8, user.id) | |
s.executeUpdate() | |
user | |
} | |
} else { | |
statement(insertUserQuery) { s => | |
setUserValues(s, user) | |
resultOf(s) { rset => | |
user.copy(id = rset.getLong("id"), persisted = true) | |
}.head | |
} | |
} | |
} | |
def remove(o: User): Future[Unit] = Async { | |
statement(deleteUserQuery) { s => | |
s.setLong(1, o.id) | |
s.executeUpdate() | |
} | |
} | |
} | |
val users = new UserRepositoryImpl | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment