Skip to content

Instantly share code, notes, and snippets.

@nuboat
Last active December 10, 2015 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nuboat/dd7c61c9a391dcba9599 to your computer and use it in GitHub Desktop.
Save nuboat/dd7c61c9a391dcba9599 to your computer and use it in GitHub Desktop.
Slick 3
// scalastyle:off
package services.slick
import javax.inject.Singleton
import define.JobLevel
import intercept.Logging
import models.{GroupJobLevel, Subscriber}
import org.postgresql.util.PSQLException
import play.api.Logger
import play.api.cache.Cache
import play.api.Play.current
import services.SubscriberService
import slick.driver.PostgresDriver.api._
import scala.collection.mutable
import scala.concurrent.{Future, Await}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.util.Success
/**
*
* Created by Pongthep
* Contribute by nuboat
*/
private class Subscribers(tag: Tag) extends Table[Subscriber](tag, "subscriber") {
def email: Rep[String] = column[String]("email", O.PrimaryKey)
def name: Rep[String] = column[String]("fullname")
def mobile: Rep[String] = column[String]("mobile")
def jobLevel: Rep[Int] = column[Int]("joblevel")
def * = (name, email, mobile, jobLevel) <>(Subscriber.tupled, Subscriber.unapply)
}
@Singleton
private class SlickSubscriberService extends SubscriberService {
val subscribers = TableQuery[Subscribers]
@Logging
def groupJobLevel(): List[GroupJobLevel] = {
val cache = Cache.get("groupJobLevel")
if (cache.isDefined) {
return cache.get.asInstanceOf[List[GroupJobLevel]]
}
val db = Database.forConfig("db")
try {
val query = sql"""
select joblevel, count(1) as count
from subscriber
group by joblevel
order by joblevel;""".as[(Int, Int)]
val buffer = mutable.ListBuffer[GroupJobLevel]()
val f = db.run(query).map(x => x.foreach( t => buffer.append(GroupJobLevel(JobLevel(t._1), t._2))))
Await.result(f, Duration.Inf)
val result = buffer.toList
Cache.set("groupJobLevel", result, 3600)
return result
} finally db.close
}
@Logging
def addSubscriber(subscriber: Subscriber): Option[Subscriber] = {
val db = Database.forConfig("db")
val isCompleted = {
val f = db.run((subscribers += subscriber).transactionally)
try {
Await.result(f, Duration.Inf)
true
} catch {
case _: Throwable => false
} finally db.close
}
return if (isCompleted) return Some(subscriber) else None
}
def addAsyncSubscriber(subscriber: Subscriber) = {
val db = Database.forConfig("db")
try {
db.run((subscribers += subscriber).transactionally)
} finally db.close
}
}
// scalastyle:off
package services.slick
import define.JobLevel
import models.Subscriber
import org.scalatest.{FlatSpec, Matchers}
import play.api.test.WithApplication
import services.TestJector
class SlickSubscriberServiceSpec extends FlatSpec with Matchers {
val id = System.currentTimeMillis()
"SlickSubscriberService.addSubscriber with new email" should
"result.isEmpty should be true" in new WithApplication {
val service = TestJector.instance[SlickSubscriberService]
val subscriber = Subscriber("nuboat", s"nb$id@thjug.com", "0000", JobLevel.CLevel.id)
val result = service.addSubscriber(subscriber)
result.isEmpty should be(false)
}
"SlickSubscriberService.addSubscriber with exist email" should
"result.isEmpty should be false" in new WithApplication {
val service = TestJector.instance[SlickSubscriberService]
val subscriber = Subscriber("nuboat", s"nb$id@thjug.com", "0000", JobLevel.CLevel.id)
val result = service.addSubscriber(subscriber)
result.isEmpty should be(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment