Slick 3: how to drop and take on collections with some relations

对着背影说爱祢 提交于 2019-12-05 18:37:48

I found the following solution (with 2 queries but 1 DB call):

def findAllByGenre(genreName: String, offset: Int, numberToReturn: Int): Future[Seq[ArtistWithWeightedGenres]] = {
    val query = for {
      genre <- genres.filter(_.name === genreName)
      artistGenre <- artistsGenres.filter(_.genreId === genre.id)
      artist <- artists.filter(_.id === artistGenre.artistId)

    } yield artist

    val artistsIdFromDB = query.drop(offset).take(numberToReturn) map (_.id)

    val query2 = for {
      artistWithGenres <- artists.filter(_.id in artistsIdFromDB) joinLeft
        (artistsGenres join genres on (_.genreId === _.id)) on (_.id === _._1.artistId)
    } yield artistWithGenres

    db.run(query2.result) map { seqArtistAndOptionalGenre =>
      ArtistsAndOptionalGenresToArtistsWithGenres(seqArtistAndOptionalGenre)
    } map(_.toVector)
  }

If anyone has a better solution...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!