Migrate to type-safe model ID structs

This commit is contained in:
Ske
2020-06-14 21:37:04 +02:00
parent e5ac5edc35
commit b9cbd241de
21 changed files with 167 additions and 41 deletions

View File

@@ -45,7 +45,7 @@ namespace PluralKit.Core {
public struct SwitchMembersListEntry
{
public int Member;
public MemberId Member;
public Instant Timestamp;
}
@@ -131,7 +131,7 @@ namespace PluralKit.Core {
/// Gets a system by its internal member ID.
/// </summary>
/// <returns>The <see cref="PKMember"/> with the given internal ID, or null if no member was found.</returns>
Task<PKMember> GetMemberById(int memberId);
Task<PKMember> GetMemberById(MemberId memberId);
/// <summary>
/// Gets a member by its user-facing human ID.
@@ -195,7 +195,7 @@ namespace PluralKit.Core {
/// <param name="triggerMessageId">The ID of the original trigger message containing the proxy tags.</param>
/// <param name="proxiedMemberId">The member (and by extension system) that was proxied.</param>
/// <returns></returns>
Task AddMessage(IPKConnection conn, ulong senderAccount, ulong guildId, ulong channelId, ulong postedMessageId, ulong triggerMessageId, int proxiedMemberId);
Task AddMessage(IPKConnection conn, ulong senderAccount, ulong guildId, ulong channelId, ulong postedMessageId, ulong triggerMessageId, MemberId proxiedMemberId);
/// <summary>
/// Deletes a message from the data store.

View File

@@ -125,7 +125,7 @@ namespace PluralKit.Core {
return member;
}
public async Task<PKMember> GetMemberById(int id) {
public async Task<PKMember> GetMemberById(MemberId id) {
using (var conn = await _conn.Obtain())
return await conn.QuerySingleOrDefaultAsync<PKMember>("select * from members where id = @Id", new { Id = id });
}
@@ -177,7 +177,7 @@ namespace PluralKit.Core {
return await conn.ExecuteScalarAsync<ulong>("select count(id) from members");
}
public async Task AddMessage(IPKConnection conn, ulong senderId, ulong guildId, ulong channelId, ulong postedMessageId, ulong triggerMessageId, int proxiedMemberId) {
public async Task AddMessage(IPKConnection conn, ulong senderId, ulong guildId, ulong channelId, ulong postedMessageId, ulong triggerMessageId, MemberId proxiedMemberId) {
// "on conflict do nothing" in the (pretty rare) case of duplicate events coming in from Discord, which would lead to a DB error before
await conn.ExecuteAsync("insert into messages(mid, guild, channel, member, sender, original_mid) values(@MessageId, @GuildId, @ChannelId, @MemberId, @SenderId, @OriginalMid) on conflict do nothing", new {
MessageId = postedMessageId,
@@ -334,7 +334,7 @@ namespace PluralKit.Core {
// query DB for all members involved in any of the switches above and collect into a dictionary for future use
// this makes sure the return list has the same instances of PKMember throughout, which is important for the dictionary
// key used in GetPerMemberSwitchDuration below
Dictionary<int, PKMember> memberObjects;
Dictionary<MemberId, PKMember> memberObjects;
using (var conn = await _conn.Obtain())
{
memberObjects = (
@@ -351,7 +351,7 @@ namespace PluralKit.Core {
select new SwitchListEntry
{
TimespanStart = g.Key,
Members = g.Where(x => x.Member != 0).Select(x => memberObjects[x.Member]).ToList()
Members = g.Where(x => x.Member != default(MemberId)).Select(x => memberObjects[x.Member]).ToList()
};
// Loop through every switch that overlaps the range and add it to the output list