rememberer API controller, more direct

but I need other stuff to use it instead of old ones
This commit is contained in:
adam 2025-05-23 17:15:33 -04:00
parent 22ee1d2567
commit 5c2ba7397e
6 changed files with 290 additions and 132 deletions

View File

@ -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;

View File

@ -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<User> UsersOverview()
{
List<User> toReturn;
@ -162,7 +237,7 @@ public static class Rememberer
dbAccessSemaphore.Release();
return toReturn;
}
public static List<UAC> UACsOverview()
public static List<UAC> UACsOverview()
{
List<UAC> toReturn;
dbAccessSemaphore.Wait();

View File

@ -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<ChannelsController> _logger;
public ChannelsController(ILogger<ChannelsController> 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);
}
}

View File

@ -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<RemembererController> _logger;
public RemembererController(ILogger<RemembererController> 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();
}
}

View File

@ -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<ChannelsController> _logger;
public UsersController(ILogger<ChannelsController> 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);
}
}