This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

select from mysql

  • Loading branch information...
Kuchitama committed Dec 21, 2015
1 parent 60dd111 commit 0c026f4992f18d3653010a106949f14b1b596cb9
View
@@ -0,0 +1,24 @@
package dao
import javax.inject.Singleton
import com.google.inject.Inject
import entities.Author
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.driver.JdbcProfile
/**
* Created by kuchitama on 15/12/21.
*/
@Singleton
class AuthorDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[JdbcProfile]{
import driver.api._
val authors = TableQuery[AuthorTable]
class AuthorTable(tag: Tag) extends Table[Author](tag, "author") {
def id = column[Long]("id")
def name = column[String]("name")
def * = (id, name) <> (Author.tupled, Author.unapply)
}
}
View
@@ -0,0 +1,27 @@
package dao
import javax.inject.Singleton
import com.google.inject.Inject
import entities.{Book, Author}
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.driver.JdbcProfile
/**
* Created by kuchitama on 15/12/21.
*/
@Singleton
class BookDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[JdbcProfile]{
import driver.api._
val books = TableQuery[BookTable]
class BookTable(tag: Tag) extends Table[Book](tag, "book") {
def id = column[Long]("id")
def title= column[String]("title")
def authorId = column[Long]("author_id")
def * = (id, title, authorId) <> (Book.tupled, Book.unapply)
}
def findAll() = books
}
@@ -2,5 +2,5 @@ package entities
case class Author(
id: Long,
name: String
name: String
)
View
@@ -3,14 +3,19 @@ package entities
case class Book (
id: Long,
title: String,
authorId: Long,
private val _author: Option[Author] = None
authorId: Long
) {
private var _author: Option[Author] = None
lazy val author = _author.get
}
object Book {
implicit class BookWithAuthor(tuple: (Book, Author)) {
def withAuthor = tuple._1.copy(_author = Some(tuple._2))
def withAuthor = {
tuple._1._author = Some(tuple._2)
tuple._1
}
}
def tupled = (Book.apply _).tupled
}
@@ -3,22 +3,32 @@ package repositories
import javax.inject.Singleton
import com.google.inject.Inject
import dao.{AuthorDao, BookDao}
import entities.{Author, Book}
import Book.BookWithAuthor
import play.api.db.slick.{HasDatabaseConfigProvider, DatabaseConfigProvider}
import slick.driver.JdbcProfile
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class BooksRepository@Inject()() {
val sampleBook = (Book(1, "Programming in Scala", 1), Author(1, "Martin Odersky"))
class BooksRepository@Inject()(
bookDao: BookDao,
authorDao: AuthorDao,
protected val dbConfigProvider: DatabaseConfigProvider
) extends HasDatabaseConfigProvider[JdbcProfile] {
import driver.api._
def findAll(implicit executionContext: ExecutionContext):Future[List[Book]] = db.run(bookDao.findAll().result).map(_.toList)
def findById(id: Long)(implicit executionContext: ExecutionContext):Future[Book] = db.run{
(for {
b <- bookDao.books.filter(_.id === id)
a <- authorDao.authors.filter(_.id === b.authorId)
} yield (b, a)).result.head.transactionally
}.map(_.withAuthor)
def findAll(implicit executionContext: ExecutionContext):Future[List[Book]] = {
val books = List(sampleBook).map(_.withAuthor)
Future.successful(books)
}
def findById(long: Long)(implicit executionContext: ExecutionContext):Future[Book] = {
Future.successful(sampleBook.withAuthor)
}
def create(title:String, authorName: String): Future[Book] = {
val author = Author(2, authorName)
val book = Book(2, title, author.id)
View
@@ -3,8 +3,18 @@ val commonSettings = Seq(
scalaVersion := "2.11.6"
)
val slickVersion = "3.1.0"
val playSlickVersion = "1.1.0"
val appDeps = Seq(
"org.skinny-framework" %% "skinny-json" % "1.3.20"
"org.skinny-framework" %% "skinny-json" % "1.3.20",
"com.typesafe.play" %% "play-slick" % playSlickVersion,
"com.typesafe.play" %% "play-slick-evolutions" % playSlickVersion,
"mysql" % "mysql-connector-java" % "5.1.36",
"com.typesafe.slick" %% "slick" % slickVersion,
"com.typesafe.slick" %% "slick-codegen" % slickVersion,
"com.github.tototoshi" %% "slick-joda-mapper" % "2.1.0"
)
val stressTestDeps = Seq (
View
@@ -14,31 +14,22 @@ play.crypto.secret = "changeme"
# ~~~~~
play.i18n.langs = [ "en" ]
# Router
# ~~~~~
# Define the Router object to use for this application.
# This router will be looked up first when the application is starting up,
# so make sure this is the entry point.
# Furthermore, it's assumed your route file is named properly.
# So for an application router like `my.application.Router`,
# you may need to define a router file `conf/my.application.routes`.
# Default to Routes in the root package (and conf/routes)
# play.http.router = my.application.Routes
# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
# db.default.driver=org.h2.Driver
# db.default.url="jdbc:h2:mem:play"
# db.default.username=sa
# db.default.password=""
# Evolutions
# ~~~~~
# You can disable evolutions if needed
# play.evolutions.enabled=false
play.evolutions.enabled=true
play.evolutions.autoApply=true
evolutions.db.default.autoApply=true
slick.dbs.default {
driver="slick.driver.MySQLDriver$"
db {
driver=com.mysql.jdbc.Driver
url="jdbc:mysql://localhost:3306/bookshelf"
user=root
password=password
numThreads = 10
connectionTimeout = 5000
validationTimeout = 5000
}
}
# You can disable evolutions for a specific datasource if necessary
# play.evolutions.db.default.enabled=false
@@ -0,0 +1,20 @@
# --- !Ups
CREATE TABLE IF NOT EXISTS author (
id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(200) NOT NULL
);
CREATE TABLE IF NOT EXISTS book (
id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
author_id BIGINT(20) NOT NULL
);
INSERT INTO author VALUES (1, "Martin Odersky");
INSERT INTO book VALUES (1, "Programming in Scala", 1);
# --- !Downs
DROP TABLE book;
DROP TABLE author;

0 comments on commit 0c026f4

Please sign in to comment.