fix: dapper doesn't play nice with List<T>
This commit is contained in:
parent
64b4c26d93
commit
da4dfae27c
4 changed files with 31 additions and 6 deletions
|
|
@ -14,6 +14,9 @@
|
|||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Data;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Catalogger.Backend.Database.Models;
|
||||
using Dapper;
|
||||
using NodaTime;
|
||||
using Npgsql;
|
||||
|
|
@ -113,6 +116,7 @@ public class DatabasePool
|
|||
SqlMapper.AddTypeHandler(new UlongArrayHandler());
|
||||
|
||||
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<Instant>());
|
||||
SqlMapper.AddTypeHandler(new JsonTypeHandler<Guild.ChannelConfig>());
|
||||
}
|
||||
|
||||
// Copied from PluralKit:
|
||||
|
|
@ -144,6 +148,21 @@ public class DatabasePool
|
|||
public override ulong[] Parse(object value) =>
|
||||
Array.ConvertAll((long[])value, i => (ulong)i);
|
||||
}
|
||||
|
||||
public class JsonTypeHandler<T> : SqlMapper.TypeHandler<T>
|
||||
{
|
||||
public override T Parse(object value)
|
||||
{
|
||||
string json = (string)value;
|
||||
return JsonSerializer.Deserialize<T>(json)
|
||||
?? throw new CataloggerError("JsonTypeHandler<T> returned null");
|
||||
}
|
||||
|
||||
public override void SetValue(IDbDataParameter parameter, T? value)
|
||||
{
|
||||
parameter.Value = JsonSerializer.Serialize(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ServiceCollectionDatabaseExtensions
|
||||
|
|
|
|||
|
|
@ -74,12 +74,18 @@ public class DatabaseContext : DbContext
|
|||
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode()))
|
||||
);
|
||||
|
||||
private static readonly ValueComparer<ulong[]> UlongArrayValueComparer =
|
||||
new(
|
||||
(c1, c2) => c1 != null && c2 != null && c1.SequenceEqual(c2),
|
||||
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode()))
|
||||
);
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder
|
||||
.Entity<Guild>()
|
||||
.Property(g => g.KeyRoles)
|
||||
.Metadata.SetValueComparer(UlongListValueComparer);
|
||||
.Metadata.SetValueComparer(UlongArrayValueComparer);
|
||||
|
||||
modelBuilder.Entity<Invite>().HasKey(i => i.Code);
|
||||
modelBuilder.Entity<Invite>().HasIndex(i => i.GuildId);
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ public class Guild
|
|||
|
||||
[Column(TypeName = "jsonb")]
|
||||
public ChannelConfig Channels { get; init; } = new();
|
||||
public List<string> BannedSystems { get; init; } = [];
|
||||
public List<ulong> KeyRoles { get; init; } = [];
|
||||
public string[] BannedSystems { get; set; } = [];
|
||||
public ulong[] KeyRoles { get; set; } = [];
|
||||
|
||||
public bool IsSystemBanned(PluralkitApiService.PkSystem system) =>
|
||||
BannedSystems.Contains(system.Id) || BannedSystems.Contains(system.Uuid.ToString());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue