Skip to content

Commit

Permalink
Added consumer log table
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianusIV committed Dec 30, 2023
1 parent 71abe03 commit cff6b0c
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 2 deletions.
1 change: 1 addition & 0 deletions Contracts/DbContext/IDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface IDbContext : IDisposable
public DbSet<Lease> Leases { get; }
public DbSet<User> Users { get; }
public DbSet<Role> Roles { get; }
public DbSet<Log> Logs { get; }
public int SaveChanges();
}
}
2 changes: 2 additions & 0 deletions DataAccess/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class DbContext : Microsoft.EntityFrameworkCore.DbContext, IDbContext
public DbSet<User> Users => Set<User>();
public DbSet<Role> Roles => Set<Role>();

public DbSet<Log> Logs => Set<Log>();

//ensure latest migration is applied to the current db
public DbContext()
=> Database.Migrate();
Expand Down
174 changes: 174 additions & 0 deletions DataAccess/Migrations/20231230180922_Add log table.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions DataAccess/Migrations/20231230180922_Add log table.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DataAccess.Migrations
{
/// <inheritdoc />
public partial class Addlogtable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Logs",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Timestamp = table.Column<DateTime>(type: "datetime(6)", nullable: false),
Content = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_Logs", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Logs");
}
}
}
20 changes: 19 additions & 1 deletion DataAccess/Migrations/DbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.2")
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 64);

modelBuilder.Entity("Models.Lease", b =>
Expand Down Expand Up @@ -75,6 +75,24 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("Leases");
});

modelBuilder.Entity("Models.Log", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Content")
.IsRequired()
.HasColumnType("longtext");
b.Property<DateTime>("Timestamp")
.HasColumnType("datetime(6)");
b.HasKey("Id");
b.ToTable("Logs");
});

modelBuilder.Entity("Models.Role", b =>
{
b.Property<int>("Id")
Expand Down
2 changes: 2 additions & 0 deletions DefaultPlugins/TwitchConsumer/TwitchConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TwitchConsumer : IConsumerPlugin
public string Name => "Default_TwitchConsumer";

public IPluginRepository? PluginRepository { get; set; }
public ILogRepository? LogRepository { get; set; }
public ILogger? Logger { get; set; }

public Response HandleGet(Lease lease, Request request)
Expand Down Expand Up @@ -57,6 +58,7 @@ public Response HandlePost(Lease lease, Request request)
switch (messageType)
{
case "notification":
LogRepository!.CreateLogEntry(new Log(DateTime.Now, request.Body));
var notifPayload = JsonSerializer.Deserialize<TwitchNotificationJson>(request.Body);
if (notifPayload is null)
return new Response() { ReturnStatus = HttpStatusCode.InternalServerError };
Expand Down
2 changes: 2 additions & 0 deletions DefaultPlugins/YouTubeConsumer/YouTubeConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class YouTubeConsumer : IConsumerPlugin
public string Name => "Default_YoutubeConsumer";
//DI
public IPluginRepository? PluginRepository { get; set; }
public ILogRepository? LogRepository { get; set; }
public ILogger? Logger { get; set; }

public Response HandleGet(Lease lease, Request request)
Expand Down Expand Up @@ -91,6 +92,7 @@ public Response HandlePost(Lease lease, Request request)
response.ReturnStatus = HttpStatusCode.BadRequest;
return response;
}
LogRepository!.CreateLogEntry(new Log(DateTime.Now, request.Body));

//transform plugin data saved in lease object as json
var data = lease.GetObjectFromConsumerString<DefaultYouTubeConsPluginData>();
Expand Down
15 changes: 15 additions & 0 deletions Models/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Models
{
public class Log
{
public int Id { get; set; }
public DateTime Timestamp { get; set; }
public string Content { get; set; }

public Log(DateTime timestamp, string content)
{
Timestamp = timestamp;
Content = content;
}
}
}
2 changes: 2 additions & 0 deletions PluginLibrary/Interfaces/IConsumerPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Models;
using Models.ApiCommunication;
using PluginLibrary.PluginRepositories;

namespace PluginLibrary.Interfaces
{
public interface IConsumerPlugin : IBasePlugin
{
public ILogRepository? LogRepository { get; set; }
public Task<bool> SubscribeAsync(Lease lease, bool subscribe = true);

public Response HandleGet(Lease lease, Request request);
Expand Down
9 changes: 9 additions & 0 deletions PluginLibrary/PluginRepositories/ILogRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Models;

namespace PluginLibrary.PluginRepositories
{
public interface ILogRepository
{
public void CreateLogEntry(Log log);
}
}
6 changes: 5 additions & 1 deletion PluginLoader/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ public class PluginManager : IPluginManager
private Dictionary<IPublisherPlugin, Type?> _publisherPlugins;
private readonly ILogger<PluginManager> _logger;
private readonly IPluginRepository _pluginRepository;
private readonly ILogRepository _logRepository;

//disable nullable warning - non nullable properties are set in ReloadPlugins method
#pragma warning disable CS8618
public PluginManager(ILogger<PluginManager> logger, IPluginRepository pluginRepository)
public PluginManager(ILogger<PluginManager> logger, IPluginRepository pluginRepository, ILogRepository logRepository)
{
_logger = logger;
_pluginRepository = pluginRepository;
//populate plugin dictionaries
ReloadPlugins();
_logRepository = logRepository;
}
#pragma warning restore CS8618

Expand Down Expand Up @@ -136,6 +138,8 @@ public void ReloadPlugins()
}
//DI into plugin
plugin.PluginRepository = _pluginRepository;
if (interfaceType == typeof(IConsumerPlugin))
((IConsumerPlugin)plugin).LogRepository = _logRepository;
plugin.Logger = _logger;
//asyncronously call init
_ = plugin.InitAsync();
Expand Down
1 change: 1 addition & 0 deletions PuSHReceiver/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
builder.Services.AddTransient<IDbContext, DbContext>();
builder.Services.AddTransient<ILeaseRepository, LeaseRepository>();
builder.Services.AddTransient<IPluginRepository, PluginRepository>();
builder.Services.AddTransient<ILogRepository, LogRepository>();
builder.Services.AddTransient<IUserRepository, UserRepository>();
builder.Services.AddSingleton<IPluginManager, PluginManager>();
builder.Services.AddSingleton<ILeaseService, LeaseService>();
Expand Down
Loading

0 comments on commit cff6b0c

Please sign in to comment.