Add super basic group model/command

This commit is contained in:
Ske
2020-06-29 23:51:12 +02:00
parent 0fadc81cda
commit 253ae43c7f
15 changed files with 199 additions and 2 deletions

View File

@@ -0,0 +1,26 @@
namespace PluralKit.Core
{
public readonly struct GroupId: INumericId<GroupId, int>
{
public int Value { get; }
public GroupId(int value)
{
Value = value;
}
public bool Equals(GroupId other) => Value == other.Value;
public override bool Equals(object obj) => obj is GroupId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(GroupId left, GroupId right) => left.Equals(right);
public static bool operator !=(GroupId left, GroupId right) => !left.Equals(right);
public int CompareTo(GroupId other) => Value.CompareTo(other.Value);
public override string ToString() => $"Member #{Value}";
}
}

View File

@@ -29,6 +29,12 @@ namespace PluralKit.Core
public static Task<PKMember?> QueryMemberByHid(this IPKConnection conn, string hid) =>
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where hid = @hid", new {hid = hid.ToLowerInvariant()});
public static Task<PKGroup?> QueryGroupByName(this IPKConnection conn, string name) =>
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where lower(name) = lower(@name)", new {name = name});
public static Task<PKGroup?> QueryGroupByHid(this IPKConnection conn, string hid) =>
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where hid = @hid", new {hid = hid.ToLowerInvariant()});
public static Task<GuildConfig> QueryOrInsertGuildConfig(this IPKConnection conn, ulong guild) =>
conn.QueryFirstAsync<GuildConfig>("insert into servers (id) values (@guild) on conflict (id) do update set id = @guild returning *", new {guild});

View File

@@ -0,0 +1,17 @@
using NodaTime;
#nullable enable
namespace PluralKit.Core
{
public class PKGroup
{
public GroupId Id { get; }
public string Hid { get; } = null!;
public SystemId System { get; }
public string Name { get; } = null!;
public string? Description { get; }
public Instant Created { get; }
}
}

View File

@@ -0,0 +1,13 @@
#nullable enable
namespace PluralKit.Core
{
public class GroupPatch: PatchObject
{
public Partial<string> Name { get; set; }
public Partial<string?> Description { get; set; }
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
.With("name", Name)
.With("description", Description);
}
}

View File

@@ -60,5 +60,10 @@ namespace PluralKit.Core
.Build();
return conn.ExecuteAsync(query, pms);
}
public static Task<PKGroup> CreateGroup(this IPKConnection conn, SystemId system, string name) =>
conn.QueryFirstAsync<PKGroup>(
"insert into groups (hid, system, name) values (find_free_group_hid(), @System, @Name) returning *",
new {System = system, Name = name});
}
}