Created
October 20, 2014 20:54
-
-
Save swoogles/7e61a1015b64d1e52658 to your computer and use it in GitHub Desktop.
This file contains 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 models | |
import anorm.SQL | |
import anorm.SqlStringInterpolation | |
import java.sql.Connection | |
import anorm.RowParser | |
import anorm.ResultSetParser | |
trait Findable[T] { | |
val table: String | |
val pk: String | |
val parser: RowParser[T] | |
def findById(id: Int)(implicit connection:Connection): Option[T] = { | |
val query = s"SELECT * FROM $table WHERE $pk = $id" | |
SQL(query).as(parser.singleOpt) | |
} | |
lazy val multiParser: ResultSetParser[List[T]] = { | |
parser * | |
} | |
} | |
trait FindableBySubscriber[T] extends Findable[T]{ | |
def findBySubscriber(id: Int)(implicit connection:Connection): List[T] = { | |
val query = s"SELECT * FROM $table WHERE subscriber_idx = $id" | |
SQL(query).as(multiParser) | |
} | |
} | |
trait FindableByPatron[T] extends Findable[T]{ | |
def findByPatron(id: Int)(implicit connection:Connection): Option[T] = { | |
val query = s"SELECT * FROM $table WHERE user_idx = $id" | |
SQL(query).as(parser.singleOpt) | |
} | |
} | |
trait SRModel[T] | |
extends Findable[T] | |
with FindableBySubscriber[T] | |
with FindableByPatron[T] | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment