2015-09-21 107 views
1

如果該行存在,我想插入一個具有不同狀態值的新行,並將新實例作爲Some返回給調用者。如果它不存在,則不會發生並返回None。我有什麼是slick 3.0選擇然後插入

def revoke(name: String, issueFor: String): Future[MyLicense] = { 
    val retrieveLicense = slickLicenses.filter(l => l.name === name && l.issueFor === issueFor). 
    sortBy(_.modifiedOn.desc).result.headOption 
    val insertLicense = for { 
    licenseOption <- retrieveLicense 
    } yield { 
    licenseOption match { 
     case Some(l) => Some(slickLicenses += l.copy(status = RevokedLicense)) 
     case None => None 
    } 
} 
db.run(insertLicense).map {r => 
    r.map { dbLicense => MyLicense(dbLicense.name, dbLicense.status) 
} 

}

我該如何解決這個問題?

編輯1

我改變了代碼

override def revoke(name: String, issueFor: String): Future[String] = { 
    val retrieveLicense = slickLicenses.filter(l => l.name === name && l.issueFor === issueFor). 
    sortBy(_.modifiedOn.desc).result.headOption 
    val insertLicenseAction = for { 
    l <- retrieveLicense 
    newLicense <- slickLicenses += l.get.copy(status = RevokedLicense) 
} yield newLicense 
    db.run(insertLicenseAction).map(_ => name) 

}

因此,問題變爲如何找我插入到數據庫實例?由於

回答

1

這是我做的最後,

override def revoke(name: String, issueFor: String): Future[String] = { 
val retrieveLicense = slickLicenses.filter(l => l.name === name && l.issueFor === issueFor). 
    sortBy(_.modifiedOn.desc).result.headOption 
val insertLicenseAction = for { 
    l <- retrieveLicense 
    _ <- l.map(x => slickLicenses += x.copy(status = RevokedLicense, 
      modifiedOn = new Timestamp(System.currentTimeMillis()))) 
    .getOrElse(DBIO.successful(l)) 
} yield() 
db.run(insertLicenseAction).map(x => name)} 
相關問題