From 5c2ba7397e4ed161938d8e5db3fb61919937a28a Mon Sep 17 00:00:00 2001 From: adam Date: Fri, 23 May 2025 17:15:33 -0400 Subject: [PATCH] rememberer API controller, more direct but I need other stuff to use it instead of old ones --- Behaver.cs | 2 +- Rememberer.cs | 79 ++++++- .../Controllers/api/ChannelsControler.cs | 89 -------- .../Controllers/api/RemembererController.cs | 212 ++++++++++++++++++ .../api/{UAC.cs => UACsController.cs} | 0 .../Controllers/api/UsersController.cs | 40 ---- 6 files changed, 290 insertions(+), 132 deletions(-) delete mode 100644 WebInterface/Controllers/api/ChannelsControler.cs create mode 100644 WebInterface/Controllers/api/RemembererController.cs rename WebInterface/Controllers/api/{UAC.cs => UACsController.cs} (100%) delete mode 100644 WebInterface/Controllers/api/UsersController.cs diff --git a/Behaver.cs b/Behaver.cs index 10b2da0..f93acf5 100644 --- a/Behaver.cs +++ b/Behaver.cs @@ -1,5 +1,5 @@ namespace vassago; -#pragma warning disable 4014 //the "not awaited" error +#pragma warning disable 4014 using gray_messages.chat; using franz; using vassago.Behavior; diff --git a/Rememberer.cs b/Rememberer.cs index 4634b7e..5d7140c 100644 --- a/Rememberer.cs +++ b/Rememberer.cs @@ -110,16 +110,51 @@ public static class Rememberer dbAccessSemaphore.Wait(); db.Accounts.Remove(toForget); db.SaveChanges(); - dbAccessSemaphore.Release(); + dbAccessSemaphore.Release(); } } + public static void ForgetAttachment(Attachment toForget) + { + dbAccessSemaphore.Wait(); + db.Attachments.Remove(toForget); + db.SaveChanges(); + dbAccessSemaphore.Release(); + } public static void ForgetChannel(Channel toForget) { + if (toForget.SubChannels?.Count > 0) + { + foreach (var childChannel in toForget.SubChannels) + { + ForgetChannel(childChannel); + } + } + if(toForget.Users?.Count > 0) + { + foreach(var account in toForget.Users) + { + ForgetAccount(account); + } + } dbAccessSemaphore.Wait(); db.Channels.Remove(toForget); db.SaveChanges(); dbAccessSemaphore.Release(); } + public static void ForgetMessage(Message toForget) + { + dbAccessSemaphore.Wait(); + db.Messages.Remove(toForget); + db.SaveChanges(); + dbAccessSemaphore.Release(); + } + public static void ForgetUAC(UAC toForget) + { + dbAccessSemaphore.Wait(); + db.UACs.Remove(toForget); + db.SaveChanges(); + dbAccessSemaphore.Release(); + } public static void ForgetUser(User toForget) { dbAccessSemaphore.Wait(); @@ -143,6 +178,22 @@ public static class Rememberer dbAccessSemaphore.Release(); return toReturn; } + public static Account AccountDetail(Guid Id) + { + Account toReturn; + dbAccessSemaphore.Wait(); + toReturn = db.Accounts.Find(Id); + dbAccessSemaphore.Release(); + return toReturn; + } + public static Attachment AttachmentDetail(Guid Id) + { + Attachment toReturn; + dbAccessSemaphore.Wait(); + toReturn = db.Attachments.Find(Id); + dbAccessSemaphore.Release(); + return toReturn; + } public static Channel ChannelDetail(Guid Id) { Channel toReturn; @@ -154,6 +205,30 @@ public static class Rememberer // .Include(u => u.Users) // .Include(u => u.ParentChannel); } + public static Message MessageDetail(Guid Id) + { + Message toReturn; + dbAccessSemaphore.Wait(); + toReturn = db.Messages.Find(Id); + dbAccessSemaphore.Release(); + return toReturn; + } + public static UAC UACDetail(Guid Id) + { + UAC toReturn; + dbAccessSemaphore.Wait(); + toReturn = db.UACs.Find(Id); + dbAccessSemaphore.Release(); + return toReturn; + } + public static User UserDetail(Guid Id) + { + User toReturn; + dbAccessSemaphore.Wait(); + toReturn = db.Users.Find(Id); + dbAccessSemaphore.Release(); + return toReturn; + } public static List UsersOverview() { List toReturn; @@ -162,7 +237,7 @@ public static class Rememberer dbAccessSemaphore.Release(); return toReturn; } - public static List UACsOverview() + public static List UACsOverview() { List toReturn; dbAccessSemaphore.Wait(); diff --git a/WebInterface/Controllers/api/ChannelsControler.cs b/WebInterface/Controllers/api/ChannelsControler.cs deleted file mode 100644 index 30b08b8..0000000 --- a/WebInterface/Controllers/api/ChannelsControler.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System.Diagnostics; -using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; -using vassago.Models; - -namespace vassago.Controllers.api; - -[Route("api/[controller]")] -[ApiController] -public class ChannelsController : ControllerBase -{ - private readonly ILogger _logger; - - public ChannelsController(ILogger logger) - { - _logger = logger; - } - - [HttpGet("{id}")] - [Produces("application/json")] - public Channel Get(Guid id) - { - return Rememberer.ChannelDetail(id); - } - - [HttpPatch] - [Produces("application/json")] - public IActionResult Patch([FromBody] Channel channel) - { - var fromDb = Rememberer.ChannelDetail(channel.Id); - if (fromDb == null) - { - _logger.LogError($"attempt to update channel {channel.Id}, not found"); - return NotFound(); - } - else - { - _logger.LogDebug($"patching {channel.DisplayName} (id: {channel.Id})"); - } - //settable values: lewdness filter level, meanness filter level. maybe i could decorate them... - fromDb.LewdnessFilterLevel = channel.LewdnessFilterLevel; - fromDb.MeannessFilterLevel = channel.MeannessFilterLevel; - Rememberer.RememberChannel(fromDb); - return Ok(fromDb); - } - [HttpDelete] - [Produces("application/json")] - public IActionResult Delete([FromBody] Channel channel) - { - var fromDb = Rememberer.ChannelDetail(channel.Id); - if (fromDb == null) - { - _logger.LogError($"attempt to delete channel {channel.Id}, not found"); - return NotFound(); - } - deleteChannel(fromDb); - 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); - } - } - - Rememberer.ForgetChannel(channel); - } - private void deleteAccount(Account account) - { - var user = account.IsUser; - var usersOnlyAccount = user.Accounts?.Count == 1; - - Rememberer.ForgetAccount(account); - - if(usersOnlyAccount) - Rememberer.ForgetUser(user); - } -} diff --git a/WebInterface/Controllers/api/RemembererController.cs b/WebInterface/Controllers/api/RemembererController.cs new file mode 100644 index 0000000..efb24a5 --- /dev/null +++ b/WebInterface/Controllers/api/RemembererController.cs @@ -0,0 +1,212 @@ +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using vassago.Models; + +namespace vassago.Controllers.api; + +[Route("api/[controller]")] +[ApiController] +public class RemembererController : ControllerBase +{ + private readonly ILogger _logger; + + public RemembererController(ILogger logger) + { + _logger = logger; + } + + //Create + [HttpPut] + [Route("Account")] + [Produces("application/json")] + public Account CreateAccount(Guid id) + { + return Rememberer.AccountDetail(id); + } + [HttpPut] + [Route("Attachment")] + [Produces("application/json")] + public Attachment CreateAttachment(Guid id) + { + return Rememberer.AttachmentDetail(id); + } + [HttpPut] + [Route("Channel")] + [Produces("application/json")] + public Channel CreateChannel(Guid id) + { + return Rememberer.ChannelDetail(id); + } + [HttpPut] + [Route("Message")] + [Produces("application/json")] + public Message CreateMessage(Guid id) + { + return Rememberer.MessageDetail(id); + } + [HttpPut] + [Route("UAC")] + [Produces("application/json")] + public UAC CreateUAC(Guid id) + { + return Rememberer.UACDetail(id); + } + [HttpPut] + [Route("User")] + [Produces("application/json")] + public User CreateUser(Guid id) + { + return Rememberer.UserDetail(id); + } + //Read + [HttpGet] + [Route("Account")] + [Produces("application/json")] + public Account GetAccount(Guid id) + { + return Rememberer.AccountDetail(id); + } + [HttpGet] + [Route("Attachment")] + [Produces("application/json")] + public Attachment GetAttachment(Guid id) + { + return Rememberer.AttachmentDetail(id); + } + [HttpGet] + [Route("Channel")] + [Produces("application/json")] + public Channel GetChannel(Guid id) + { + return Rememberer.ChannelDetail(id); + } + [HttpGet] + [Route("Message")] + [Produces("application/json")] + public Message GetMessage(Guid id) + { + return Rememberer.MessageDetail(id); + } + [HttpGet] + [Route("UAC")] + [Produces("application/json")] + public UAC GetUAC(Guid id) + { + return Rememberer.UACDetail(id); + } + [HttpGet] + [Route("User")] + [Produces("application/json")] + public User GetUser(Guid id) + { + return Rememberer.UserDetail(id); + } + //Update + [HttpPatch] + [Route("Channel")] + [Produces("application/json")] + public IActionResult Patch([FromBody] Channel channel) + { + var fromDb = Rememberer.ChannelDetail(channel.Id); + if (fromDb == null) + { + _logger.LogError($"attempt to update channel {channel.Id}, not found"); + return NotFound(); + } + else + { + _logger.LogDebug($"patching {channel.DisplayName} (id: {channel.Id})"); + } + //settable values: lewdness filter level, meanness filter level. maybe i could decorate them... + fromDb.LewdnessFilterLevel = channel.LewdnessFilterLevel; + fromDb.MeannessFilterLevel = channel.MeannessFilterLevel; + Rememberer.RememberChannel(fromDb); + return Ok(fromDb); + } + //Delete + [HttpDelete] + [Route("Account")] + [Produces("application/json")] + public IActionResult DeleteAccount(Guid id) + { + var fromDb = Rememberer.AccountDetail(id); + if (fromDb == null) + { + _logger.LogError($"attempt to delete account {id}, not found"); + return NotFound(); + } + Rememberer.ForgetAccount(fromDb); + return Ok(); + } + [HttpDelete] + [Route("Attachment")] + [Produces("application/json")] + public IActionResult DeleteAttachment(Guid id) + { + var fromDb = Rememberer.AttachmentDetail(id); + if (fromDb == null) + { + _logger.LogError($"attempt to delete attachment {id}, not found"); + return NotFound(); + } + Rememberer.ForgetAttachment(fromDb); + return Ok(); + } + [HttpDelete] + [Route("Channel")] + [Produces("application/json")] + public IActionResult DeleteChannel(Guid id) + { + var fromDb = Rememberer.ChannelDetail(id); + if (fromDb == null) + { + _logger.LogError($"attempt to delete channel {id}, not found"); + return NotFound(); + } + Rememberer.ForgetChannel(fromDb); + return Ok(); + } + [HttpDelete] + [Route("Message")] + [Produces("application/json")] + public IActionResult DeleteMessage(Guid id) + { + var fromDb = Rememberer.MessageDetail(id); + if (fromDb == null) + { + _logger.LogError($"attempt to delete message {id}, not found"); + return NotFound(); + } + Rememberer.ForgetMessage(fromDb); + return Ok(); + } + [HttpDelete] + [Route("UAC")] + [Produces("application/json")] + public IActionResult DeleteUAC(Guid id) + { + var fromDb = Rememberer.UACDetail(id); + if (fromDb == null) + { + _logger.LogError($"attempt to delete uac {id}, not found"); + return NotFound(); + } + Rememberer.ForgetUAC(fromDb); + return Ok(); + } + [HttpDelete] + [Route("User")] + [Produces("application/json")] + public IActionResult DeleteUser(Guid id) + { + var fromDb = Rememberer.UserDetail(id); + if (fromDb == null) + { + _logger.LogError($"attempt to delete user {id}, not found"); + return NotFound(); + } + Rememberer.ForgetUser(fromDb); + return Ok(); + } +} diff --git a/WebInterface/Controllers/api/UAC.cs b/WebInterface/Controllers/api/UACsController.cs similarity index 100% rename from WebInterface/Controllers/api/UAC.cs rename to WebInterface/Controllers/api/UACsController.cs diff --git a/WebInterface/Controllers/api/UsersController.cs b/WebInterface/Controllers/api/UsersController.cs deleted file mode 100644 index c522461..0000000 --- a/WebInterface/Controllers/api/UsersController.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.Diagnostics; -using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; -using vassago.Models; -using vassago.ProtocolInterfaces.DiscordInterface; - -namespace vassago.Controllers.api; - -[Route("api/[controller]")] -[ApiController] -public class UsersController : ControllerBase -{ - private readonly ILogger _logger; - - public UsersController(ILogger logger) - { - _logger = logger; - } - - [HttpPatch] - [Produces("application/json")] - public IActionResult Patch([FromBody] User user) - { - var fromDb = Rememberer.SearchUser(u => u.Id == user.Id); - if (fromDb == null) - { - _logger.LogError($"attempt to update user {user.Id}, not found"); - return NotFound(); - } - else - { - _logger.LogDebug($"patching {user.DisplayName} (id: {user.Id})"); - } - - //TODO: settable values: display name - //fromDb.DisplayName = user.DisplayName; - Rememberer.RememberUser(fromDb); - return Ok(fromDb); - } -} \ No newline at end of file