Created
July 23, 2015 11:18
-
-
Save michalfaber/768d76dad207aadfa3e5 to your computer and use it in GitHub Desktop.
Flat structure -> chierarchy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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