-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create bare play project with models
- Loading branch information
Showing
11 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package controllers | ||
|
||
import play.api.mvc.{Action, Controller} | ||
|
||
/** | ||
* Created by Harold on 2016-06-04. | ||
*/ | ||
class Application extends Controller { | ||
|
||
def index = Action { | ||
Ok(views.html.default("hello")) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package models | ||
|
||
import javax.inject.Inject | ||
|
||
import play.api.db.slick.DatabaseConfigProvider | ||
import slick.lifted.Tag | ||
import slick.driver.H2Driver.api._ | ||
import util.BaseRepo | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
/** | ||
* Created by Harold on 2016-06-04. | ||
*/ | ||
case class Article(id: Long, title: String, body: String) | ||
class ArticleTable(tag: Tag) extends Table[Article](tag, "article") { | ||
val id = column[Long]("id", O.AutoInc, O.PrimaryKey) | ||
val title = column[String]("title") | ||
val body = column[String]("body") | ||
|
||
override def * = (id, title, body) <> (Article.tupled, Article.unapply) | ||
} | ||
class ArticleRepo @Inject()(dbConfigProvider: DatabaseConfigProvider) extends BaseRepo(dbConfigProvider) { | ||
val articles = TableQuery[ArticleTable] | ||
|
||
def addArticle(title: String, body: String): Future[Article] = { | ||
val article = Article(0, title, body) | ||
val action = articles returning articles.map(_.id) into ((a, id) => a.copy(id = id)) | ||
db.run(action += article) | ||
} | ||
|
||
def deleteArticle(id: Long): Future[Int] = { | ||
val action = articles.filter(_.id === id).delete | ||
db.run(action) | ||
} | ||
|
||
def listArticles: Future[Seq[(String, String)]] = { | ||
val action = articles.map(article => (article.title, article.body)) | ||
db.run(action.result) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package models | ||
|
||
import javax.inject.Inject | ||
|
||
import play.api.db.slick.DatabaseConfigProvider | ||
import slick.lifted.{TableQuery, Tag} | ||
import slick.driver.H2Driver.api._ | ||
import util.BaseRepo | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
/** | ||
* Created by Harold on 2016-06-04. | ||
*/ | ||
case class Pinboard(id: Long, name: String) | ||
class PinboardTable(tag: Tag) extends Table[Pinboard](tag, "pinboard") { | ||
def id = column[Long]("id", O.AutoInc, O.PrimaryKey) | ||
def name = column[String]("name") | ||
|
||
override def * = (id, name) <> (Pinboard.tupled, Pinboard.unapply) | ||
} | ||
class PinboardRepo @Inject()(dbConfigProvider: DatabaseConfigProvider) extends BaseRepo(dbConfigProvider) { | ||
val pinboards = TableQuery[PinboardTable] | ||
|
||
def addPinboard(name: String): Future[Pinboard] = { | ||
val pinboard = Pinboard(0, name) | ||
val action = pinboards returning pinboards.map(_.id) into ((p, id) => p.copy(id = id)) | ||
db.run(action += pinboard) | ||
} | ||
|
||
def deletePinboard(id: Long): Future[Int] = { | ||
val action = pinboards.filter(_.id === id).delete | ||
db.run(action) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package models | ||
|
||
import javax.inject.Inject | ||
|
||
import org.joda.time.DateTime | ||
import play.api.db.slick.DatabaseConfigProvider | ||
import slick.driver.H2Driver.api._ | ||
import slick.lifted.Tag | ||
import util.BaseRepo | ||
import com.github.tototoshi.slick.H2JodaSupport._ | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
/** | ||
* Created by Harold on 2016-06-04. | ||
*/ | ||
case class PinboardArticle(pinboardId: Long, articleId: Long, pinnedAt: DateTime) | ||
class PinboardArticleTable(tag: Tag) extends Table[PinboardArticle](tag, "pinboard_article") { | ||
val pinboards = TableQuery[PinboardTable] | ||
val articles = TableQuery[ArticleTable] | ||
|
||
val pinboardId = column[Long]("pinboard_id") | ||
val articleId = column[Long]("article_id") | ||
val pinnedAt = column[DateTime]("pinned_at") | ||
|
||
val pinboardFk = foreignKey("pinboard_fk", pinboardId, pinboards)(_.id) | ||
val articleFk = foreignKey("article_fk", articleId, articles)(_.id) | ||
|
||
override def * = (pinboardId, articleId, pinnedAt) <> (PinboardArticle.tupled, PinboardArticle.unapply) | ||
} | ||
class PinboardArticleRepo @Inject()(dbConfigProvider: DatabaseConfigProvider) extends BaseRepo(dbConfigProvider) { | ||
val pinboardArticles = TableQuery[PinboardArticleTable] | ||
val pinboards = TableQuery[PinboardTable] | ||
val articles = TableQuery[ArticleTable] | ||
|
||
def pinArticle(pinboardId: Long, articleId: Long): Future[PinboardArticle] = { | ||
val pinboardArticle = PinboardArticle(pinboardId, articleId, new DateTime()) | ||
val action = pinboardArticles += pinboardArticle | ||
db.run(action).map(_ => pinboardArticle) | ||
} | ||
|
||
def unpinArticle(pinboardId: Long, articleId: Long): Future[Int] = { | ||
val action = pinboardArticles.filter(p => p.pinboardId === pinboardId && p.articleId === articleId).delete | ||
db.run(action) | ||
} | ||
|
||
def listArticles(pinboardId: Long): Future[Seq[(String, String)]] = { | ||
val query = pinboardArticles.filter(_.pinboardId === pinboardId) | ||
val joinQuery = query join articles on { | ||
case (pa, article) => pa.articleId === article.id | ||
} | ||
val namedQuery = joinQuery.map{ | ||
case (pa, article) => (article.title, article.body) | ||
} | ||
db.run(namedQuery.result) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package util | ||
|
||
import play.api.db.slick.DatabaseConfigProvider | ||
import slick.driver.JdbcProfile | ||
import slick.lifted.TableQuery | ||
|
||
/** | ||
* Created by Harold on 2016-06-04. | ||
*/ | ||
class BaseRepo(protected val dbConfigProvider: DatabaseConfigProvider) { | ||
protected val db = dbConfigProvider.get[JdbcProfile].db | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@(message: String) | ||
@message |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name := "rest_pinboard_api" | ||
|
||
version := "1.0" | ||
|
||
scalaVersion := "2.11.8" | ||
|
||
lazy val root = (project in file(".")).enablePlugins(PlayScala) | ||
|
||
libraryDependencies ++= Seq( | ||
"com.h2database" % "h2" % "1.4.192", | ||
"com.github.tototoshi" %% "slick-joda-mapper" % "2.2.0", | ||
"com.typesafe.play" %% "play-slick" % "2.0.0", | ||
"com.typesafe.play" %% "play-slick-evolutions" % "2.0.0" | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
slick{ | ||
driver = "slick.driver.H2Driver" | ||
db { | ||
driver = "org.h2.Driver" | ||
url = "jdbc:h2:mem:play" | ||
user = sa | ||
password = "" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GET / controllers.Application.index |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version = 0.13.11 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.2") |