Oh yes, of course this is wrong. It should be something like:
case (workoutWithAthlete, comments) => (workoutWithAthlete, comment.map(_._2))
Btw, to explain what I'm trying to do here:
1) You already have a RowParser[(Workout,Athlete)] called
Workout.withAthlete that is able to extract a tuple (Workout,Athlete)
from a ResultSet row.
2) You already have a RowParser[Comment] called Comment.simple that is
able to extract a Commet from a ResultSet row.
3) Let's compose both to have a RowParser[(Workout,Athlete)~Comment]:
----
Workout.withAthlete ~ Comment.simple
----
4) Now, let's use this to construct a
ResultSetParser[List[(Workout,Athlete)~Comment]]. This is a parser
able to give me a List[(Workout,Athlete)~Comment] from a ResultSet:
----
(Workout.withAthlete ~ Comment.simple) *
----
This parser will give you something like:
----
List(
(myWorkout1,myAthlete1)~comment1,
(myWorkout1,myAthlete1)~comment2,
(myWorkout1,myAthlete1)~comment3
)
----
That is a simple representation of your JDBC ResultSet
5) Let's transform this
ResultSetParser[List[(Workout,Athlete)~Comment]] to a
ResultSetParser[Option[((Workout,Athlete),List[Comment]]] because this
is what you want.
We use the `map` operation to do that (it transforms a
ResultSetParser[A] to a ResultSetParser[B] given a A => B function).
We want to transform:
----
List(
(myWorkout1,myAthlete1)~comment1,
(myWorkout1,myAthlete1)~comment2,
(myWorkout1,myAthlete1)~comment3
)
----
to
----
Some( ( (myWorkout1,myAthlete1), List(comment1,comment2comment3) ) )
----
And
----
List()
----
to
----
None
----
Let's start by grouping by the first item:
----
items.groupBy(_._1)
----
So we get:
----
Map(
(myWorkout1,myAthlete1),
List(
(myWorkout1,myAthlete1)~comment1,
(myWorkout1,myAthlete1)~comment2,
(myWorkout1,myAthlete1)~comment3
)
)
----
Basically we get a Map with a single key if the query returned any
result, or an empty Map if the query returned no results.
Let's transform it to an Option:
----
items.groupBy(_._1).headOption
----
So we get:
----
Some(
(
(myWorkout1,myAthlete1),
List(
(myWorkout1,myAthlete1)~comment1,
(myWorkout1,myAthlete1)~comment2,
(myWorkout1,myAthlete1)~comment3
)
)
)
----
(or None if the Map was empty)
Now, we just want to filter out the repeated (myWorkout1,myAthlete1)
in the List to keep the comment only:
----
items.groupBy(_._1).headOption.map {
case (workoutWithAthlete, list) => (workoutWithAthlete, list.map(_._2))
}
----
And we finally get:
----
Some(
(
(myWorkout1,myAthlete1),
List(
comment1,
comment2,
comment3
)
)
)
----
Or None if the initial list was empty.
Finally, here is the whole stuff altogether that you can assign to a
value and reuse as a generic parser to parse the result of several
different queries:
----
val withAthleteAndComments = {
(Workout.withAthlete ~ Comment.simple *) map { items =>
items.groupBy(_._1).headOption.map {
case (workoutWithAthlete, list) => (workoutWithAthlete, list.map(_._2))
}
}
}
----
>> > To post to this group, send email to
play-fr...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> >
play-framewor...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/play-framework/-/EjkwtUA27nsJ.
>
> To post to this group, send email to
play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
>
play-framewor...@googlegroups.com.