Refactor member updates to use a patch object
This commit is contained in:
@@ -3,6 +3,8 @@ using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
|
||||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public static class ModelQueryExt
|
||||
@@ -26,5 +28,16 @@ namespace PluralKit.Core
|
||||
conn.QueryFirstAsync<MemberGuildSettings>(
|
||||
"insert into member_guild (guild, member) values (@guild, @member) on conflict (guild, member) do update set guild = @guild, member = @member returning *",
|
||||
new {guild, member});
|
||||
|
||||
public static Task<PKMember> UpdateMember(this IPKConnection conn, MemberId id, MemberPatch patch)
|
||||
{
|
||||
var (query, pms) = patch.Apply(new UpdateQueryBuilder("members", "id = @id"))
|
||||
.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});
|
||||
}
|
||||
}
|
44
PluralKit.Core/Models/Patch/MemberPatch.cs
Normal file
44
PluralKit.Core/Models/Patch/MemberPatch.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
#nullable enable
|
||||
|
||||
using NodaTime;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public class MemberPatch: PatchObject<MemberId, PKMember>
|
||||
{
|
||||
public Partial<string> Name { get; set; }
|
||||
public Partial<string?> DisplayName { get; set; }
|
||||
public Partial<string?> Color { get; set; }
|
||||
public Partial<LocalDate?> Birthday { get; set; }
|
||||
public Partial<string?> Pronouns { get; set; }
|
||||
public Partial<string?> Description { get; set; }
|
||||
public Partial<ProxyTag[]> ProxyTags { get; set; }
|
||||
public Partial<bool> KeepProxy { get; set; }
|
||||
public Partial<int> MessageCount { get; set; }
|
||||
public Partial<PrivacyLevel> Visibility { get; set; }
|
||||
public Partial<PrivacyLevel> NamePrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> PronounPrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> BirthdayPrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> AvatarPrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> MetadataPrivacy { get; set; }
|
||||
|
||||
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
||||
.With("name", Name)
|
||||
.With("display_name", DisplayName)
|
||||
.With("color", Color)
|
||||
.With("birthday", Birthday)
|
||||
.With("pronouns", Pronouns)
|
||||
.With("description", Description)
|
||||
.With("proxy_tags", ProxyTags)
|
||||
.With("keep_proxy", KeepProxy)
|
||||
.With("message_count", MessageCount)
|
||||
.With("member_visibility", Visibility)
|
||||
.With("name_privacy", NamePrivacy)
|
||||
.With("description_privacy", DescriptionPrivacy)
|
||||
.With("pronoun_privacy", PronounPrivacy)
|
||||
.With("birthday_privacy", BirthdayPrivacy)
|
||||
.With("avatar_privacy", AvatarPrivacy)
|
||||
.With("metadata_privacy", MetadataPrivacy);
|
||||
}
|
||||
}
|
9
PluralKit.Core/Models/Patch/PatchObject.cs
Normal file
9
PluralKit.Core/Models/Patch/PatchObject.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public abstract class PatchObject<TKey, TObj>
|
||||
{
|
||||
public abstract UpdateQueryBuilder Apply(UpdateQueryBuilder b);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user