Move hid generation to the database. Closes #157.

This commit is contained in:
Ske
2020-06-13 00:43:48 +02:00
parent c39c51426f
commit 8ac2f1e4b8
5 changed files with 36 additions and 43 deletions

View File

@@ -58,15 +58,9 @@ namespace PluralKit.Core {
}
public async Task<PKSystem> CreateSystem(string systemName = null) {
string hid;
do
{
hid = StringUtils.GenerateHid();
} while (await GetSystemByHid(hid) != null);
PKSystem system;
using (var conn = await _conn.Obtain())
system = await conn.QuerySingleAsync<PKSystem>("insert into systems (hid, name) values (@Hid, @Name) returning *", new { Hid = hid, Name = systemName });
system = await conn.QuerySingleAsync<PKSystem>("insert into systems (hid, name) values (find_free_system_hid(), @Name) returning *", new { Name = systemName });
_logger.Information("Created system {System}", system.Id);
// New system has no accounts, therefore nothing gets cached, therefore no need to invalidate caches right here
@@ -147,16 +141,9 @@ namespace PluralKit.Core {
}
public async Task<PKMember> CreateMember(PKSystem system, string name) {
string hid;
do
{
hid = StringUtils.GenerateHid();
} while (await GetMemberByHid(hid) != null);
PKMember member;
using (var conn = await _conn.Obtain())
member = await conn.QuerySingleAsync<PKMember>("insert into members (hid, system, name) values (@Hid, @SystemId, @Name) returning *", new {
Hid = hid,
member = await conn.QuerySingleAsync<PKMember>("insert into members (hid, system, name) values (find_free_member_hid(), @SystemId, @Name) returning *", new {
SystemID = system.Id,
Name = name
});