Scala play slick postgresql

37 Views Asked by At

I am trying to get the hang of slick and have a simple client to save and load users

    
    import play.api.db.slick.DatabaseConfigProvider
    import com.github.tminglei.slickpg._
    
    import javax.inject.Inject
    import scala.concurrent.{ExecutionContext, Future}
    
    trait DatabaseClient {
      //Users
      def addOrUpdateUser(userRow: UserRow): Future[Option[UserRow]]
      def listUsers(): Future[Seq[UserRow]]
    }
    
    class PostgresDatabaseClient @Inject() (dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
      private val dbConfig = dbConfigProvider.get[ExPostgresProfile]
      import dbConfig._
      import profile.api._
      private val users = TableQuery[UserTable]
      private lazy val addUserQuery = users returning users.map(_.id) into (
        (f, id) => f.copy(id = Some(id))
        )
    
      def addOrUpdateUser(userRow: UserRow): Future[UserRow] = db.run {
        for {
line 29 >          maybeExisting: Option[UserRow] <- this.users.filter(_.id === userRow.id).result.headOption
          newRow: UserRow = maybeExisting match {
            case Some(r) => userRow.withId(r.id)
            case _ => userRow
          }
          result: Option[UserRow] <- addUserQuery.insertOrUpdate(newRow)
        } yield result.getOrElse(newRow)
      }
    
      def listUsers(): Future[Seq[UserRow]] = db.run {
        users.result
      }
    }

I get the following build errors

C:\xxxxxxxxx\api\app\db\DatabaseClient.scala:29:38
type mismatch;
 found   : ((Option[db.UserRow], db.UserRow)) => slick.dbio.DBIOAction[db.UserRow,slick.dbio.NoStream,Nothing]
 required: ((Option[db.UserRow], db.UserRow)) => slick.dbio.DBIOAction[db.UserRow,slick.dbio.NoStream,E2]
      maybeExisting: Option[UserRow] <- this.users.filter(_.id === userRow.id).result.headOption

and dont understand why, or what e2 is. i have added "line 29 >" to line 29 ,, that is not there in the code

0

There are 0 best solutions below