2016-07-20 9 views
0

Ich habe dieses Slick 2.1 basierte Repo-Methode:Update eine Multi-Anweisung Slick 2.1 withDynSession Block Slick 3.0.3

def addContentBySourceInfo(userId: UUID, adopted: Boolean, contentId: UUID, contentInfo: ContentWithoutId): Either[ContentAlreadyExistsError, Content] = { 

    getDatabase withDynSession { 
    val content = contentInfo.toContent(contentId) 

    Try { 
     ContentTable.query += content 
     UserContentTable.query += UserContentModel(userId, contentId, Some(adopted)) 
    } match { 
     case Failure(e:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException) => 
     Left(ContentAlreadyExistsError(content.source, content.sourceId)) 
     case Failure(e) => throw e // could be some other error, and we should fail fast. 
     case Success(s) => Right(content) 
    } 
    } 
} 

Wo getDatabase einfach Database.forURL(..) von slick.jdbc.JdbcBackend zurückgibt.

Wie würde ich dies konvertieren, um mit der DBIO API von Slick 3.x kompatibel zu sein?

Hinweis: Ich möchte diese Methoden Synchron halten, bis ich bin bereit, meine ganze Repository-Schicht zu aktualisieren Asynchronous Anrufe bearbeiten (dh ich will nicht nur noch mein Repository API brechen)

Antwort

1

Sie sollten so etwas wie dies versuchen:

def addContentBySourceInfo(userId: UUID, adopted: Boolean, contentId: UUID, contentInfo: ContentWithoutId): Either[ContentAlreadyExistsError, Content] = { 
    val content = contentInfo.toContent(contentId) 
    val actions = DBIO.seq(
    ContentTable.query  += content, 
    UserContentTable.query += UserContentModel(userId, contentId, Some(adopted)) 
) 

    Try(Await.result(db.run(actions), Duration.Inf)) match { 
    case Failure(e:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException) => 
     Left(ContentAlreadyExistsError(content.source, content.sourceId)) 
    case Failure(e) => throw e 
    case Success(s) => Right(content) 
    } 
} 
+0

Danke, bin gerade etwas ähnliches zu berichten, nachdem ich: https://youtu.be/WvxXz7aklik?t=30m16s – ThaDon