Skip to content

Instantly share code, notes, and snippets.

@jmparsons
Last active December 18, 2015 09:39
Show Gist options
  • Save jmparsons/5762505 to your computer and use it in GitHub Desktop.
Save jmparsons/5762505 to your computer and use it in GitHub Desktop.
Pages model difference between anorm and slick.
package models
import java.util.Date
import play.api.db._
import play.api.Play.current
import play.api.db.slick.DB
import play.api.db.slick.Config.driver.simple._
case class Page(id: Option[Long] = None, title: String, slug: String, content: String, posted_at: Date)
object Pages extends Table[Page]("page") {
implicit val javaUtilDateTypeMapper = MappedTypeMapper.base[java.util.Date, java.sql.Date](
x => new java.sql.Date(x.getTime),
x => new java.util.Date(x.getTime)
)
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def title = column[String]("title")
def slug = column[String]("slug")
def content = column[String]("content")
def posted_at = column[Date]("posted_at")
def * = id.? ~ title ~ slug ~ content ~ posted_at <>(Page.apply _, Page.unapply _)
def autoInc = title ~ slug ~ content ~ posted_at returning id
val byId = createFinderBy(_.id)
val bySlug = createFinderBy(_.slug)
def create(page: Page) = DB.withSession { implicit session =>
Pages.autoInc.insert(page.title, page.slug, page.content, page.posted_at)
}
def update(id: Long, page: Page) = DB.withSession { implicit session =>
val pageToUpdate: Page = page.copy(Some(id))
Pages.where(_.id === id).update(pageToUpdate)
}
def findById(id:Long): Option[Page] = DB.withSession { implicit session =>
Pages.byId(id).firstOption
}
def findBySlug(slug: String): Option[Page] = DB.withSession { implicit session =>
Pages.bySlug(slug).firstOption
}
def all(): List[Page] = DB.withSession { implicit session =>
(for(m <- Pages) yield m).sortBy(_.id).list
}
def count: Int = DB.withSession { implicit session =>
Query(Pages.length).first
}
}
package models
import java.util.Date
import anorm._
import anorm.SqlParser._
import play.api.data._
import play.api.data.Forms._
import play.api.db._
import play.api.Play.current
case class Page(id: Pk[Long] = NotAssigned, title: String, slug: String, content: String, posted_at: Date)
object Page {
val simple = {
get[Pk[Long]]("page.id") ~
get[String]("page.title") ~
get[String]("page.slug") ~
get[String]("page.content") ~
get[Date]("page.posted_at") map {
case id ~ title ~ slug ~ content ~ posted_at => Page(
id, title, slug, content, posted_at
)
}
}
def create(page: Page) = {
DB.withConnection { implicit c =>
SQL(
"""
insert into page(title, slug, content, posted_at) values (
{title}, {slug}, {content}, {posted_at}
)
"""
).on(
'title -> page.title,
'slug -> page.slug,
'content -> page.content,
'posted_at -> page.posted_at
).executeUpdate()
}
}
def update(id: Long, page: Page) = {
DB.withConnection { implicit c =>
SQL(
"""
update page
set title = {title}, slug = {slug}, content = {content}, posted_at = {posted_at}
where id = {id}
"""
).on(
'id -> id,
'title -> page.title,
'slug -> page.slug,
'content -> page.content,
'posted_at -> page.posted_at
).executeUpdate()
}
}
def findById(id: Long): Option[Page] = {
DB.withConnection { implicit c =>
SQL("select * from page where id = {id}").on(
'id -> id
).as(Page.simple.singleOpt)
}
}
def findBySlug(slug: String): Option[Page] = {
DB.withConnection { implicit c =>
SQL("select * from page where slug = {slug}").on(
'slug -> slug
).as(Page.simple.singleOpt)
}
}
def count() = {
DB.withConnection { implicit connection =>
SQL("select count(*) from page").as(scalar[Long].single)
}
}
def all(): List[Page] = {
DB.withConnection { implicit connection =>
SQL("select * from page").as(Page.simple *)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment