scala - slick 3.0 select then insert -
i want insert new row different status value if row exists , return new instance some
caller. if not exist, nothing happens , return none
. have
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 = { 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) }
}
how fix this?
edit 1
i changed code
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 = { l <- retrievelicense newlicense <- slicklicenses += l.get.copy(status = revokedlicense) } yield newlicense db.run(insertlicenseaction).map(_ => name)
}
hence, question changes how instance insert database? thanks
this did finally,
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 = { 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)}
Comments
Post a Comment