aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-10-13 13:54:17 -0500
committerJP Dillingham <jp@dillingham.me>2024-10-13 13:54:17 -0500
commitf31e306c4d4a6abca109b4d619a450b9efb0ad0e (patch)
tree75e17ffd6a7c8d8eedbb56b408ddc3b7b99826ca /src
parentbac18981efba4daa4e4668dd382b7fbed8765636 (diff)
downloadslskd-f31e306c4d4a6abca109b4d619a450b9efb0ad0e.tar.gz
add an EventType enum
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Events/EventsDbContext.cs5
-rw-r--r--src/slskd/Events/Types/EventRecord.cs9
-rw-r--r--src/slskd/Events/Types/Events.cs11
3 files changed, 18 insertions, 7 deletions
diff --git a/src/slskd/Events/EventsDbContext.cs b/src/slskd/Events/EventsDbContext.cs
index 9acbc9a4..a86f5d1c 100644
--- a/src/slskd/Events/EventsDbContext.cs
+++ b/src/slskd/Events/EventsDbContext.cs
@@ -34,6 +34,11 @@ public class EventsDbContext : DbContext
{
modelBuilder
.Entity<EventRecord>()
+ .Property(e => e.Type)
+ .HasConversion<string>();
+
+ modelBuilder
+ .Entity<EventRecord>()
.Property(e => e.Timestamp)
.HasConversion(v => v, v => DateTime.SpecifyKind(v, DateTimeKind.Utc));
}
diff --git a/src/slskd/Events/Types/EventRecord.cs b/src/slskd/Events/Types/EventRecord.cs
index 11825c09..4323a42b 100644
--- a/src/slskd/Events/Types/EventRecord.cs
+++ b/src/slskd/Events/Types/EventRecord.cs
@@ -19,7 +19,6 @@ namespace slskd.Events;
using System;
using System.ComponentModel.DataAnnotations;
-using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
@@ -31,7 +30,7 @@ using Microsoft.EntityFrameworkCore;
public record EventRecord
{
public DateTime Timestamp { get; init; } = DateTime.UtcNow;
- public string Type { get; private set; }
+ public EventType Type { get; private set; }
public string Data { get; init; }
[Key]
public Guid Id { get; private set; }
@@ -61,10 +60,6 @@ public record EventRecord
public static EventRecord From<T>(Event e)
where T : Event
{
- // this will be 'slskd.NameOfEvent'; we want to chop off 'slskd.' and 'Event' = 'NameOf'
- var type = e.GetType().Name.Split('.').TakeLast(1).First();
- type = type.Substring(0, type.Length - "Event".Length); // chop off "Event" suffix
-
// construct the data for the record by serializing the event and removing redundant properties
var json = JsonSerializer.Serialize(e as T, JsonSerializerOptions);
var data = JsonNode.Parse(json).AsObject();
@@ -76,7 +71,7 @@ public record EventRecord
return new EventRecord
{
Timestamp = e.Timestamp,
- Type = type,
+ Type = e.Type,
Id = e.Id,
Data = data.ToJsonString(JsonSerializerOptions),
};
diff --git a/src/slskd/Events/Types/Events.cs b/src/slskd/Events/Types/Events.cs
index 20ec3f9f..b7e5e5fe 100644
--- a/src/slskd/Events/Types/Events.cs
+++ b/src/slskd/Events/Types/Events.cs
@@ -24,10 +24,20 @@ public abstract record Event
{
public Guid Id { get; } = Guid.NewGuid();
public DateTime Timestamp { get; init; } = DateTime.UtcNow;
+ public abstract EventType Type { get; }
+}
+
+public enum EventType
+{
+ None = 0,
+ All = 1,
+ DownloadFileComplete = 2,
+ DownloadDirectoryComplete = 3,
}
public sealed record DownloadFileCompleteEvent : Event
{
+ public override EventType Type => EventType.DownloadFileComplete;
public string LocalFilename { get; init; }
public string RemoteFilename { get; init; }
public Transfer Transfer { get; init; }
@@ -35,6 +45,7 @@ public sealed record DownloadFileCompleteEvent : Event
public sealed record DownloadDirectoryCompleteEvent : Event
{
+ public override EventType Type => EventType.DownloadDirectoryComplete;
public string LocalDirectoryName { get; init; }
public string RemoteDirectoryName { get; init; }
public string Username { get; init; }