channels and accounts organized into a treeview

even if every account is orphaned, this isn't how it should be
This commit is contained in:
Adam R Grey 2024-05-26 19:43:17 -04:00
parent c446521977
commit b4b5544ec4
2 changed files with 191 additions and 54 deletions

View File

@ -1,5 +1,8 @@
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using System.Web;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileSystemGlobbing.Internal.PathSegments;
using vassago.Models; using vassago.Models;
namespace vassago.Controllers; namespace vassago.Controllers;
@ -7,15 +10,152 @@ namespace vassago.Controllers;
public class HomeController : Controller public class HomeController : Controller
{ {
private readonly ILogger<HomeController> _logger; private readonly ILogger<HomeController> _logger;
private readonly ChattingContext _db;
public HomeController(ILogger<HomeController> logger) public HomeController(ILogger<HomeController> logger, ChattingContext db)
{ {
_logger = logger; _logger = logger;
_db = db;
} }
public IActionResult Index() public IActionResult Index()
{ {
return View(); var allAccounts = _db.Accounts.ToList();
var allChannels = _db.Channels.ToList();
var sb = new StringBuilder();
sb.Append("[");
sb.Append("{text: \"channels\", nodes: [");
var first = true;
foreach (var topLevelChannel in _db.Channels.Where(x => x.ParentChannel == null))
{
if (first)
{
first = false;
}
else
{
sb.Append(',');
}
serializeChannel(ref sb, ref allChannels, ref allAccounts, topLevelChannel);
}
sb.Append("]},");
if (allChannels.Any())
{
sb.Append("{text: \"orphaned channels\", nodes: [");
first = true;
while (true)
{
if (first)
{
first = false;
}
else
{
sb.Append(',');
}
serializeChannel(ref sb, ref allChannels, ref allAccounts, allChannels.First());
if (!allChannels.Any())
{
break;
}
}
sb.Append("]},");
}
if (allAccounts.Any())
{
sb.Append("{text: \"orphaned accounts\", nodes: [");
first = true;
foreach (var acc in allAccounts)
{
if (first)
{
first = false;
}
else
{
sb.Append(',');
}
serializeAccount(ref sb, acc);
}
sb.Append("]}");
}
sb.Append("]");
ViewData.Add("treeString", sb.ToString());
return View("Index");
}
private void serializeChannel(ref StringBuilder sb, ref List<Channel> allChannels, ref List<Account> allAccounts, Channel currentChannel)
{
allChannels.Remove(currentChannel);
sb.Append($"{{\"text\": \"{currentChannel.DisplayName}\"");
var theseAccounts = allAccounts.Where(a => a.SeenInChannel?.Id == currentChannel.Id);
allAccounts.RemoveAll(a => a.SeenInChannel?.Id == currentChannel.Id);
var first = true;
if (currentChannel.SubChannels != null || theseAccounts != null)
{
sb.Append(", \"nodes\": [");
}
if (currentChannel.SubChannels != null)
{
foreach (var subChannel in currentChannel.SubChannels ?? new List<Channel>())
{
if (first)
{
first = false;
}
else
{
sb.Append(',');
}
serializeChannel(ref sb, ref allChannels, ref allAccounts, subChannel);
}
if (theseAccounts != null)
{
sb.Append(',');
}
}
if (theseAccounts != null)
{
first = true;
sb.Append($"{{\"text\": \"(accounts: {theseAccounts.Count()})\", \"expanded\":true, nodes:[");
foreach (var account in theseAccounts)
{
if (first)
{
first = false;
}
else
{
sb.Append(',');
}
serializeAccount(ref sb, account);
}
sb.Append("]}");
}
sb.Append("]}");
}
private void serializeAccount(ref StringBuilder sb, Account currentAccount)
{
sb.Append($"{{\"text\": \"{currentAccount.DisplayName}\"}}");
}
private void serializeUser(ref StringBuilder sb, ref List<Account> allAccounts, User currentUser)
{
sb.Append($"{{\"text\": \"{currentUser.DisplayName}\", ");
var ownedAccounts = allAccounts.Where(a => a.IsUser == currentUser);
sb.Append("nodes: [");
sb.Append($"{{\"text\": \"owned accounts:\", \"nodes\": [");
if (ownedAccounts != null)
{
foreach (var acc in ownedAccounts)
{
serializeAccount(ref sb, acc);
sb.Append(',');
}
}
sb.Append("]}]}");
} }
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

View File

@ -2,62 +2,59 @@
ViewData["Title"] = "Home Page"; ViewData["Title"] = "Home Page";
} }
<div id="tree"></div> <div id="tree"></div>
<div> tree above.
<a href="Users">Users</a>
</div>
<div>
<a href="Accounts">Accounts</a>
</div>
<div>
<a href="Channels">Channels</a>
</div>
<div>
<a href="Behaviors">Behaviors</a>
</div>
<div id="tree"></div>
@section Scripts{ @section Scripts{
<script type="text/javascript"> <script type="text/javascript">
function getTree() { function getTree() {
// Some logic to retrieve, or generate tree structure // Some logic to retrieve, or generate tree structure
var tree = [ var tree = @Html.Raw(ViewData["treeString"]);
{ console.log(tree);
text: "Parent 1", @* var channels = {text: "channels", nodes: []};
nodes: [ @{
{ var doThing = (currentChannel) => {};
text: "Child 1", foreach(Channel c in ViewData["Channels"])
nodes: [
{ {
text: "Grandchild 1" channels.push(currentChannel);
},
{
text: "Grandchild 2"
} }
] } *@
}, var throwaway =
{ [
text: "Child 2" {
} text: "Parent 1",
] nodes: [
}, {
{ text: "Child 1",
text: "Parent 2" nodes: [
}, {
{ text: "Grandchild 1"
text: "Parent 3" },
}, {
{ text: "Grandchild 2"
text: "Parent 4" }
}, ]
{ },
text: "Parent 5" {
text: "Child 2"
}
]
},
{
text: "Parent 2"
},
{
text: "Parent 3"
},
{
text: "Parent 4"
},
{
text: "Parent 5"
}
];
return tree;
} }
];
return tree;
}
$('#tree').bstreeview({data: getTree()}); $('#tree').bstreeview({ data: getTree() });
</script> </script>
} }