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

55 lines
2.7 KiB
C#
Raw Normal View History

2021-08-27 15:03:47 +00:00
#nullable enable
using System;
using System.Data;
2020-08-29 11:46:27 +00:00
using System.Threading.Tasks;
using Dapper;
namespace PluralKit.Core
{
public partial class ModelRepository
{
public Task<PKMember?> GetMember(IPKConnection conn, MemberId id) =>
2021-08-27 15:03:47 +00:00
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new { id });
public Task<PKMember?> GetMemberByHid(IPKConnection conn, string hid, SystemId? system = null)
=> conn.QuerySingleOrDefaultAsync<PKMember?>(
"select * from members where hid = @Hid" + (system != null ? " and system = @System" : ""),
new { Hid = hid.ToLower(), System = system }
);
2020-08-29 11:46:27 +00:00
2021-09-27 03:18:17 +00:00
public Task<PKMember?> GetMemberByGuid(IPKConnection conn, Guid guid) =>
conn.QuerySingleOrDefaultAsync<PKMember?>("select * from members where uuid = @Uuid", new { Uuid = guid });
2021-08-27 15:03:47 +00:00
public Task<PKMember?> GetMemberByName(IPKConnection conn, SystemId system, string name) =>
2020-08-29 11:46:27 +00:00
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });
2021-08-27 15:03:47 +00:00
public Task<PKMember?> GetMemberByDisplayName(IPKConnection conn, SystemId system, string name) =>
2020-08-29 11:46:27 +00:00
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(display_name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });
public async Task<PKMember> CreateMember(IPKConnection conn, SystemId id, string memberName, IDbTransaction? transaction = null)
2020-08-29 11:46:27 +00:00
{
var member = await conn.QueryFirstAsync<PKMember>(
"insert into members (hid, system, name) values (find_free_member_hid(), @SystemId, @Name) returning *",
2021-08-27 15:03:47 +00:00
new { SystemId = id, Name = memberName }, transaction);
2020-08-29 11:46:27 +00:00
_logger.Information("Created {MemberId} in {SystemId}: {MemberName}",
member.Id, id, memberName);
return member;
}
public Task<PKMember> UpdateMember(IPKConnection conn, MemberId id, MemberPatch patch, IDbTransaction? transaction = null)
2020-08-29 11:46:27 +00:00
{
_logger.Information("Updated {MemberId}: {@MemberPatch}", id, patch);
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("members", "id = @id"))
.WithConstant("id", id)
.Build("returning *");
return conn.QueryFirstAsync<PKMember>(query, pms, transaction);
2020-08-29 11:46:27 +00:00
}
public Task DeleteMember(IPKConnection conn, MemberId id)
{
_logger.Information("Deleted {MemberId}", id);
2021-08-27 15:03:47 +00:00
return conn.ExecuteAsync("delete from members where id = @Id", new { Id = id });
2020-08-29 11:46:27 +00:00
}
}
}