Search code examples
scalaplayframeworkslick

slick: what type do I need get back to iterate though a list in a view


The code below works find to show the most recent post by a user, but now I would like to get all the posts created by a user. How could I implement that?

What is confusing me is that the type that I am supposed to get back. It is Future[Option[Seq[Blog]]] or Future[Seq[Option[Blog]] or is there any better solutions?

  def find(user: User): Future[Option[Blog]] = {
    val blogQuery = blogs.filter(_.userID === user.userID.toString).sortBy(_.createdAt.desc)

     db.run(blogQuery.result.headOption).map { dbBlogOption =>
       dbBlogOption.map {
         dbBlog => Blog(None, dbBlog.title, dbBlog.content, UUID.fromString(dbBlog.userID), DateTime.parse(dbBlog.createdAt))
      }
    }
  }

Solution

  • Just leave out the .headOption and you'll get a Future[Seq[Blog]] . Note that it can be empty if none found, thus you don't need an Option.