Add linked suggestions with synced voting
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using GameList.Data;
|
||||
using GameList.Domain;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -163,4 +164,35 @@ internal static class EndpointHelpers
|
||||
ResultsOpen = false,
|
||||
UpdatedAt = DateTimeOffset.UnixEpoch
|
||||
};
|
||||
|
||||
public static Dictionary<int, int> BuildLinkRoots(IEnumerable<(int Id, int? ParentId)> items)
|
||||
{
|
||||
var parentMap = items.ToDictionary(x => x.Id, x => x.ParentId);
|
||||
var roots = new Dictionary<int, int>();
|
||||
foreach (var id in parentMap.Keys)
|
||||
{
|
||||
roots[id] = FindRootId(id, parentMap);
|
||||
}
|
||||
return roots;
|
||||
}
|
||||
|
||||
public static int FindRootId(int suggestionId, IReadOnlyDictionary<int, int?> parentMap)
|
||||
{
|
||||
var current = suggestionId;
|
||||
var visited = new HashSet<int>();
|
||||
|
||||
while (parentMap.TryGetValue(current, out var parent) && parent is int p && !visited.Contains(p))
|
||||
{
|
||||
visited.Add(current);
|
||||
current = p;
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
public static List<int> LinkedIdsFor(int suggestionId, IReadOnlyDictionary<int, int> rootIndex)
|
||||
{
|
||||
if (!rootIndex.TryGetValue(suggestionId, out var root)) return new();
|
||||
return rootIndex.Where(kv => kv.Value == root).Select(kv => kv.Key).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user