Skip to content

Instantly share code, notes, and snippets.

@lauris
Created January 12, 2018 19:42
Show Gist options
  • Save lauris/d5448ec446d252690c346bd1eb94a879 to your computer and use it in GitHub Desktop.
Save lauris/d5448ec446d252690c346bd1eb94a879 to your computer and use it in GitHub Desktop.
package models
import play.api.db.slick.Config.driver.simple._
import java.sql.Timestamp
case class ChartCase(id: Option[Long], title: String, query: String, settings: String, db: String, user: Long,
conn: Long, created: Timestamp, team_shared: Boolean = false, public: Boolean = false)
class Chart(tag: Tag) extends Table[ChartCase](tag, "charts") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def title = column[String]("title")
def query = column[String]("query")
def settings = column[String]("settings")
def db = column[String]("db")
def user = column[Long]("user_id")
def conn = column[Long]("conn_id")
def created = column[Timestamp]("created_at", O.NotNull)
def team_shared = column[Boolean]("team_shared", O.Default(false))
def public = column[Boolean]("public", O.Default(false))
def * = (id.?, title, query, settings, db, user, conn, created, team_shared, public) <>(ChartCase.tupled,
ChartCase.unapply)
}
object Chart {
lazy val charts = TableQuery[Chart]
def findById(id: Long, user: Long)(implicit s: Session): Option[ChartCase] =
charts.filter(c => c.id === id && c.user === user).firstOption
def findEmbedById(id: Long)(implicit s: Session): Option[ChartCase] =
charts.filter(c => c.id === id && c.public === true).firstOption
def deleteById(id: Long, user: Long)(implicit s: Session): Int =
charts.filter(c => c.id === id && c.user === user).delete
def findAll(conn: Long, db: String, user: Long)(implicit s: Session): List[ChartCase] =
charts.filter(c => c.user === user && c.conn === conn && c.db === db)
.sortBy(_.created.desc).list
def save(id: Long, user: Long, chart: ChartCase)(implicit s: Session): Int = {
charts.filter(c => c.id === id && c.user === user).update(chart)
}
def create(chart: ChartCase)(implicit s: Session): ChartCase = {
val id = (charts returning charts.map(_.id)) += chart
chart.copy(id = Option(id.toLong))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment