class ItemTable(tag: Tag) extends Table[Item](tag, _tableName = "items") {
def id: Rep[Int] = column[Int]("id", O.PrimaryKey, O.AutoInc)
def authId: Rep[String] = column[String]("auth_id")
def productId: Rep[Int] = column[Int]("product_id")
def position: Rep[Int] = column[Int]("position")
override def * : ProvenShape[Item] = (id, authId, productId, position) <> ((Item.apply _).tupled, Item.unapply)
}
class ItemService @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)(implicit executionContext: ExecutionContext) {
private lazy val query = TableQuery[ItemTable]
def updateItem(item: Item, previousPosition: Int): Future[Item] = {
if (previousPosition < item.position) {
// this is the sql I want to convert to slick
db.run(sqlu""" UPDATE #${query.baseTableRow.tableName} SET position = position - 1 WHERE position >= #$previousPosition AND position <= #${item.position} AND auth_id = '#${item.authId}'""")
}
else if (previousPosition > item.position) {
db.run(sqlu""" UPDATE #${query.baseTableRow.tableName} SET position = position + 1 WHERE position <= #$previousPosition AND position >= #${item.position} AND auth_id = '#${item.authId}' """)
}
else {
// some other code
}
}
}
case class Item(id: Int, authId: String, productId: Int, position: Int){}
У меня SQL работает, вот застрял переводить на слик.
val q2 = query.filter(f=> f.authId === item.authId && f.position >= previousPosition && f.position <= item.position)
// Я не знаю, как обновить несколько или передать результат в функцию обновления