Add switch editing functionality
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user