Created
February 21, 2016 00:43
-
-
Save khajavi/50e2e745250e4e629809 to your computer and use it in GitHub Desktop.
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
postgres = { | |
driver = org.postgresql.Driver | |
url = "jdbc:postgresql://localhost:5432/test" | |
user = "postgres" | |
password = "postgres" | |
connectionPool = disabled | |
keepAliveConnection = true | |
} |
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 controllers | |
import play.api.libs.json._ | |
import play.api.mvc._ | |
import slick.driver.PostgresDriver.api._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
object Application extends Controller { | |
implicit val bookWrites = Json.writes[Book] | |
implicit val bookReads = Json.reads[Book] | |
def saveBook = Action.async(BodyParsers.parse.json) { implicit request => | |
request.body.validate[Book].fold( | |
err => Future.successful(BadRequest), | |
book => BookDAO.insert(book).map(i => Ok) | |
) | |
} | |
def findById(id: Int) = Action.async { | |
BookDAO.findById(id).map { | |
case i: Book => Ok | |
case _ => BadRequest | |
} | |
} | |
def updateBook(id: Int) = Action.async(BodyParsers.parse.json) { implicit request => | |
request.body.validate[Book].fold( | |
error => Future.successful(BadRequest), | |
data => BookDAO.update(id, data).map { | |
case 0 => BadRequest | |
case 1 => Ok | |
} | |
) | |
} | |
def deleteBook(id: Int) = Action.async { | |
BookDAO.delete(id).map { | |
case 0 => BadRequest | |
case 1 => Ok | |
} | |
} | |
def listBooks = Action.async { | |
BookDAO.list.map(books => Ok(Json.toJson(books))) | |
} | |
} | |
trait DAO[T] { | |
def insert(o: T): Future[Int] | |
def update(id: Int, o: T): Future[Int] | |
def delete(id: Int): Future[Int] | |
def findById(id: Int): Future[T] | |
def count: Future[Int] | |
def list: Future[Seq[T]] | |
} | |
object BookDAO extends DAO[Book] { | |
private val books = TableQuery[Books] | |
private def db: Database = Database.forConfig("postgres") | |
override def insert(book: Book): Future[Int] = { | |
try db.run(books += book) | |
finally db.close | |
} | |
override def count: Future[Int] = { | |
try db.run(books.length.result) | |
finally db.close | |
} | |
override def update(id: Int, book: Book): Future[Int] = { | |
try db.run(books.filter(_.id === id).update(book)) | |
finally db.close | |
} | |
override def findById(id: Int): Future[Book] = { | |
try db.run(books.filter(_.id === id).result.head) | |
finally db.close | |
} | |
override def delete(id: Int): Future[Int] = { | |
try db.run(books.filter(_.id === id).delete) | |
finally db.close | |
} | |
override def list: Future[Seq[Book]] = { | |
try db.run(books.result) | |
finally db.close | |
} | |
} | |
case class Book(name: String, id: Option[Int] = None) | |
class Books(tag: Tag) extends Table[Book](tag, "BOOKS") { | |
def id = column[Int]("ID", O.PrimaryKey, O.AutoInc) | |
def name = column[String]("NAME") | |
def * = (name, id.?) <>(Book.tupled, Book.unapply) | |
} |
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
name := "async-play-crud-with-slick" | |
version := "1.0" | |
scalaVersion := "2.11.7" | |
resolvers += Resolver.mavenLocal | |
resolvers += "Typesafe Releases" at "http://repo.typesafe.com/typesafe/releases/" | |
lazy val root = project.in(file(".")).enablePlugins(PlayScala) | |
libraryDependencies ++= List( | |
"org.postgresql" % "postgresql" % "9.4-1206-jdbc42", | |
"org.slf4j" % "slf4j-nop" % "1.6.4", | |
"com.typesafe.slick" %% "slick" % "3.0.0-RC1" | |
) | |
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
GET /books controllers.Application.listBooks | |
POST /books/save controllers.Application.saveBook | |
GET /books/find/:id controllers.Application.findById(id:Int) | |
POST /books/update/:id controllers.Application.updateBook(id:Int) | |
GET /books/delete/:id controllers.Application.deleteBook(id:Int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment