diff --git a/Behaver.cs b/Behaver.cs index 42f2199..c225422 100644 --- a/Behaver.cs +++ b/Behaver.cs @@ -31,6 +31,8 @@ public class Behaver private static readonly Behaver _instance = new Behaver(); + //TODO: you know why I didn't make this a static class? lifecycle issues with the dbcontext. but now that we don't have a stored instance, + //no need to have a... *checks over shoulder*... *whispers*: singleton public static Behaver Instance { get { return _instance; } @@ -80,6 +82,7 @@ public class Behaver CollapseUsers(SelfUser, selfAccount.IsUser, db); } SelfAccounts = db.Accounts.Where(a => a.IsUser == SelfUser).ToList(); + db.SaveChanges(); } public bool CollapseUsers(User primary, User secondary, ChattingContext db) diff --git a/Models/ChattingContext.cs b/Models/ChattingContext.cs index 6ffbc66..43490e2 100644 --- a/Models/ChattingContext.cs +++ b/Models/ChattingContext.cs @@ -16,7 +16,7 @@ public class ChattingContext : DbContext public ChattingContext() : base() { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseNpgsql(Shared.DBConnectionString) - .EnableSensitiveDataLogging(true); //who the fuck is looking at log output but not allowed to see it? this should be on by default. + optionsBuilder.UseNpgsql(Shared.DBConnectionString); + //.EnableSensitiveDataLogging(true); //"sensitive" is one thing. writing "did something" every time you think a thought is a different thing. } } \ No newline at end of file diff --git a/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs b/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs index 3f13289..805bbd7 100644 --- a/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs +++ b/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs @@ -198,7 +198,7 @@ public class DiscordInterface a.Filename = dAttachment.Filename; a.Size = dAttachment.Size; a.Source = new Uri(dAttachment.Url); - + db.SaveChanges(); return a; } internal Message UpsertMessage(IUserMessage dMessage) @@ -234,6 +234,7 @@ public class DiscordInterface m.Reply = (t) => { return dMessage.ReplyAsync(t); }; m.React = (e) => { return attemptReact(dMessage, e); }; + db.SaveChangesAsync(); return m; } internal Channel UpsertChannel(IMessageChannel channel) @@ -276,6 +277,7 @@ public class DiscordInterface c.DisplayName = "DM: " + (channel as IPrivateChannel).Recipients?.FirstOrDefault(u => u.Id != client.CurrentUser.Id).Username; break; } + db.SaveChangesAsync(); return c; } internal Channel UpsertChannel(IGuild channel) @@ -286,6 +288,7 @@ public class DiscordInterface { c = new Channel(); db.Channels.Add(c); + Console.WriteLine($"upserting channel {channel.Name} from discord, have to create a new one in the DB"); } c.DisplayName = channel.Name; @@ -299,6 +302,7 @@ public class DiscordInterface c.SendMessage = (t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; cannot accept text"); }; c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); }; + db.SaveChanges(); return c; } internal Account UpsertAccount(IUser user, Channel inChannel) @@ -323,6 +327,7 @@ public class DiscordInterface acc.IsUser = new User() { Accounts = new List() { acc } }; db.Users.Add(acc.IsUser); } + db.SaveChanges(); return acc; } diff --git a/WebInterface/Controllers/ChannelsController.cs b/WebInterface/Controllers/ChannelsController.cs index 188f40d..93b4b75 100644 --- a/WebInterface/Controllers/ChannelsController.cs +++ b/WebInterface/Controllers/ChannelsController.cs @@ -47,7 +47,7 @@ public class ChannelsController : Controller } var sb = new StringBuilder(); sb.Append("["); - sb.Append("{text: \"channels\", nodes: ["); + sb.Append($"{{text: \"{channel.SubChannels?.Count}\", nodes: ["); var first=true; foreach(var subChannel in channel.SubChannels) { @@ -59,9 +59,9 @@ public class ChannelsController : Controller { first = false; } - sb.Append($"{{\"text\": \"{subChannel.DisplayName}\""); + sb.Append($"{{\"text\": \"{subChannel.DisplayName}\"}}"); } - sb.Append("]"); + sb.Append("]}]"); ViewData.Add("channelsTree", sb.ToString()); return View( diff --git a/WebInterface/Controllers/api/ChannelsControler.cs b/WebInterface/Controllers/api/ChannelsControler.cs index fb3dfd6..8487e64 100644 --- a/WebInterface/Controllers/api/ChannelsControler.cs +++ b/WebInterface/Controllers/api/ChannelsControler.cs @@ -41,4 +41,53 @@ public class ChannelsController : ControllerBase _db.SaveChanges(); return Ok(fromDb); } + [HttpDelete] + [Produces("application/json")] + public IActionResult Delete([FromBody] Channel channel) + { + var fromDb = _db.Channels.Find(channel.Id); + if (fromDb == null) + { + _logger.LogError($"attempt to delete channel {channel.Id}, not found"); + return NotFound(); + } + deleteChannel(fromDb); + _db.SaveChanges(); + return Ok(); + } + private void deleteChannel(Channel channel) + { + if (channel.SubChannels?.Count > 0) + { + foreach (var childChannel in channel.SubChannels) + { + deleteChannel(childChannel); + } + } + + if(channel.Users?.Count > 0) + { + foreach(var account in channel.Users) + { + deleteAccount(account); + } + } + + if(channel.Messages?.Count > 0) + { + _db.Remove(channel.Messages); + } + + _db.Remove(channel); + } + private void deleteAccount(Account account) + { + var user = account.IsUser; + var usersOnlyAccount = user.Accounts?.Count == 1; + + _db.Remove(account); + + if(usersOnlyAccount) + _db.Users.Remove(user); + } } diff --git a/WebInterface/Views/Channels/Details.cshtml b/WebInterface/Views/Channels/Details.cshtml index df872d3..8d9d549 100644 --- a/WebInterface/Views/Channels/Details.cshtml +++ b/WebInterface/Views/Channels/Details.cshtml @@ -93,6 +93,11 @@ Accounts @(ThisChannel.Users?.Count ?? 0) + + + + + @@ -117,15 +122,18 @@ console.log(channelNow); return channelNow; } - @if((ThisChannel.SubChannels?.Count ?? 0) > 0) - { - /* + function forget(){ + console.log("here we go"); + if(window.confirm("delete? really really?") == true){ + deleteModel(jsonifyChannel(), '/api/Channels/'); + } + } + function channelsTree() { - var tree = @Html.Raw(ViewData["treeString"]); + var tree = @Html.Raw(ViewData["channelsTree"]); return tree; } $('#channelsTree').bstreeview({ data: channelsTree() }); - */ - } + } \ No newline at end of file diff --git a/wwwroot/js/site.js b/wwwroot/js/site.js index afe475b..06de7e7 100644 --- a/wwwroot/js/site.js +++ b/wwwroot/js/site.js @@ -20,7 +20,6 @@ function patchModel(model, apiUrl) if(components[2] !== "Details") { console.log("wtf are you doing? " + components[2] + " is something other than Details"); - //add different endpoings here, if you like } var type=components[1]; var id=components[3]; @@ -47,4 +46,35 @@ function patchModel(model, apiUrl) .catch(error => { console.error('Error:', error); }); +} + +function deleteModel(model, apiUrl) +{ + var components = window.location.pathname.split('/'); + if(components[2] !== "Details") + { + console.log("wtf are you doing? " + components[2] + " is something other than Details"); + } + var type=components[1]; + var id=components[3]; + fetch(apiUrl, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(model), + }) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not "ok". which is not ok.'); + } + return response.json(); + }) + .then(returnedSuccessdata => { + // perhaps a success callback + console.log('returnedSuccessdata:', returnedSuccessdata); + }) + .catch(error => { + console.error('Error:', error); + }); } \ No newline at end of file