I'm new to Scala and I'm building a Play app to learn Scala and Slick. The app has todo lists and each list has items. Each user has multiple lists. I've written a controller to get the list as JSON, and the JSON includes all the items in the list.
def viewList(id: Long) = AuthenticatedAction.async { implicit request =>
val userId = request.user.toLong;
db.run(todoLists.filter(listById(id, userId)).result.headOption).flatMap { list =>
list match {
case Some(_) => {
val json = toJson(list).as[JsObject]
// Fetch the items in this list and add them to the JSON response
db.run(todoItems.filter(_.listId === id).sortBy(_.text).result).map { items =>
Ok(json + ("items" -> toJson(items)))
}
}
case None => Future.successful(NotFound)
}
}
}
Is it possible to write this function using a for comprehension? I have a nested flatMap+map call, so it seems like it should be possible.