Migrate guild objects to the patch system
This commit is contained in:
16
PluralKit.Core/Models/Patch/GuildPatch.cs
Normal file
16
PluralKit.Core/Models/Patch/GuildPatch.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public class GuildPatch: PatchObject
|
||||
{
|
||||
public Partial<ulong?> LogChannel { get; set; }
|
||||
public Partial<ulong[]> LogBlacklist { get; set; }
|
||||
public Partial<ulong[]> Blacklist { get; set; }
|
||||
public Partial<bool> LogCleanupEnabled { get; set; }
|
||||
|
||||
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
||||
.With("log_channel", LogChannel)
|
||||
.With("log_blacklist", LogBlacklist)
|
||||
.With("blacklist", Blacklist)
|
||||
.With("log_cleanup_enabled", LogCleanupEnabled);
|
||||
}
|
||||
}
|
13
PluralKit.Core/Models/Patch/MemberGuildPatch.cs
Normal file
13
PluralKit.Core/Models/Patch/MemberGuildPatch.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
#nullable enable
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public class MemberGuildPatch: PatchObject
|
||||
{
|
||||
public Partial<string?> DisplayName { get; set; }
|
||||
public Partial<string?> AvatarUrl { get; set; }
|
||||
|
||||
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
||||
.With("display_name", DisplayName)
|
||||
.With("avatar_url", AvatarUrl);
|
||||
}
|
||||
}
|
@@ -8,7 +8,7 @@ namespace PluralKit.Core
|
||||
{
|
||||
public static Task<PKSystem> UpdateSystem(this IPKConnection conn, SystemId id, SystemPatch patch)
|
||||
{
|
||||
var (query, pms) = patch.Apply(new UpdateQueryBuilder("systems", "id = @id"))
|
||||
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("systems", "id = @id"))
|
||||
.WithConstant("id", id)
|
||||
.Build("returning *");
|
||||
return conn.QueryFirstAsync<PKSystem>(query, pms);
|
||||
@@ -24,7 +24,7 @@ namespace PluralKit.Core
|
||||
|
||||
public static Task<PKMember> UpdateMember(this IPKConnection conn, MemberId id, MemberPatch patch)
|
||||
{
|
||||
var (query, pms) = patch.Apply(new UpdateQueryBuilder("members", "id = @id"))
|
||||
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("members", "id = @id"))
|
||||
.WithConstant("id", id)
|
||||
.Build("returning *");
|
||||
return conn.QueryFirstAsync<PKMember>(query, pms);
|
||||
@@ -32,5 +32,33 @@ namespace PluralKit.Core
|
||||
|
||||
public static Task DeleteMember(this IPKConnection conn, MemberId id) =>
|
||||
conn.ExecuteAsync("delete from members where id = @Id", new {Id = id});
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
15
PluralKit.Core/Models/Patch/SystemGuildPatch.cs
Normal file
15
PluralKit.Core/Models/Patch/SystemGuildPatch.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
#nullable enable
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public class SystemGuildPatch: PatchObject
|
||||
{
|
||||
public Partial<bool> ProxyEnabled { get; set; }
|
||||
public Partial<AutoproxyMode> AutoproxyMode { get; set; }
|
||||
public Partial<MemberId?> AutoproxyMember { get; set; }
|
||||
|
||||
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
||||
.With("proxy_enabled", ProxyEnabled)
|
||||
.With("autoproxy_mode", AutoproxyMode)
|
||||
.With("autoproxy_member", AutoproxyMember);
|
||||
}
|
||||
}
|
@@ -50,7 +50,7 @@ namespace PluralKit.Core
|
||||
else _updateFragment.Append(", ");
|
||||
|
||||
_updateFragment.Append(fieldName);
|
||||
_updateFragment.Append("=");
|
||||
_updateFragment.Append(" = ");
|
||||
_updateFragment.Append(paramName);
|
||||
return this;
|
||||
}
|
||||
@@ -71,7 +71,7 @@ namespace PluralKit.Core
|
||||
if (Type == QueryType.Update && _condition != null)
|
||||
query.Append($" where {_condition}");
|
||||
|
||||
if (suffix != null)
|
||||
if (!string.IsNullOrEmpty(suffix))
|
||||
query.Append($" {suffix}");
|
||||
query.Append(";");
|
||||
|
||||
|
@@ -6,34 +6,29 @@ namespace PluralKit.Core
|
||||
{
|
||||
public class UpdateQueryBuilder
|
||||
{
|
||||
private readonly string _table;
|
||||
private readonly string _condition;
|
||||
private readonly QueryBuilder _qb;
|
||||
private readonly DynamicParameters _params = new DynamicParameters();
|
||||
|
||||
private bool _hasFields = false;
|
||||
private readonly StringBuilder _setClause = new StringBuilder();
|
||||
|
||||
public UpdateQueryBuilder(string table, string condition)
|
||||
private UpdateQueryBuilder(QueryBuilder qb)
|
||||
{
|
||||
_table = table;
|
||||
_condition = condition;
|
||||
_qb = qb;
|
||||
}
|
||||
|
||||
public static UpdateQueryBuilder Insert(string table) => new UpdateQueryBuilder(QueryBuilder.Insert(table));
|
||||
public static UpdateQueryBuilder Update(string table, string condition) => new UpdateQueryBuilder(QueryBuilder.Update(table, condition));
|
||||
public static UpdateQueryBuilder Upsert(string table, string conflictField) => new UpdateQueryBuilder(QueryBuilder.Upsert(table, conflictField));
|
||||
|
||||
public UpdateQueryBuilder WithConstant<T>(string name, T value)
|
||||
{
|
||||
_params.Add(name, value);
|
||||
_qb.Constant(name, $"@{name}");
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateQueryBuilder With<T>(string columnName, T value)
|
||||
{
|
||||
_params.Add(columnName, value);
|
||||
|
||||
if (_hasFields)
|
||||
_setClause.Append(", ");
|
||||
else _hasFields = true;
|
||||
|
||||
_setClause.Append($"{columnName} = @{columnName}");
|
||||
_qb.Variable(columnName, $"@{columnName}");
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -42,10 +37,9 @@ namespace PluralKit.Core
|
||||
return partialValue.IsPresent ? With(columnName, partialValue.Value) : this;
|
||||
}
|
||||
|
||||
public (string Query, DynamicParameters Parameters) Build(string append = "")
|
||||
public (string Query, DynamicParameters Parameters) Build(string suffix = "")
|
||||
{
|
||||
var query = $"update {_table} set {_setClause} where {_condition} {append}";
|
||||
return (query, _params);
|
||||
return (_qb.Build(suffix), _params);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user