Question

I am writing website using play 2.0 framework. And I have a problem when parsing results. This request to mysql db gets all the links(can be several per episode) added to the database per episode together with all the information about episode and anime.

  def lastReleasedLink(limit:Long=5):List[(((Episode,Anime),Link),Genre)] = {
    DB.withConnection { implicit c =>
      SQL(
        """
          select * from yas_episodes as a
          inner join anime as b on a.ep_anime_id=b.id
          left outer join yas_links as c on a.ep_id=c.ep_id
          LEFT JOIN yas_animes_genres AS d ON a.ep_anime_id = d.ag_anime_id
          INNER JOIN yas_genres AS e ON e.g_id = d.ag_genre_id
          where c.ep_id IS NOT NULL group by c.ep_id order by c.date desc limit {limit}
        """).on('limit ->limit)as(Episode.simple~Anime.simple~Link.simple~Genre.simple map{
        case episode~anime~link~genre => episode -> anime -> link -> Genre
      } *)
    }
  }

The return value is type of List[(((Episode,Anime),Link),Genre)] but how can I form output to the list of let say List[episode,anime,Seq[links]] or List[episode,anime,Seq[Genres],Seq[links]] don't know where to put genres.

You can imagine that when you have two links per one episode information from table anime and yas_episodes will be copied for every row. So I need somehow stack them together(group) by episode record. Then it will be possible to iterate list and access to all objects.

As you can see, in the request there is many-to-many relation of anime with genres. I have no idea how can I put all together to one list to be able to access it in view. Should Genre be part of Anime model?

Was it helpful?

Solution

It seems that the preferred way to achieve this is using the Scala collection API, see this post by Guillaume Bort.

In your case, I think you could do something like

lastReleasedLink groupBy { case (((e, a), l), g) => (e, a) }

to group by (Episode, Anime). Generally speaking, those manipulations are probably a little easier if you change your function to return a List[(Episode, Anime, Link, Genre)], i.e.

case episode~anime~link~genre => (episode, anime, link, genre)

then you could achieve the same with this code:

lastReleasedLink groupBy ((_._1, _._2))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top