#nullable enable using System.Data; using System.Threading.Tasks; using Dapper; namespace PluralKit.Core { public partial class ModelRepository { public Task GetMember(IPKConnection conn, MemberId id) => conn.QueryFirstOrDefaultAsync("select * from members where id = @id", new {id}); public Task GetMemberByHid(IPKConnection conn, string hid) => conn.QuerySingleOrDefaultAsync("select * from members where hid = @Hid", new { Hid = hid.ToLower() }); public Task GetMemberByName(IPKConnection conn, SystemId system, string name) => conn.QueryFirstOrDefaultAsync("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system }); public Task GetMemberByDisplayName(IPKConnection conn, SystemId system, string name) => conn.QueryFirstOrDefaultAsync("select * from members where lower(display_name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system }); public async Task CreateMember(IPKConnection conn, SystemId id, string memberName, IDbTransaction? transaction = null) { var member = await conn.QueryFirstAsync( "insert into members (hid, system, name) values (find_free_member_hid(), @SystemId, @Name) returning *", new {SystemId = id, Name = memberName}, transaction); _logger.Information("Created {MemberId} in {SystemId}: {MemberName}", member.Id, id, memberName); return member; } public Task UpdateMember(IPKConnection conn, MemberId id, MemberPatch patch, IDbTransaction? transaction = null) { _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(query, pms, transaction); } public Task DeleteMember(IPKConnection conn, MemberId id) { _logger.Information("Deleted {MemberId}", id); return conn.ExecuteAsync("delete from members where id = @Id", new {Id = id}); } } }