collapse users - extracted for LinkMe and my own self-registration

This commit is contained in:
Adam R Grey 2024-01-05 22:12:57 -05:00
parent efb4ab00d2
commit c5f9ae2c6b
4 changed files with 47 additions and 38 deletions

View File

@ -77,9 +77,48 @@ public class Behaver
} }
else if (SelfUser != selfAccount.IsUser) else if (SelfUser != selfAccount.IsUser)
{ {
//TODO: collapse CollapseUsers(SelfUser, selfAccount.IsUser);
} }
SelfAccounts = _db.Accounts.Where(a => a.IsUser == SelfUser).ToList(); SelfAccounts = _db.Accounts.Where(a => a.IsUser == SelfUser).ToList();
} }
public bool CollapseUsers(User primary, User secondary)
{
Console.WriteLine($"{secondary.Id} is being consumed into {primary.Id}");
primary.Accounts.AddRange(secondary.Accounts);
foreach(var a in secondary.Accounts)
{
a.IsUser = primary;
}
secondary.Accounts.Clear();
Console.WriteLine("accounts transferred");
try
{
_db.SaveChanges();
}
catch(Exception e)
{
Console.WriteLine("First save exception.");
Console.Error.WriteLine(e);
return false;
}
Console.WriteLine("saved");
_db.Users.Remove(secondary);
Console.WriteLine("old account cleaned up");
try
{
_db.SaveChanges();
}
catch(Exception e)
{
Console.WriteLine("Second save exception.");
Console.Error.WriteLine(e);
return false;
}
Console.WriteLine("saved, again, separately");
return true;
}
} }
#pragma warning restore 4014 //the "async not awaited" error #pragma warning restore 4014 //the "async not awaited" error

View File

@ -74,44 +74,14 @@ public class LinkClose : Behavior
return true; return true;
} }
Console.WriteLine($"{secondary.Id} is being consumed into {_primary.IsUser.Id}"); if(Behaver.Instance.CollapseUsers(_primary.IsUser, secondary))
_primary.IsUser.Accounts.AddRange(secondary.Accounts);
foreach(var a in secondary.Accounts)
{ {
a.IsUser = _primary.IsUser; await message.Channel.SendMessage("done :)");
} }
secondary.Accounts.Clear(); else
Console.WriteLine("accounts transferred");
try
{ {
await _db.SaveChangesAsync(); await message.Channel.SendMessage("failed :(");
} }
catch(Exception e)
{
message.Channel.SendMessage("error in first save");
Console.WriteLine("fucks sake if I don't catch Exception it *mysteriously vanishes*");
Console.Error.WriteLine(e);
return false;
}
Console.WriteLine("saved");
_db.Users.Remove(secondary);
Console.WriteLine("old account cleaned up");
try
{
await _db.SaveChangesAsync();
}
catch(Exception e)
{
message.Channel.SendMessage("error in second save");
Console.WriteLine("fucks sake if I don't catch Exception it *mysteriously vanishes*");
Console.Error.WriteLine(e);
return false;
}
Console.WriteLine("saved, again, separately");
await message.Channel.SendMessage("done :)");
return true; return true;
} }

View File

@ -22,8 +22,8 @@ namespace vassago
public async Task StartAsync(CancellationToken cancellationToken) public async Task StartAsync(CancellationToken cancellationToken)
{ {
var dbc = new ChattingContext(); var dbc = new ChattingContext();
dbc.Database.EnsureCreated(); await dbc.Database.EnsureCreatedAsync();
dbc.Database.Migrate(); await dbc.Database.MigrateAsync();
if (DiscordTokens?.Any() ?? false) if (DiscordTokens?.Any() ?? false)
foreach (var dt in DiscordTokens) foreach (var dt in DiscordTokens)

View File

@ -7,7 +7,7 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IHostedService, vassago.ConsoleService>(); builder.Services.AddSingleton<IHostedService, vassago.ConsoleService>();
builder.Services.AddDbContext<ChattingContext>(options => builder.Services.AddDbContext<ChattingContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("ChattingContext"))); options.UseNpgsql(builder.Configuration.GetConnectionString("ChattingContext") ),ServiceLifetime.Transient);
var app = builder.Build(); var app = builder.Build();