I cannot edit the QueryPermissions
class because it is an external .dll that was given to me to use. Yes, I realize it's a bad practice to use an out variables, but I am pushing for a rewrite of that service.
As you can see below the two methods do the almost the EXACT same thing only one uses a groupkey
and the other uses a userkey
and then calls the appropriate method. For whatever reason I am drawing a blank on refactoring these methods.
public IEnumerable<Node> GetNodesForUser(int userKey, IEnumerable<OnlineReport> onlineReports)
{
var nodes = new List<Node>();
foreach (var onlineReport in onlineReports)
{
var parentNode = new Node {Text = onlineReport.Key, Value = onlineReport.Value};
var webQueries = new List<WebQuery>();
//Line that is different
_QueryPermissions.GetQueriesForTaskAndUser(Int32.Parse(onlineReport.Value), userKey, ref webQueries, true);
foreach (var childNode in webQueries.Select(webQuery => new Node(parentNode){Text = webQuery.QueryTitle, Value = webQuery.QueryKey.ToString()}))
{
parentNode.Children.Add(childNode);
}
nodes.Add(parentNode);
}
return nodes;
}
public IEnumerable<Node> GetNodesForGroup(int groupKey, IEnumerable<OnlineReport> onlineReports)
{
var nodes = new List<Node>();
foreach (var onlineReport in onlineReports)
{
var parentNode = new Node { Text = onlineReport.Key, Value = onlineReport.Value };
var webQueries = new List<WebQuery>();
//Line that is different
_QueryPermissions.GetQueriesForTaskAndUserGroup(Int32.Parse(onlineReport.Value), groupKey, ref webQueries, true);
foreach (var childNode in webQueries.Select(webQuery => new Node(parentNode) { Text = webQuery.QueryTitle, Value = webQuery.QueryKey.ToString() }))
{
parentNode.Children.Add(childNode);
}
nodes.Add(parentNode);
}
return nodes;
}