2021-08-27 15:03:47 +00:00
|
|
|
#nullable enable
|
2021-09-13 07:14:59 +00:00
|
|
|
using System;
|
2020-08-29 11:46:27 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
using SqlKata;
|
2020-08-29 11:46:27 +00:00
|
|
|
|
|
|
|
namespace PluralKit.Core
|
|
|
|
{
|
|
|
|
public partial class ModelRepository
|
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKMember?> GetMember(MemberId id)
|
|
|
|
{
|
|
|
|
var query = new Query("members").Where("id", id);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKMember?> GetMemberByHid(string hid, SystemId? system = null)
|
|
|
|
{
|
|
|
|
var query = new Query("members").Where("hid", hid.ToLower());
|
|
|
|
if (system != null)
|
|
|
|
query = query.Where("system", system);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKMember?> GetMemberByGuid(Guid uuid)
|
|
|
|
{
|
|
|
|
var query = new Query("members").Where("uuid", uuid);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2021-09-27 03:18:17 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKMember?> GetMemberByName(SystemId system, string name)
|
|
|
|
{
|
|
|
|
var query = new Query("members").WhereRaw(
|
|
|
|
"lower(name) = lower(?)",
|
|
|
|
name.ToLower()
|
|
|
|
).Where("system", system);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKMember?> GetMemberByDisplayName(SystemId system, string name)
|
|
|
|
{
|
|
|
|
var query = new Query("members").WhereRaw(
|
|
|
|
"lower(display_name) = lower(?)",
|
|
|
|
name.ToLower()
|
|
|
|
).Where("system", system);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public async Task<PKMember> CreateMember(SystemId systemId, string memberName, IPKConnection? conn = null)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("members").AsInsert(new
|
|
|
|
{
|
|
|
|
hid = new UnsafeLiteral("find_free_member_hid()"),
|
|
|
|
system = systemId,
|
|
|
|
name = memberName
|
|
|
|
});
|
|
|
|
var member = await _db.QueryFirst<PKMember>(conn, query, "returning *");
|
2020-08-29 11:46:27 +00:00
|
|
|
_logger.Information("Created {MemberId} in {SystemId}: {MemberName}",
|
2021-09-30 01:51:38 +00:00
|
|
|
member.Id, systemId, memberName);
|
2020-08-29 11:46:27 +00:00
|
|
|
return member;
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKMember> UpdateMember(MemberId id, MemberPatch patch, IPKConnection? conn = null)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
|
|
|
_logger.Information("Updated {MemberId}: {@MemberPatch}", id, patch);
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = patch.Apply(new Query("members").Where("id", id));
|
2021-10-03 16:02:26 +00:00
|
|
|
return _db.QueryFirst<PKMember>(conn, query, extraSql: "returning *");
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task DeleteMember(MemberId id)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
|
|
|
_logger.Information("Deleted {MemberId}", id);
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("members").AsDelete().Where("id", id);
|
|
|
|
return _db.ExecuteQuery(query);
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|