collapse users - extracted for LinkMe and my own self-registration
All checks were successful
gitea/vassago/pipeline/head This commit looks good

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)
{
//TODO: collapse
CollapseUsers(SelfUser, selfAccount.IsUser);
}
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

View File

@ -74,44 +74,14 @@ public class LinkClose : Behavior
return true;
}
Console.WriteLine($"{secondary.Id} is being consumed into {_primary.IsUser.Id}");
_primary.IsUser.Accounts.AddRange(secondary.Accounts);
foreach(var a in secondary.Accounts)
if(Behaver.Instance.CollapseUsers(_primary.IsUser, secondary))
{
a.IsUser = _primary.IsUser;
}
secondary.Accounts.Clear();
Console.WriteLine("accounts transferred");
try
{
await _db.SaveChangesAsync();
}
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 :)");
}
else
{
await message.Channel.SendMessage("failed :(");
}
return true;
}

View File

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

View File

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