Add switch editing functionality

This commit is contained in:
BeautifulPixel
2021-09-26 20:08:38 -04:00
committed by spiral
parent e3fa0f3b32
commit 7d60b3e7cf
5 changed files with 92 additions and 4 deletions

View File

@@ -39,6 +39,33 @@ namespace PluralKit.Core
_logger.Information("Created {SwitchId} in {SystemId}: {Members}", sw.Id, system, members);
}
public async Task EditSwitch(IPKConnection conn, SwitchId switchId, IReadOnlyCollection<MemberId> members)
{
// Use a transaction here since we're doing multiple executed commands in one
await using var tx = await conn.BeginTransactionAsync();
// Remove the old members from the switch
await conn.ExecuteAsync("delete from switch_members where switch = @Switch",
new {Switch = switchId});
// Add the new members
await using (var w = conn.BeginBinaryImport("copy switch_members (switch, member) from stdin (format binary)"))
{
foreach (var member in members)
{
await w.StartRowAsync();
await w.WriteAsync(switchId.Value, NpgsqlDbType.Integer);
await w.WriteAsync(member.Value, NpgsqlDbType.Integer);
}
await w.CompleteAsync();
}
// Finally we commit the tx, since the using block will otherwise rollback it
await tx.CommitAsync();
_logger.Information("Updated {SwitchId} members: {Members}", switchId, members);
}
public async Task MoveSwitch(IPKConnection conn, SwitchId id, Instant time)
{