Skip to content
This repository was archived by the owner on Oct 1, 2019. It is now read-only.

Update this project to be up-to-date #17

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions app/controllers/Companies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import play.api.mvc._
@Singleton
class Companies @Inject() (json4s: Json4s) extends Controller {

import json4s._
import json4s.implicits._
implicit val formats = DefaultFormats ++ JodaTimeSerializers.all

def all = Action {
Expand All @@ -32,9 +32,7 @@ class Companies @Inject() (json4s: Json4s) extends Controller {
private val companyForm = Form(
mapping(
"name" -> text.verifying(nonEmpty),
"url" -> optional(text)
)(CompanyForm.apply)(CompanyForm.unapply)
)
"url" -> optional(text))(CompanyForm.apply)(CompanyForm.unapply))

def create = Action { implicit req =>
companyForm.bindFromRequest.fold(
Expand All @@ -43,8 +41,7 @@ class Companies @Inject() (json4s: Json4s) extends Controller {
val company = Company.create(name = form.name, url = form.url)
Created.withHeaders(LOCATION -> s"/companies/${company.id}")
NoContent
}
)
})
}

def delete(id: Long) = Action {
Expand Down
9 changes: 3 additions & 6 deletions app/controllers/Programmers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import play.api.mvc._
@Singleton
class Programmers @Inject() (json4s: Json4s) extends Controller {

import json4s._
import json4s.implicits._
implicit val formats = DefaultFormats ++ JodaTimeSerializers.all

def all = Action {
Expand All @@ -32,9 +32,7 @@ class Programmers @Inject() (json4s: Json4s) extends Controller {
private val programmerForm = Form(
mapping(
"name" -> text.verifying(nonEmpty),
"companyId" -> optional(longNumber)
)(ProgrammerForm.apply)(ProgrammerForm.unapply)
)
"companyId" -> optional(longNumber))(ProgrammerForm.apply)(ProgrammerForm.unapply))

def create = Action { implicit req =>
programmerForm.bindFromRequest.fold(
Expand All @@ -43,8 +41,7 @@ class Programmers @Inject() (json4s: Json4s) extends Controller {
val programmer = Programmer.create(name = form.name, companyId = form.companyId)
Created.withHeaders(LOCATION -> s"/programmers/${programmer.id}")
NoContent
}
)
})
}

def addSkill(programmerId: Long, skillId: Long) = Action {
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/Skills.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import play.api.mvc._
@Singleton
class Skills @Inject() (json4s: Json4s) extends Controller {

import json4s._
import json4s.implicits._
implicit val formats = DefaultFormats ++ JodaTimeSerializers.all

def all = Action {
Expand All @@ -30,8 +30,7 @@ class Skills @Inject() (json4s: Json4s) extends Controller {
case class SkillForm(name: String)

private val skillForm = Form(
mapping("name" -> text.verifying(nonEmpty))(SkillForm.apply)(SkillForm.unapply)
)
mapping("name" -> text.verifying(nonEmpty))(SkillForm.apply)(SkillForm.unapply))

def create = Action { implicit req =>
skillForm.bindFromRequest.fold(
Expand All @@ -40,8 +39,7 @@ class Skills @Inject() (json4s: Json4s) extends Controller {
val skill = Skill.create(name = form.name)
Created.withHeaders(LOCATION -> s"/skills/${skill.id}")
NoContent
}
)
})
}

def delete(id: Long) = Action {
Expand Down
27 changes: 12 additions & 15 deletions app/models/Company.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package models

import java.time.ZonedDateTime

import scalikejdbc._
import org.joda.time.DateTime

case class Company(
id: Long,
name: String,
url: Option[String] = None,
createdAt: DateTime,
deletedAt: Option[DateTime] = None
) {
id: Long,
name: String,
url: Option[String] = None,
createdAt: ZonedDateTime,
deletedAt: Option[ZonedDateTime] = None) {

def save()(implicit session: DBSession = Company.autoSession): Company = Company.save(this)(session)
def destroy()(implicit session: DBSession = Company.autoSession): Unit = Company.destroy(id)(session)
Expand All @@ -23,8 +23,7 @@ object Company extends SQLSyntaxSupport[Company] {
name = rs.get(c.name),
url = rs.get(c.url),
createdAt = rs.get(c.createdAt),
deletedAt = rs.get(c.deletedAt)
)
deletedAt = rs.get(c.deletedAt))

val c = Company.syntax("c")
private val isNotDeleted = sqls.isNull(c.deletedAt)
Expand Down Expand Up @@ -53,13 +52,12 @@ object Company extends SQLSyntaxSupport[Company] {
select(sqls.count).from(Company as c).where.append(isNotDeleted).and.append(sqls"${where}")
}.map(_.long(1)).single.apply().get

def create(name: String, url: Option[String] = None, createdAt: DateTime = DateTime.now)(implicit session: DBSession = autoSession): Company = {
def create(name: String, url: Option[String] = None, createdAt: ZonedDateTime = ZonedDateTime.now)(implicit session: DBSession = autoSession): Company = {
val id = withSQL {
insert.into(Company).namedValues(
column.name -> name,
column.url -> url,
column.createdAt -> createdAt
)
column.createdAt -> createdAt)
}.updateAndReturnGeneratedKey.apply()

Company(id = id, name = name, url = url, createdAt = createdAt)
Expand All @@ -69,14 +67,13 @@ object Company extends SQLSyntaxSupport[Company] {
withSQL {
update(Company).set(
column.name -> m.name,
column.url -> m.url
).where.eq(column.id, m.id).and.isNull(column.deletedAt)
column.url -> m.url).where.eq(column.id, m.id).and.isNull(column.deletedAt)
}.update.apply()
m
}

def destroy(id: Long)(implicit session: DBSession = autoSession): Unit = withSQL {
update(Company).set(column.deletedAt -> DateTime.now).where.eq(column.id, id)
update(Company).set(column.deletedAt -> ZonedDateTime.now).where.eq(column.id, id)
}.update.apply()

}
34 changes: 15 additions & 19 deletions app/models/Programmer.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package models

import java.time.ZonedDateTime

import scalikejdbc._
import org.joda.time.DateTime

case class Programmer(
id: Long,
name: String,
companyId: Option[Long] = None,
company: Option[Company] = None,
skills: Seq[Skill] = Nil,
createdAt: DateTime,
deletedAt: Option[DateTime] = None
) {
id: Long,
name: String,
companyId: Option[Long] = None,
company: Option[Company] = None,
skills: Seq[Skill] = Nil,
createdAt: ZonedDateTime,
deletedAt: Option[ZonedDateTime] = None) {

def save()(implicit session: DBSession = Programmer.autoSession): Programmer = Programmer.save(this)(session)
def destroy()(implicit session: DBSession = Programmer.autoSession): Unit = Programmer.destroy(id)(session)
Expand Down Expand Up @@ -47,8 +47,7 @@ object Programmer extends SQLSyntaxSupport[Programmer] {
name = rs.get(p.name),
companyId = rs.get(p.companyId),
createdAt = rs.get(p.createdAt),
deletedAt = rs.get(p.deletedAt)
)
deletedAt = rs.get(p.deletedAt))

// join query with company table
def apply(p: SyntaxProvider[Programmer], c: SyntaxProvider[Company])(rs: WrappedResultSet): Programmer = {
Expand Down Expand Up @@ -121,39 +120,36 @@ object Programmer extends SQLSyntaxSupport[Programmer] {
select(sqls.count).from(Programmer as p).where.append(isNotDeleted).and.append(sqls"${where}")
}.map(_.long(1)).single.apply().get

def create(name: String, companyId: Option[Long] = None, createdAt: DateTime = DateTime.now)(implicit session: DBSession = autoSession): Programmer = {
def create(name: String, companyId: Option[Long] = None, createdAt: ZonedDateTime = ZonedDateTime.now)(implicit session: DBSession = autoSession): Programmer = {
if (companyId.isDefined && Company.find(companyId.get).isEmpty) {
throw new IllegalArgumentException(s"Company is not found! (companyId: ${companyId})")
}
val id = withSQL {
insert.into(Programmer).namedValues(
column.name -> name,
column.companyId -> companyId,
column.createdAt -> createdAt
)
column.createdAt -> createdAt)
}.updateAndReturnGeneratedKey.apply()

Programmer(
id = id,
name = name,
companyId = companyId,
company = companyId.flatMap(id => Company.find(id)),
createdAt = createdAt
)
createdAt = createdAt)
}

def save(m: Programmer)(implicit session: DBSession = autoSession): Programmer = {
withSQL {
update(Programmer).set(
column.name -> m.name,
column.companyId -> m.companyId
).where.eq(column.id, m.id).and.isNull(column.deletedAt)
column.companyId -> m.companyId).where.eq(column.id, m.id).and.isNull(column.deletedAt)
}.update.apply()
m
}

def destroy(id: Long)(implicit session: DBSession = autoSession): Unit = withSQL {
update(Programmer).set(column.deletedAt -> DateTime.now).where.eq(column.id, id)
update(Programmer).set(column.deletedAt -> ZonedDateTime.now).where.eq(column.id, id)
}.update.apply()

}
19 changes: 9 additions & 10 deletions app/models/Skill.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package models

import java.time.ZonedDateTime

import scalikejdbc._
import org.joda.time.DateTime

case class Skill(
id: Long,
name: String,
createdAt: DateTime,
deletedAt: Option[DateTime] = None
) {
id: Long,
name: String,
createdAt: ZonedDateTime,
deletedAt: Option[ZonedDateTime] = None) {

def save()(implicit session: DBSession = Skill.autoSession): Skill = Skill.save(this)(session)
def destroy()(implicit session: DBSession = Skill.autoSession): Unit = Skill.destroy(id)(session)
Expand All @@ -21,8 +21,7 @@ object Skill extends SQLSyntaxSupport[Skill] {
id = rs.get(s.id),
name = rs.get(s.name),
createdAt = rs.get(s.createdAt),
deletedAt = rs.get(s.deletedAt)
)
deletedAt = rs.get(s.deletedAt))

def opt(s: SyntaxProvider[Skill])(rs: WrappedResultSet): Option[Skill] = rs.longOpt(s.resultName.id).map(_ => apply(s.resultName)(rs))

Expand Down Expand Up @@ -54,7 +53,7 @@ object Skill extends SQLSyntaxSupport[Skill] {
select(sqls.count).from(Skill as s).where.append(isNotDeleted).and.append(sqls"${where}")
}.map(_.long(1)).single.apply().get

def create(name: String, createdAt: DateTime = DateTime.now)(implicit session: DBSession = autoSession): Skill = {
def create(name: String, createdAt: ZonedDateTime = ZonedDateTime.now)(implicit session: DBSession = autoSession): Skill = {
val id = withSQL {
insert.into(Skill).namedValues(column.name -> name, column.createdAt -> createdAt)
}.updateAndReturnGeneratedKey.apply()
Expand All @@ -70,7 +69,7 @@ object Skill extends SQLSyntaxSupport[Skill] {
}

def destroy(id: Long)(implicit session: DBSession = autoSession): Unit = withSQL {
update(Skill).set(column.deletedAt -> DateTime.now).where.eq(column.id, id)
update(Skill).set(column.deletedAt -> ZonedDateTime.now).where.eq(column.id, id)
}.update.apply()

}
18 changes: 10 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
lazy val root = (project in file("."))
.enablePlugins(PlayScala)
.enablePlugins(SbtWeb)
.enablePlugins(ScalikejdbcPlugin)
.enablePlugins(SbtScalariform)
.settings(
name := "hello-scalikejdbc",
version := "0.1",
scalaVersion := "2.11.8",
scalaVersion := "2.12.6",
resolvers ++= Seq(
"sonatype releases" at "http://oss.sonatype.org/content/repositories/releases",
"scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
Expand All @@ -18,8 +20,9 @@ lazy val root = (project in file("."))
"org.scalikejdbc" %% "scalikejdbc-play-fixture" % scalikejdbcPlayVersion,
"com.h2database" % "h2" % h2Version,
"org.json4s" %% "json4s-ext" % "3.4.+",
"com.github.tototoshi" %% "play-json4s-native" % "0.5.+",
"org.flywaydb" %% "flyway-play" % "3.0.+",
"com.github.tototoshi" %% "play-json4s-native" % "0.8.+",
"org.flywaydb" %% "flyway-play" % "4.0.+",
guice,
"org.scalikejdbc" %% "scalikejdbc-test" % scalikejdbcVersion % "test",
specs2 % "test"
),
Expand All @@ -31,10 +34,9 @@ lazy val root = (project in file("."))
implicit val autoSession = AutoSession
val (p, c, s, ps) = (Programmer.syntax("p"), Company.syntax("c"), Skill.syntax("s"), ProgrammerSkill.syntax("ps"))
""",
routesGenerator := InjectedRoutesGenerator,
scalikejdbcSettings // http://scalikejdbc.org/documentation/setup.html
).settings(scalariformSettings)
routesGenerator := InjectedRoutesGenerator
)

lazy val scalikejdbcVersion = scalikejdbc.ScalikejdbcBuildInfo.version
lazy val scalikejdbcPlayVersion = "2.5.+"
lazy val h2Version = "1.4.+"
lazy val scalikejdbcPlayVersion = "2.6.0-scalikejdbc-3.2"
lazy val h2Version = "1.4.+"
3 changes: 3 additions & 0 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ play.modules.enabled += "org.flywaydb.play.PlayModule"
play.modules.enabled += "scalikejdbc.PlayModule"
play.modules.enabled += "scalikejdbc.PlayFixtureModule"
play.modules.enabled += "modules.GlobalModule"

play.filters.disabled += "play.filters.csrf.CSRFFilter"
play.filters.disabled += "play.filters.headers.SecurityHeadersFilter"
2 changes: 1 addition & 1 deletion conf/logback.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<configuration>

<conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.12
sbt.version=1.1.1
10 changes: 5 additions & 5 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ resolvers ++= Seq(
)
libraryDependencies += "com.h2database" % "h2" % "1.4.191"

addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.6.0")
addSbtPlugin("org.scalikejdbc" % "scalikejdbc-mapper-generator" % "2.5.2")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.4")
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.10")
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2")
addSbtPlugin("org.scalikejdbc" % "scalikejdbc-mapper-generator" % "3.2.2")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.16")
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.2")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.4")
1 change: 0 additions & 1 deletion test/models/CompanySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package models

import scalikejdbc.specs2.mutable.AutoRollback
import org.specs2.mutable._
import org.joda.time._
import scalikejdbc._

class CompanySpec extends Specification with settings.DBSettings {
Expand Down
1 change: 0 additions & 1 deletion test/models/ProgrammerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package models

import scalikejdbc.specs2.mutable.AutoRollback
import org.specs2.mutable._
import org.joda.time._
import scalikejdbc._

class ProgrammerSpec extends Specification with settings.DBSettings {
Expand Down
1 change: 0 additions & 1 deletion test/models/SkillSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package models

import scalikejdbc.specs2.mutable.AutoRollback
import org.specs2.mutable._
import org.joda.time._
import scalikejdbc._

class SkillSpec extends Specification with settings.DBSettings {
Expand Down
2 changes: 1 addition & 1 deletion test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<configuration>

<conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
Expand Down