refactor: add SqlKata for SQL generation, move connection handling into ModelRepository
This commit is contained in:
@@ -23,20 +23,18 @@ namespace PluralKit.Bot
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
var account = await ctx.MatchUser() ?? throw new PKSyntaxError("You must pass an account to link with (either ID or @mention).");
|
||||
var accountIds = await _repo.GetSystemAccounts(conn, ctx.System.Id);
|
||||
var accountIds = await _repo.GetSystemAccounts(ctx.System.Id);
|
||||
if (accountIds.Contains(account.Id))
|
||||
throw Errors.AccountAlreadyLinked;
|
||||
|
||||
var existingAccount = await _repo.GetSystemByAccount(conn, account.Id);
|
||||
var existingAccount = await _repo.GetSystemByAccount(account.Id);
|
||||
if (existingAccount != null)
|
||||
throw Errors.AccountInOtherSystem(existingAccount);
|
||||
|
||||
var msg = $"{account.Mention()}, please confirm the link.";
|
||||
if (!await ctx.PromptYesNo(msg, "Confirm", user: account, matchFlag: false)) throw Errors.MemberLinkCancelled;
|
||||
await _repo.AddAccount(conn, ctx.System.Id, account.Id);
|
||||
await _repo.AddAccount(ctx.System.Id, account.Id);
|
||||
await ctx.Reply($"{Emojis.Success} Account linked to system.");
|
||||
}
|
||||
|
||||
@@ -44,20 +42,18 @@ namespace PluralKit.Bot
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
ulong id;
|
||||
if (!ctx.MatchUserRaw(out id))
|
||||
throw new PKSyntaxError("You must pass an account to link with (either ID or @mention).");
|
||||
|
||||
var accountIds = (await _repo.GetSystemAccounts(conn, ctx.System.Id)).ToList();
|
||||
var accountIds = (await _repo.GetSystemAccounts(ctx.System.Id)).ToList();
|
||||
if (!accountIds.Contains(id)) throw Errors.AccountNotLinked;
|
||||
if (accountIds.Count == 1) throw Errors.UnlinkingLastAccount;
|
||||
|
||||
var msg = $"Are you sure you want to unlink <@{id}> from your system?";
|
||||
if (!await ctx.PromptYesNo(msg, "Unlink")) throw Errors.MemberUnlinkCancelled;
|
||||
|
||||
await _repo.RemoveAccount(conn, ctx.System.Id, id);
|
||||
await _repo.RemoveAccount(ctx.System.Id, id);
|
||||
await ctx.Reply($"{Emojis.Success} Account unlinked.");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user