PluralKit/PluralKit.Core/Database/Repository/ModelRepository.System.cs

79 lines
3.9 KiB
C#
Raw Normal View History

2021-08-27 15:03:47 +00:00
#nullable enable
2020-08-29 11:46:27 +00:00
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Dapper;
namespace PluralKit.Core
{
public partial class ModelRepository
{
public Task<PKSystem?> GetSystem(IPKConnection conn, SystemId id) =>
2021-08-27 15:03:47 +00:00
conn.QueryFirstOrDefaultAsync<PKSystem?>("select * from systems where id = @id", new { id });
2020-08-29 11:46:27 +00:00
public Task<PKSystem?> GetSystemByAccount(IPKConnection conn, ulong accountId) =>
conn.QuerySingleOrDefaultAsync<PKSystem?>(
"select systems.* from systems, accounts where accounts.system = systems.id and accounts.uid = @Id",
2021-08-27 15:03:47 +00:00
new { Id = accountId });
2020-08-29 11:46:27 +00:00
public Task<PKSystem?> GetSystemByHid(IPKConnection conn, string hid) =>
conn.QuerySingleOrDefaultAsync<PKSystem?>("select * from systems where systems.hid = @Hid",
2021-08-27 15:03:47 +00:00
new { Hid = hid.ToLower() });
2020-08-29 11:46:27 +00:00
public Task<IEnumerable<ulong>> GetSystemAccounts(IPKConnection conn, SystemId system) =>
2021-08-27 15:03:47 +00:00
conn.QueryAsync<ulong>("select uid from accounts where system = @Id", new { Id = system });
2020-08-29 11:46:27 +00:00
public IAsyncEnumerable<PKMember> GetSystemMembers(IPKConnection conn, SystemId system) =>
2021-08-27 15:03:47 +00:00
conn.QueryStreamAsync<PKMember>("select * from members where system = @SystemID", new { SystemID = system });
2020-08-29 11:46:27 +00:00
public Task<int> GetSystemMemberCount(IPKConnection conn, SystemId id, PrivacyLevel? privacyFilter = null)
{
var query = new StringBuilder("select count(*) from members where system = @Id");
if (privacyFilter != null)
2021-08-27 15:03:47 +00:00
query.Append($" and member_visibility = {(int)privacyFilter.Value}");
return conn.QuerySingleAsync<int>(query.ToString(), new { Id = id });
2020-08-29 11:46:27 +00:00
}
public async Task<PKSystem> CreateSystem(IPKConnection conn, string? systemName = null, IPKTransaction? tx = null)
2020-08-29 11:46:27 +00:00
{
var system = await conn.QuerySingleAsync<PKSystem>(
"insert into systems (hid, name) values (find_free_system_hid(), @Name) returning *",
2021-08-27 15:03:47 +00:00
new { Name = systemName },
transaction: tx);
2020-08-29 11:46:27 +00:00
_logger.Information("Created {SystemId}", system.Id);
return system;
}
public Task<PKSystem> UpdateSystem(IPKConnection conn, SystemId id, SystemPatch patch, IPKTransaction? tx = null)
2020-08-29 11:46:27 +00:00
{
_logger.Information("Updated {SystemId}: {@SystemPatch}", id, patch);
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("systems", "id = @id"))
.WithConstant("id", id)
.Build("returning *");
return conn.QueryFirstAsync<PKSystem>(query, pms, transaction: tx);
2020-08-29 11:46:27 +00:00
}
public async Task AddAccount(IPKConnection conn, SystemId system, ulong accountId)
{
// We have "on conflict do nothing" since linking an account when it's already linked to the same system is idempotent
// This is used in import/export, although the pk;link command checks for this case beforehand
await conn.ExecuteAsync("insert into accounts (uid, system) values (@Id, @SystemId) on conflict do nothing",
2021-08-27 15:03:47 +00:00
new { Id = accountId, SystemId = system });
2020-08-29 11:46:27 +00:00
_logger.Information("Linked account {UserId} to {SystemId}", accountId, system);
}
public async Task RemoveAccount(IPKConnection conn, SystemId system, ulong accountId)
{
await conn.ExecuteAsync("delete from accounts where uid = @Id and system = @SystemId",
2021-08-27 15:03:47 +00:00
new { Id = accountId, SystemId = system });
2020-08-29 11:46:27 +00:00
_logger.Information("Unlinked account {UserId} from {SystemId}", accountId, system);
}
public Task DeleteSystem(IPKConnection conn, SystemId id)
{
_logger.Information("Deleted {SystemId}", id);
2021-08-27 15:03:47 +00:00
return conn.ExecuteAsync("delete from systems where id = @Id", new { Id = id });
2020-08-29 11:46:27 +00:00
}
}
}