Skip to content

Commit

Permalink
Fetch correct number of total replies for posts.
Browse files Browse the repository at this point in the history
  • Loading branch information
aminsys committed Oct 2, 2024
1 parent 83988e8 commit a44f3b5
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion HackerNewsApp2/Model/HNAlgoliaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class HNAlgoliaModel
public string Url { get; set; }
public HNAlgoliaModel[] Children { get; set; }
public int Descendants {
get => Children.Length;
get => TotalReplies(this) + Children.Length;
set => Descendants = value;
}

Expand Down Expand Up @@ -65,5 +65,19 @@ public string Text
}
set => text = value;
}

private int TotalReplies(HNAlgoliaModel post)
{
int totalReplies = 0;
if(post.Children == null)
{
return 0;
}

foreach (var child in post.Children) {
totalReplies += child.Children.Length + TotalReplies(child);
}
return totalReplies;
}
}
}

0 comments on commit a44f3b5

Please sign in to comment.