Skip to content

Instantly share code, notes, and snippets.

@michalfaber
Created July 23, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalfaber/768d76dad207aadfa3e5 to your computer and use it in GitHub Desktop.
Save michalfaber/768d76dad207aadfa3e5 to your computer and use it in GitHub Desktop.
Flat structure -> chierarchy
def comments(postId: Int) : Future[Seq[CommentNode]] = {
val query = for {
comment <- Comments if comment.objectId === postId
author <- Users if author.id === comment.userId
} yield (author, comment)
val comments: Future[Seq[Comment]] = for {
result <- db.run(query.result)
} yield result.map { case (author, comment) =>
Comment(comment.id, comment.replyToId, author.name, author.site, comment.date, comment.body, comment.bodyHtml)
}
comments.map { m =>
var newMap: mutable.Map[Int, CommentNode] = mutable.HashMap()
m.foreach { c =>
newMap = newMap + (c.id -> CommentNode(c))
}
newMap.values.foreach { cm =>
cm.data.replyToId.foreach { parentId =>
newMap.get(parentId).foreach { n =>
cm.parent = Some(n)
n.children = n.children :+ cm
}
}
}
newMap.values.filter(_.parent.isEmpty).toSeq
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment