2021-08-27 15:03:47 +00:00
#nullable enable
2021-09-13 07:14:59 +00:00
using System ;
2021-04-21 21:57:19 +00:00
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 } ) ;
2021-09-13 07:14:59 +00:00
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 } ) ;
2021-04-21 21:57:19 +00:00
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 ;
}
2021-04-21 21:57:19 +00:00
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 *" ) ;
2021-04-21 21:57:19 +00:00
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
}
}
}