Skip to content

Commit

Permalink
Merge branch 'release/0.3.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Jan 8, 2014
2 parents e77cbc6 + 9e341cb commit b780cbe
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 135 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Add `scalikejdbc-async` to your dependencies.
```scala
libraryDependencies ++= Seq(
"org.scalikejdbc" %% "scalikejdbc-async" % "[0.3,)",
"com.github.mauricio" %% "postgresql-async" % "0.2.8",
"com.github.mauricio" %% "mysql-async" % "0.2.8",
"com.github.mauricio" %% "postgresql-async" % "0.2.10",
"com.github.mauricio" %% "mysql-async" % "0.2.10",
"org.slf4j" % "slf4j-simple" % "[1.7,)" // slf4j implementation
)
```
Expand All @@ -43,7 +43,7 @@ If you're a Play2 user, use play-plugin too!
val appDependencies = Seq(
"org.scalikejdbc" %% "scalikejdbc-async" % "[0.3,)",
"org.scalikejdbc" %% "scalikejdbc-async-play-plugin" % "[0.3,)",
"com.github.mauricio" %% "postgresql-async" % "0.2.8"
"com.github.mauricio" %% "postgresql-async" % "0.2.10"
)
```

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/scalikejdbc/async/AsyncDBSession.scala
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ case class TxAsyncDBSession(connection: NonSharedAsyncConnection) extends AsyncD

def isActive: Boolean = connection.isActive

def begin(): Future[AsyncQueryResult] = connection.sendQuery("BEGIN")
def begin()(implicit ctx: EC = ECGlobal): Future[AsyncQueryResult] = connection.sendQuery("BEGIN")

def rollback(): Future[AsyncQueryResult] = connection.sendQuery("ROLLBACK")
def rollback()(implicit ctx: EC = ECGlobal): Future[AsyncQueryResult] = connection.sendQuery("ROLLBACK")

def commit(): Future[AsyncQueryResult] = connection.sendQuery("COMMIT")
def commit()(implicit ctx: EC = ECGlobal): Future[AsyncQueryResult] = connection.sendQuery("COMMIT")

def release(): Unit = connection.release()

Expand Down
41 changes: 16 additions & 25 deletions play-sample/app/controllers/Companies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import play.api.data._, Forms._, validation.Constraints._
import org.json4s._, ext.JodaTimeSerializers
import com.github.tototoshi.play2.json4s.native._

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

import models._
Expand All @@ -14,17 +15,13 @@ object Companies extends Controller with Json4s {

implicit val formats = DefaultFormats ++ JodaTimeSerializers.all

def all = Action {
Async {
Company.findAll.map(companies => Ok(Extraction.decompose(companies)))
}
def all = Action.async {
Company.findAll.map(companies => Ok(Extraction.decompose(companies)))
}

def show(id: Long) = Action {
Async {
Company.find(id) map { companyOpt =>
companyOpt map { company => Ok(Extraction.decompose(company)) } getOrElse NotFound
}
def show(id: Long) = Action.async {
Company.find(id) map { companyOpt =>
companyOpt map { company => Ok(Extraction.decompose(company)) } getOrElse NotFound
}
}

Expand All @@ -37,27 +34,21 @@ object Companies extends Controller with Json4s {
)(CompanyForm.apply)(CompanyForm.unapply)
)

def create = Action { implicit req =>
def create = Action.async { implicit req =>
companyForm.bindFromRequest.fold(
formWithErrors => BadRequest("invalid parameters"),
form => {
Async {
Company.create(name = form.name, url = form.url).map { company =>
Created.withHeaders(LOCATION -> s"/companies/${company.id}")
}
}
formWithErrors => Future.successful(BadRequest("invalid parameters")),
form => Company.create(name = form.name, url = form.url).map { company =>
Created.withHeaders(LOCATION -> s"/companies/${company.id}")
}
)
}

def delete(id: Long) = Action {
Async {
Company.find(id).map { companyOpt =>
companyOpt map { company =>
company.destroy()
NoContent
} getOrElse NotFound
}
def delete(id: Long) = Action.async {
Company.find(id).map { companyOpt =>
companyOpt map { company =>
company.destroy()
NoContent
} getOrElse NotFound
}
}

Expand Down
114 changes: 49 additions & 65 deletions play-sample/app/controllers/Programmers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import play.api.data._, Forms._, validation.Constraints._

import org.json4s._, ext.JodaTimeSerializers
import com.github.tototoshi.play2.json4s.native._

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

import models._
Expand All @@ -13,17 +15,13 @@ object Programmers extends Controller with Json4s {

implicit val formats = DefaultFormats ++ JodaTimeSerializers.all

def all = Action {
Async {
Programmer.findAll.map(programmers => Ok(Extraction.decompose(programmers)))
}
def all = Action.async {
Programmer.findAll.map(programmers => Ok(Extraction.decompose(programmers)))
}

def show(id: Long) = Action {
Async {
Programmer.find(id).map { programmerOpt =>
programmerOpt map { programmer => Ok(Extraction.decompose(programmer)) } getOrElse NotFound
}
def show(id: Long) = Action.async {
Programmer.find(id).map { programmerOpt =>
programmerOpt map { programmer => Ok(Extraction.decompose(programmer)) } getOrElse NotFound
}
}

Expand All @@ -36,82 +34,68 @@ object Programmers extends Controller with Json4s {
)(ProgrammerForm.apply)(ProgrammerForm.unapply)
)

def create = Action { implicit req =>
def create = Action.async { implicit req =>
programmerForm.bindFromRequest.fold(
formWithErrors => BadRequest("invalid parameters"),
form => {
Async {
Programmer.create(name = form.name, companyId = form.companyId).map { programmer =>
Created.withHeaders(LOCATION -> s"/programmers/${programmer.id}")
}
}
formWithErrors => Future.successful(BadRequest("invalid parameters")),
form => Programmer.create(name = form.name, companyId = form.companyId).map { programmer =>
Created.withHeaders(LOCATION -> s"/programmers/${programmer.id}")
}
)
}

def addSkill(programmerId: Long, skillId: Long) = Action {
Async {
Programmer.find(programmerId).map { programmerOpt =>
programmerOpt map { programmer =>
try {
Skill.find(skillId).map { skillOpt =>
skillOpt map { skill => programmer.addSkill(skill) }
}
Ok
} catch { case e: Exception => Conflict }
} getOrElse NotFound
}
}
}

def deleteSkill(programmerId: Long, skillId: Long) = Action {
Async {
Programmer.find(programmerId).map { programmerOpt =>
programmerOpt map { programmer =>
def addSkill(programmerId: Long, skillId: Long) = Action.async {
Programmer.find(programmerId).map { programmerOpt =>
programmerOpt map { programmer =>
try {
Skill.find(skillId).map { skillOpt =>
skillOpt map { skill => programmer.addSkill(skill) }
}
Ok
} getOrElse NotFound
}
} catch { case e: Exception => Conflict }
} getOrElse NotFound
}
}

def joinCompany(programmerId: Long, companyId: Long) = Action {
Async {
for {
companyOpt <- Company.find(companyId)
programmerOpt <- Programmer.find(programmerId)
} yield {
companyOpt map { company =>
programmerOpt.map { programmer =>
programmer.copy(companyId = Some(company.id)).save()
Ok
} getOrElse BadRequest("Programmer not found!")
} getOrElse BadRequest("Company not found!")
}
def deleteSkill(programmerId: Long, skillId: Long) = Action.async {
Programmer.find(programmerId).map { programmerOpt =>
programmerOpt map { programmer =>
Skill.find(skillId).map { skillOpt =>
skillOpt map { skill => programmer.addSkill(skill) }
}
Ok
} getOrElse NotFound
}
}

def leaveCompany(programmerId: Long) = Action {
Async {
Programmer.find(programmerId).map { programmerOpt =>
programmerOpt map { programmer =>
programmer.copy(companyId = None).save()
def joinCompany(programmerId: Long, companyId: Long) = Action.async {
for {
companyOpt <- Company.find(companyId)
programmerOpt <- Programmer.find(programmerId)
} yield {
companyOpt map { company =>
programmerOpt.map { programmer =>
programmer.copy(companyId = Some(company.id)).save()
Ok
} getOrElse BadRequest("Programmer not found!")
}
} getOrElse BadRequest("Company not found!")
}
}

def delete(id: Long) = Action {
Async {
Programmer.find(id).map { programmerOpt =>
programmerOpt map { programmer =>
programmer.destroy()
NoContent
} getOrElse NotFound
}
def leaveCompany(programmerId: Long) = Action.async {
Programmer.find(programmerId).map { programmerOpt =>
programmerOpt map { programmer =>
programmer.copy(companyId = None).save()
Ok
} getOrElse BadRequest("Programmer not found!")
}
}

def delete(id: Long) = Action.async {
Programmer.find(id).map { programmerOpt =>
programmerOpt map { programmer =>
programmer.destroy()
NoContent
} getOrElse NotFound
}
}

Expand Down
46 changes: 19 additions & 27 deletions play-sample/app/controllers/Skills.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import play.api.data._, Forms._, validation.Constraints._

import org.json4s._, ext.JodaTimeSerializers
import com.github.tototoshi.play2.json4s.native._

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

import models._
Expand All @@ -13,19 +15,15 @@ object Skills extends Controller with Json4s {

implicit val formats = DefaultFormats ++ JodaTimeSerializers.all

def all = Action {
Async {
Skill.findAll.map(skills => Ok(Extraction.decompose(skills)))
}
def all = Action.async {
Skill.findAll.map(skills => Ok(Extraction.decompose(skills)))
}

def show(id: Long) = Action {
Async {
Skill.find(id).map { skillOpt =>
skillOpt map { skill =>
Ok(Extraction.decompose(skill))
} getOrElse NotFound
}
def show(id: Long) = Action.async {
Skill.find(id).map { skillOpt =>
skillOpt map { skill =>
Ok(Extraction.decompose(skill))
} getOrElse NotFound
}
}

Expand All @@ -35,27 +33,21 @@ object Skills extends Controller with Json4s {
mapping("name" -> text.verifying(nonEmpty))(SkillForm.apply)(SkillForm.unapply)
)

def create = Action { implicit req =>
def create = Action.async { implicit req =>
skillForm.bindFromRequest.fold(
formWithErrors => BadRequest("invalid parameters"),
form => {
Async {
Skill.create(name = form.name).map { skill =>
Created.withHeaders(LOCATION -> s"/skills/${skill.id}")
}
}
formWithErrors => Future.successful(BadRequest("invalid parameters")),
form => Skill.create(name = form.name).map { skill =>
Created.withHeaders(LOCATION -> s"/skills/${skill.id}")
}
)
}

def delete(id: Long) = Action {
Async {
Skill.find(id).map { skillOpt =>
skillOpt map { skill =>
skill.destroy()
NoContent
} getOrElse NotFound
}
def delete(id: Long) = Action.async {
Skill.find(id).map { skillOpt =>
skillOpt map { skill =>
skill.destroy()
NoContent
} getOrElse NotFound
}
}

Expand Down
56 changes: 56 additions & 0 deletions play-sample/conf/db/migration/default/V1__create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
drop table if exists programmer;
create sequence programmer_id_seq start with 1;
create table programmer (
id bigint not null default nextval('programmer_id_seq') primary key,
name varchar(255) not null,
company_id bigint,
created_timestamp timestamp not null,
deleted_timestamp timestamp
);

drop table if exists company;
create sequence company_id_seq start with 1;
create table company (
id bigint not null default nextval('company_id_seq') primary key,
name varchar(255) not null,
url varchar(255),
created_at timestamp not null,
deleted_at timestamp
);

drop table if exists skill;
create sequence skill_id_seq start with 1;
create table skill (
id bigint not null default nextval('skill_id_seq') primary key,
name varchar(255) not null,
created_at timestamp not null,
deleted_at timestamp
);

drop table if exists programmer_skill;
create table programmer_skill (
programmer_id bigint not null,
skill_id bigint not null,
primary key(programmer_id, skill_id)
);

insert into company (name, url, created_at) values ('Typesafe', 'http://typesafe.com/', current_timestamp);
insert into company (name, url, created_at) values ('Oracle', 'http://www.oracle.com/', current_timestamp);
insert into company (name, url, created_at) values ('Google', 'http://www.google.com/', current_timestamp);
insert into company (name, url, created_at) values ('Microsoft', 'http://www.microsoft.com/', current_timestamp);

insert into skill (name, created_at) values ('Scala', current_timestamp);
insert into skill (name, created_at) values ('Java', current_timestamp);
insert into skill (name, created_at) values ('Ruby', current_timestamp);
insert into skill (name, created_at) values ('MySQL', current_timestamp);
insert into skill (name, created_at) values ('PostgreSQL', current_timestamp);


insert into programmer (name, company_id, created_timestamp) values ('Alice', 1, current_timestamp);
insert into programmer (name, company_id, created_timestamp) values ('Bob', 2, current_timestamp);
insert into programmer (name, company_id, created_timestamp) values ('Chris', 1, current_timestamp);

insert into programmer_skill values (1, 1);
insert into programmer_skill values (1, 2);
insert into programmer_skill values (2, 2);

3 changes: 2 additions & 1 deletion play-sample/conf/play.plugins
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
888:scalikejdbc.async.PlayPlugin
666:com.github.tototoshi.play2.flyway.Plugin
777:scalikejdbc.async.PlayPlugin

Loading

0 comments on commit b780cbe

Please # to comment.