Created
January 22, 2013 13:23
-
-
Save MagnusSmith/4594635 to your computer and use it in GitHub Desktop.
My first attempt to use slick with play framework shows a slick table defined using a case class and a few helper methods to create, find, update and delete records
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 models | |
import scala.slick.driver.H2Driver.simple._ | |
import scala.slick.session.Session | |
/** | |
* Created with IntelliJ IDEA. | |
* User: Magnus.Smith | |
* Date: 21/01/13 | |
* Time: 16:20 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
case class ThirdParty(id: Option[Int] = None, var name: String, var reference: String, var webAddress: String, var primaryPhone: String, | |
var secondaryPhone: String, var email: String, var valid: Boolean = true) | |
object ThirdParties extends Table[ThirdParty]("THIRD_PARTIES") { | |
def id = column[Int]("SUP_ID", O.PrimaryKey, O.AutoInc) | |
def name = column[String]("NAME") | |
def reference = column[String]("REFERENCE") | |
def webAddress = column[String]("WEB_ADDRESS") | |
def primaryPhone = column[String]("PRIMARY_PHONE") | |
def secondaryPhone = column[String]("SECONDARY_PHONE") | |
def email = column[String]("EMAIL") | |
def valid = column[Boolean]("VALID") | |
def * = id.? ~ name ~ reference ~ webAddress ~ primaryPhone ~ secondaryPhone ~ email ~ valid <> (ThirdParty, ThirdParty.unapply _) | |
def autoInc = id.? ~ name ~ reference ~ webAddress ~ primaryPhone ~ secondaryPhone ~ email ~ valid <> (ThirdParty, ThirdParty.unapply _) returning id | |
def findAll()(implicit session: Session): List[ThirdParty] = { | |
val s = (for (s <- ThirdParties) yield s) | |
s.list map { case t: ThirdParty => t } | |
} | |
def findByName(name: String)(implicit session: Session): List[ThirdParty] = { | |
(for { | |
s <- ThirdParties | |
if (s.name === name) | |
} yield(s)).list map { case t: ThirdParty => t } | |
} | |
def findById(id: Int)(implicit session: Session): Option[ThirdParty] = { | |
val s = (for { | |
s <- ThirdParties | |
if (s.id === id) | |
} yield(s)).list map { case t: ThirdParty => t } | |
if (s.size > 0) Some(s.head) | |
else None | |
} | |
def countByName(name: String)(implicit session: Session) = { | |
(for { | |
s <- ThirdParties | |
if (s.name === name) | |
} yield(s)).list.size | |
} | |
def create(thirdParty: ThirdParty)(implicit session: Session): Int = { | |
ThirdParties.autoInc.insert(thirdParty) | |
} | |
def update(thirdParty: ThirdParty)(implicit session: Session) = { | |
ThirdParties.where(_.id === thirdParty.id.get).update(thirdParty) | |
} | |
def delete(thirdParty: ThirdParty)(implicit session: Session) = { | |
ThirdParties.where(_.id === thirdParty.id.get).delete | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment