将DBIO移植到Future [Either [Error,User]]

时间:2018-10-30 20:22:25

标签: scala sbt slick slick-3.0

我是Slick的新手,下面有问题。 我正在尝试实现此处定义的方法.withCustomOverides

createUser

问题是,如果用户已经存在,我想给出一个错误,因此方法的签名应该是

def createUser(data: UserCreate): Future[User] = {
   val userRow = User(userId = UUID.randomUUID(),
     email = data.email, password = data.password,
     firstName = data.firstName, lastName = data.lastName, balance = 0)
   val userAction =
     Users
       .filter(_.email === data.email).result.headOption.flatMap {
       case Some(res) =>
         //          mylog("product was there: " + product)
           DBIO.successful(res)
       case None =>
         //          mylog("inserting product")
         (Users returning Users.map(_.id) into (
           (user, newId) => user.copy(id = Some(newId)))
           ) += userRow
     }
       .transactionally
   db.run(userAction)
}

您对我该怎么办有任何想法?没有提高Throwable?

1 个答案:

答案 0 :(得分:0)

恕我直言,这是转换var diff = require('diff-compare'); var register = function(Handlebars) { var helpers = { // put all of your helpers inside this object toLowerCase : function(value){ return (value && typeof value === 'string') ? value.toLowerCase() : ''; }, compareTxt : function(baseText, newText){ return diff.build({baseText: baseText, newText: newText}); } }; if (Handlebars && typeof Handlebars.registerHelper === "function") { // register helpers for (var prop in helpers) { Handlebars.registerHelper(prop, helpers[prop]); } } else { // just return helpers object if we can't register helpers here return helpers; } }; module.exports.register = register; module.exports.helpers = register(null); 的一种情况,而不是有关滑动的问题。

关于以下方面的事情如何?

Future

代替我拥有的所有功能,可以在表示“用户已经存在”的特定数据库异常上进行匹配

注意: 在我的示例中,scala> :paste // Entering paste mode (ctrl-D to finish) import scala.concurrent.Future import scala.util.{Failure, Success} import scala.concurrent.ExecutionContext.Implicits.global case object RecordAlreadyExists extends RuntimeException case class User(id: Int) Future[User](throw new RuntimeException). transformWith { case Success(user) => Future.successful(Right[RecordAlreadyExists.type, User](user)) case Failure(_) => Future.successful(Left[RecordAlreadyExists.type, User](RecordAlreadyExists)) } // Exiting paste mode, now interpreting. import scala.concurrent.Future import scala.util.{Failure, Success} import scala.concurrent.ExecutionContext.Implicits.global defined object RecordAlreadyExists defined class User res3: scala.concurrent.Future[scala.util.Either[RecordAlreadyExists.type,User]] = Future(<not completed>) 代表问题中指定的完整功能。