-
-
Save pedrofurla/9790366 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
/** | |
* Created by pedrofurla on 26/03/14. | |
*/ | |
package slicks.docs | |
import slicks.docs.dao.{Entity, IdentifiableTable, CrudComponent, Profile} | |
case class User(id: Option[Long], first: String, last: String) extends Entity[Long] | |
trait UserComponent extends CrudComponent { outer: Profile => | |
import simple._ | |
class UsersTable(tag: Tag) extends Table[User](tag, "users") with IdentifiableTable[Long] { | |
override def id = column[Long]("id", O.PrimaryKey, O.AutoInc) | |
def first = column[String]("first") | |
def last = column[String]("last") | |
def * = (id.?, first, last) <> (User.tupled, User.unapply) | |
} | |
class UserRepository(implicit session: Session) extends Crud[UsersTable, User, Long] { | |
val query = TableQuery[UsersTable] | |
override def withId(user: User, id: Long): User = user.copy(id = Option(id)) | |
} | |
} |
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
/** | |
* Created by pedrofurla on 26/03/14. | |
*/ | |
package slicks.docs.dao | |
trait Profile { | |
val profile: scala.slick.driver.JdbcProfile | |
val simple:profile.simple.type = profile.simple | |
} | |
trait CrudComponent { this: Profile => | |
import simple._ | |
abstract class Crud[T <: Table[E] with IdentifiableTable[PK], E <: Entity[PK], PK: BaseColumnType](implicit session: Session) { | |
val query: TableQuery[T] | |
def count: Int = query.length.run | |
def findAll: List[E] = query.list() | |
def queryById(id: PK) = query.filter(_.id === id) | |
def findOne(id: PK): Option[E] = queryById(id).firstOption | |
def add(m: E): PK = (query returning query.map(_.id)) += m | |
def withId(model: E, id: PK): E | |
def extractId(m: E): Option[PK] = m.id | |
def save(m: E): E = extractId(m) match { | |
case Some(id) => | |
queryById(id).update(m) | |
m | |
case None => withId(m, add(m)) | |
} | |
def saveAll(ms: E*): Option[Int] = query ++= ms | |
def deleteById(id: PK): Int = queryById(id).delete | |
def delete(m: E): Int = extractId(m) match { | |
case Some(id) => deleteById(id) | |
case None => 0 | |
} | |
} | |
} | |
trait Entity[PK] { | |
def id: Option[PK] | |
} | |
trait IdentifiableTable[I] { | |
def id: scala.slick.lifted.Column[I] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment