2020-06-29 12:39:19 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using Dapper;
|
|
|
|
|
|
|
|
|
|
namespace PluralKit.Core
|
|
|
|
|
{
|
|
|
|
|
public static class ModelPatchExt
|
|
|
|
|
{
|
|
|
|
|
public static Task<PKSystem> UpdateSystem(this IPKConnection conn, SystemId id, SystemPatch patch)
|
|
|
|
|
{
|
2020-06-29 13:20:28 +00:00
|
|
|
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("systems", "id = @id"))
|
2020-06-29 12:39:19 +00:00
|
|
|
|
.WithConstant("id", id)
|
|
|
|
|
.Build("returning *");
|
|
|
|
|
return conn.QueryFirstAsync<PKSystem>(query, pms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Task DeleteSystem(this IPKConnection conn, SystemId id) =>
|
|
|
|
|
conn.ExecuteAsync("delete from systems where id = @Id", new {Id = id});
|
2020-06-29 12:54:11 +00:00
|
|
|
|
|
|
|
|
|
public static Task<PKMember> CreateMember(this IPKConnection conn, SystemId system, string memberName) =>
|
|
|
|
|
conn.QueryFirstAsync<PKMember>(
|
|
|
|
|
"insert into members (hid, system, name) values (find_free_member_hid(), @SystemId, @Name) returning *",
|
|
|
|
|
new {SystemId = system, Name = memberName});
|
|
|
|
|
|
2020-06-29 12:39:19 +00:00
|
|
|
|
public static Task<PKMember> UpdateMember(this IPKConnection conn, MemberId id, MemberPatch patch)
|
|
|
|
|
{
|
2020-06-29 13:20:28 +00:00
|
|
|
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("members", "id = @id"))
|
2020-06-29 12:39:19 +00:00
|
|
|
|
.WithConstant("id", id)
|
|
|
|
|
.Build("returning *");
|
|
|
|
|
return conn.QueryFirstAsync<PKMember>(query, pms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Task DeleteMember(this IPKConnection conn, MemberId id) =>
|
|
|
|
|
conn.ExecuteAsync("delete from members where id = @Id", new {Id = id});
|
2020-06-29 13:20:28 +00:00
|
|
|
|
|
|
|
|
|
public static Task UpsertSystemGuild(this IPKConnection conn, SystemId system, ulong guild,
|
|
|
|
|
SystemGuildPatch patch)
|
|
|
|
|
{
|
|
|
|
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Upsert("system_guild", "system, guild"))
|
|
|
|
|
.WithConstant("system", system)
|
|
|
|
|
.WithConstant("guild", guild)
|
|
|
|
|
.Build();
|
|
|
|
|
return conn.ExecuteAsync(query, pms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Task UpsertMemberGuild(this IPKConnection conn, MemberId member, ulong guild,
|
|
|
|
|
MemberGuildPatch patch)
|
|
|
|
|
{
|
|
|
|
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Upsert("member_guild", "member, guild"))
|
|
|
|
|
.WithConstant("member", member)
|
|
|
|
|
.WithConstant("guild", guild)
|
|
|
|
|
.Build();
|
|
|
|
|
return conn.ExecuteAsync(query, pms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Task UpsertGuild(this IPKConnection conn, ulong guild, GuildPatch patch)
|
|
|
|
|
{
|
|
|
|
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Upsert("servers", "id"))
|
|
|
|
|
.WithConstant("id", guild)
|
|
|
|
|
.Build();
|
|
|
|
|
return conn.ExecuteAsync(query, pms);
|
|
|
|
|
}
|
2020-06-29 12:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|