Skip to content

Instantly share code, notes, and snippets.

@dsugden
Last active December 11, 2015 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsugden/4543765 to your computer and use it in GitHub Desktop.
Save dsugden/4543765 to your computer and use it in GitHub Desktop.
Articles Slick object
import play.api.db._
import play.api.Play.current
import java.sql.Date
import scala.slick.driver.PostgresDriver.simple._
import play.Logger
import scalaz.Failure
import scala.slick.lifted.Projection
import Users._
import scala.slick.lifted.BaseTypeMapper
import scala.slick.driver.BasicProfile
import scala.util.{ Try, Success, Failure }
sealed trait ArticleCategory
case object NEWS extends ArticleCategory
case object BLOG extends ArticleCategory
case object PORTFOLIO extends ArticleCategory
case object TEAMBIO extends ArticleCategory
object Articles {
lazy val database = Database.forDataSource(DB.getDataSource())
val articleCategoryMappings = Map("NEWS" -> NEWS,
"BLOG" -> BLOG,
"PORTFOLIO" -> PORTFOLIO,
"TEAMBIO" -> TEAMBIO)
val ArticleTable = new Table[Article]("Article") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def body = column[Option[String]]("body")
def publishedBody = column[Option[String]]("publishedBody")
def userID = column[Long]("user_Id")
def created = column[Date]("created")
def category = column[String]("category")
def published = column[Boolean]("published")
def displayOrder = column[Int]("displayOrder")
def userFK = foreignKey("USER_FK", userID, UserTable)(_.id)
def * = id.? ~ name ~ body ~ userID ~ created ~ category ~ published ~ displayOrder ~ publishedBody <> (Article, Article.unapply _)
def forInsert = name ~ body ~ userID ~ created ~ category ~ published ~ displayOrder ~ publishedBody <>
({ (name, body, userID, created, category, published, displayOrder, publishedBody) => Article(None, name, body, userID, created, category, published, displayOrder, publishedBody) },
{ u: Article => Some((u.name, u.body, u.userID, u.created, u.category, u.published, u.displayOrder, u.publishedBody)) }) returning id
}
/**
* Returns the ArticleId of newly created row, or throws error
*/
def create(userId: Long, articleForCreation: ArticleForCreation): Try[ArticleId] = database.withSession { implicit db: Session =>
Try(
ArticleId(
ArticleTable.forInsert.insert(
Article(None,
articleForCreation.name,
articleForCreation.body,
userId,
new java.sql.Date(System.currentTimeMillis()),
articleForCreation.category.toString(),
articleForCreation.published,
-1,
None))))
}
/**
* Updates table row
* Throws error
*/
def update(article: ArticleForUpdate): Try[Unit] = database.withSession { implicit db: Session =>
Try((ArticleTable.filter(a => a.id === article.id).map(a => a.name ~ a.body ~ a.displayOrder))
.update(article.name, article.body, article.displayOrder))
}
def findByName(name: String): Option[Article] = database.withSession { implicit db: Session =>
(for (u <- ArticleTable; if u.name === name) yield u).firstOption
}
def findById(id: Long): Option[Article] = database.withSession { implicit db: Session =>
(for (u <- ArticleTable; if u.id === id) yield u).firstOption
}
def findAllByCategory(category: ArticleCategory): List[Article] = database.withSession { implicit db: Session =>
(for (u <- ArticleTable; if u.category === category.toString) yield u).sortBy(a => a.displayOrder).list
}
def findAllForTableByCategory(category: ArticleCategory): List[ArticleForTable] = database.withSession { implicit db: Session =>
val query = for (
a <- ArticleTable;
if a.category === category.toString;
u <- UserTable;
if (a.userID === u.id)
) yield ((a, u.email))
val articleList = query.sortBy(a => a._1.displayOrder).list
for (tuple <- articleList) yield {
val article = tuple._1
ArticleForTable(article.id, article.name, article.userID, tuple._2, article.created)
}
}
def delete(id: Long): Try[Unit] = database.withSession { implicit db: Session =>
Try(ArticleTable.filter(_.id === id).delete == 1)
}
def deleteByName(name: String): Try[Unit] = database.withSession { implicit db: Session =>
Try(ArticleTable.filter(_.name === name).delete)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment