From 235ab23ed49f96ed00a15a8b42305daf23990b83 Mon Sep 17 00:00:00 2001 From: KoCoder Date: Thu, 19 Feb 2026 16:37:45 +0100 Subject: [PATCH] Reporting: Add UserName --- Client/Components/ReportComponent.razor | 121 +++++------------- .../Edit.razor | 6 + .../Index.razor | 23 +++- .../Index.razor | 40 ++++-- ...enverein.Module.AdminModules.Client.csproj | 1 + .../Services/ReportSystemReportingService.cs | 2 +- .../ReportSystem/01000001_AddUserName.cs | 30 +++++ .../Services/ReportSystemReportingService.cs | 2 +- Shared/Models/AdminModules.cs | 11 +- Shared/Models/ReportSystem/Reporting.cs | 1 + ...enverein.Module.AdminModules.Shared.csproj | 1 + 11 files changed, 137 insertions(+), 101 deletions(-) create mode 100644 Server/Migrations/ReportSystem/01000001_AddUserName.cs diff --git a/Client/Components/ReportComponent.razor b/Client/Components/ReportComponent.razor index 830e84e..657d12e 100644 --- a/Client/Components/ReportComponent.razor +++ b/Client/Components/ReportComponent.razor @@ -1,9 +1,9 @@ -@inherits ComponentBase +@inherits LocalizableComponent @using Interfaces @implements Interfaces.IReportUI @inject IReportingHandler ReportingHandler - @@ -29,63 +29,6 @@ } - - @code { public Type ReportType => typeof(ReportComponent); @@ -95,23 +38,11 @@ private bool _showReportModal = false; private string _reportReason = ""; - private class ReportEntityTest : IReportable - { - public int EntityID { get; set; } - public string ModuleName { get; set; } - public int ModuleID { get; set; } - } - - protected override Task OnInitializedAsync() - { - ReportableEntity = new ReportEntityTest { EntityID = 1, ModuleID = 2, ModuleName = "TestModule" }; - return base.OnInitializedAsync(); - } - - private void ShowReportModal() + private Task ShowReportModal() { _reportReason = ""; _showReportModal = true; + return Task.CompletedTask; } private void CloseReportModal() @@ -119,21 +50,39 @@ _showReportModal = false; } - private void ReportEntry() + private async Task ReportEntry() { - ReportingHandler.Report(ReportableEntity, _reportReason); - Console.WriteLine($"Eintrag gemeldet mit Grund: {_reportReason}"); - CloseReportModal(); - } - - public override Task SetParametersAsync(ParameterView parameters) - { - Console.WriteLine("ParameterView received in ReportComponent:"); - foreach (var parameter in parameters) + // Basic null checks to avoid runtime NREs + if (ReportingHandler == null) { - Console.WriteLine(parameter.Name + "name - value" + parameter.Value); + Console.WriteLine("ReportingHandler is not available (null). Ensure it is registered and injected."); + CloseReportModal(); + return; } - return base.SetParametersAsync(parameters); - } + if (ReportableEntity == null) + { + Console.WriteLine("ReportableEntity is null. Cannot report."); + CloseReportModal(); + return; + } + + try + { + // If the handler exposes an async API use it instead. We run the synchronous call off the UI thread to avoid blocking. + await Task.Run(() => ReportingHandler.Report(ReportableEntity, _reportReason)); + Console.WriteLine($"Eintrag gemeldet mit Grund: {_reportReason}"); + AddModuleMessage($"Eintrag mit Grund {_reportReason} gemeldet.", MessageType.Success); + } + catch (Exception ex) + { + Console.WriteLine($"Reporting failed: {ex.Message}"); + AddModuleMessage($"Eintrag konnte nicht gemeldet werden, bitte melden sie sich direkt beim Absolventenverein: ({ex.StackTrace}).", MessageType.Error); + } + finally + { + CloseReportModal(); + StateHasChanged(); + } + } } \ No newline at end of file diff --git a/Client/Modules/SZUAbsolventenverein.Module.AdminModules/Edit.razor b/Client/Modules/SZUAbsolventenverein.Module.AdminModules/Edit.razor index b275a06..760750f 100644 --- a/Client/Modules/SZUAbsolventenverein.Module.AdminModules/Edit.razor +++ b/Client/Modules/SZUAbsolventenverein.Module.AdminModules/Edit.razor @@ -1,4 +1,5 @@ @using Oqtane.Modules.Controls +@using SZUAbsolventenverein.Module.AdminModules.Client.Components @using SZUAbsolventenverein.Module.AdminModules.Services @using SZUAbsolventenverein.Module.AdminModules.Models @@ -24,6 +25,8 @@ @Localizer["Cancel"]

+ +

@if (PageState.Action == "Edit") { @@ -54,6 +57,8 @@ private string _modifiedby; private DateTime _modifiedon; + private AdminModules AdminModules; + protected override async Task OnInitializedAsync() { try @@ -70,6 +75,7 @@ _createdon = AdminModules.CreatedOn; _modifiedby = AdminModules.ModifiedBy; _modifiedon = AdminModules.ModifiedOn; + this.AdminModules = AdminModules; } } } diff --git a/Client/Modules/SZUAbsolventenverein.Module.AdminModules/Index.razor b/Client/Modules/SZUAbsolventenverein.Module.AdminModules/Index.razor index 92f39d0..53b21ac 100644 --- a/Client/Modules/SZUAbsolventenverein.Module.AdminModules/Index.razor +++ b/Client/Modules/SZUAbsolventenverein.Module.AdminModules/Index.razor @@ -1,3 +1,4 @@ +@using Interfaces @using SZUAbsolventenverein.Module.AdminModules.Services @using SZUAbsolventenverein.Module.AdminModules.Models @@ -6,6 +7,7 @@ @inject IAdminModulesService AdminModulesService @inject NavigationManager NavigationManager @inject IStringLocalizer Localizer +@inject IReportingHandler ReportingHandler; @if (_AdminModuless == null) { @@ -23,11 +25,13 @@ else       +   @Localizer["Name"] - + + @context.Name @@ -40,7 +44,7 @@ else } @code { - public override string RenderMode => RenderModes.Static; + public override string RenderMode => RenderModes.Interactive; public override List Resources => new List() { @@ -78,4 +82,19 @@ else AddModuleMessage(Localizer["Message.DeleteError"], MessageType.Error); } } + + private async Task Report(AdminModules AdminModules) + { + try + { + ReportingHandler.Report(AdminModules, "Reported by User"); + await logger.LogInformation("AdminModules Reported {AdminModules}", AdminModules); + StateHasChanged(); + } + catch (Exception ex) + { + await logger.LogError(ex, "Error Reportign AdminModules {AdminModules} {Error}", AdminModules, ex.Message); + AddModuleMessage(Localizer["Message.DeleteError"], MessageType.Error); + } + } } \ No newline at end of file diff --git a/Client/Modules/SZUAbsolventenverein.Module.ReportSystem/Index.razor b/Client/Modules/SZUAbsolventenverein.Module.ReportSystem/Index.razor index 1b3ca78..bccba5e 100644 --- a/Client/Modules/SZUAbsolventenverein.Module.ReportSystem/Index.razor +++ b/Client/Modules/SZUAbsolventenverein.Module.ReportSystem/Index.razor @@ -7,8 +7,9 @@ @inject NavigationManager NavigationManager @inject IReportSystemReportingService ReportingService @inject IStringLocalizer Localizer +@inject IModuleService ModuleService -@if (reportings == null) +@if (_reportings == null) {

Loading...

} @@ -16,19 +17,24 @@ else {

- @if (reportings.Count != 0) + @if (_reportings.Count != 0) { - +
    -   + @Localizer["Submitter"] + @Localizer["Note"] @Localizer["Name"]
- + @context.CreatedBy + @context.CreatedOn + @context.ModuleId + @context.ReportingID + @context.Note @context.Reason
@@ -39,7 +45,6 @@ else } } - @code { public override string RenderMode => RenderModes.Interactive; @@ -49,13 +54,28 @@ else new Script("_content/SZUAbsolventenverein.Module.ReportSystem/Module.js") }; - private List reportings = new List(); - + private List _reportings = new List(); + private Dictionary _modules = new Dictionary(); + protected override async Task OnInitializedAsync() { try { - reportings = await ReportingService.GetReportsAsync(ModuleState.ModuleId); + _reportings = await ReportingService.GetReportsAsync(ModuleState.ModuleId); + foreach (var moduleId in _reportings.Select(r => r.ModuleId).Distinct()) + { + Console.WriteLine(moduleId); + try + { + _modules.Add(moduleId, await ModuleService.GetModuleAsync(moduleId)); + await logger.LogDebug(LogFunction.Create, "Module found {ModuleId} while loading Modules for Reportings.", moduleId); + } + catch (Exception ex) + { + _modules.Add(moduleId, new Module {Title = $"Module not found {ex.Message}"}); + await logger.LogDebug("Module not found {ModuleId} while loading Modules for Reportings. {error}", moduleId, ex); + } + } } catch (Exception ex) { @@ -68,7 +88,7 @@ else try { await ReportingService.DeleteReportingAsync(reporting.ReportingID, ModuleState.ModuleId); - reportings.Remove(reporting); + _reportings.Remove(reporting); StateHasChanged(); } catch (Exception ex) diff --git a/Client/SZUAbsolventenverein.Module.AdminModules.Client.csproj b/Client/SZUAbsolventenverein.Module.AdminModules.Client.csproj index b44f0ef..8c65c5e 100644 --- a/Client/SZUAbsolventenverein.Module.AdminModules.Client.csproj +++ b/Client/SZUAbsolventenverein.Module.AdminModules.Client.csproj @@ -13,6 +13,7 @@ + diff --git a/Client/Services/ReportSystemReportingService.cs b/Client/Services/ReportSystemReportingService.cs index 340fc5a..2fcd279 100644 --- a/Client/Services/ReportSystemReportingService.cs +++ b/Client/Services/ReportSystemReportingService.cs @@ -19,7 +19,7 @@ namespace SZUAbsolventenverein.Module.ReportSystem.Services public void Report(IReportable reportable, string note) { CreateReportAsync(new Reporting - { ModuleId = reportable.ModuleID, EntityId = reportable.EntityID, Note = note, Reason = "Default Reason" }); + { ModuleId = reportable.ModuleID, EntityId = reportable.EntityID, Note = note, UserName = reportable.UserName, Reason = "Default Reason" }); } public Task CreateReportAsync(Reporting reporting) diff --git a/Server/Migrations/ReportSystem/01000001_AddUserName.cs b/Server/Migrations/ReportSystem/01000001_AddUserName.cs new file mode 100644 index 0000000..2950f63 --- /dev/null +++ b/Server/Migrations/ReportSystem/01000001_AddUserName.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Oqtane.Databases.Interfaces; +using Oqtane.Migrations; +using SZUAbsolventenverein.Module.AdminModules.Migrations.EntityBuilders; +using SZUAbsolventenverein.Module.AdminModules.Repository; + +namespace SZUAbsolventenverein.Module.ReportSystem.Migrations +{ + [DbContext(typeof(ReportingContext))] + [Migration("SZUAbsolventenverein.Module.ReportSystem.01.00.00.01")] + public class AddUserName : MultiDatabaseMigration + { + public AddUserName(IDatabase database) : base(database) + { + } + + protected override void Up(MigrationBuilder migrationBuilder) + { + var entityBuilder = new ReportingEntityBuilder(migrationBuilder, ActiveDatabase); + entityBuilder.AddStringColumn("UserName", 256, false, false, ""); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + var entityBuilder = new ReportingEntityBuilder(migrationBuilder, ActiveDatabase); + entityBuilder.DropColumn("UserName"); + } + } +} \ No newline at end of file diff --git a/Server/Services/ReportSystemReportingService.cs b/Server/Services/ReportSystemReportingService.cs index 16282c7..08466fa 100644 --- a/Server/Services/ReportSystemReportingService.cs +++ b/Server/Services/ReportSystemReportingService.cs @@ -120,7 +120,7 @@ namespace SZUAbsolventenverein.Module.ReportSystem.Services { // if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.Edit)) { - Reporting reporting = await CreateReportAsync(new Reporting {ModuleId = reportable.ModuleID, EntityId = reportable.EntityID, Note = note, Reason = "Default Reason"}); + Reporting reporting = await CreateReportAsync(new Reporting {ModuleId = reportable.ModuleID, EntityId = reportable.EntityID, UserName = reportable.UserName, Note = note, Reason = "Default Reason"}); if (reporting != null) { _logger.Log(LogLevel.Information, this, LogFunction.Delete, "Reporting recieved {ReportingId}", reporting.ReportingID); diff --git a/Shared/Models/AdminModules.cs b/Shared/Models/AdminModules.cs index 83500c9..bb8466b 100644 --- a/Shared/Models/AdminModules.cs +++ b/Shared/Models/AdminModules.cs @@ -1,12 +1,13 @@ using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using Interfaces; using Oqtane.Models; namespace SZUAbsolventenverein.Module.AdminModules.Models { [Table("SZUAbsolventenvereinAdminModules")] - public class AdminModules : IAuditable + public class AdminModules : IAuditable, IReportable { [Key] public int AdminModulesId { get; set; } @@ -18,5 +19,13 @@ namespace SZUAbsolventenverein.Module.AdminModules.Models public DateTime CreatedOn { get; set; } public string ModifiedBy { get; set; } public DateTime ModifiedOn { get; set; } + + [NotMapped] public string ModuleName => ""; + + [NotMapped] public int ModuleID => ModuleId; + + [NotMapped] public int EntityID => AdminModulesId; + + [NotMapped] public string UserName => CreatedBy; } } diff --git a/Shared/Models/ReportSystem/Reporting.cs b/Shared/Models/ReportSystem/Reporting.cs index 2db78bf..84fd44f 100644 --- a/Shared/Models/ReportSystem/Reporting.cs +++ b/Shared/Models/ReportSystem/Reporting.cs @@ -10,6 +10,7 @@ public class Reporting : IAuditable public int ReportingID { get; set; } public int ModuleId { get; set; } public int EntityId { get; set; } + public string UserName { get; set; } public string Note { get; set; } public string Reason { get; set; } diff --git a/Shared/SZUAbsolventenverein.Module.AdminModules.Shared.csproj b/Shared/SZUAbsolventenverein.Module.AdminModules.Shared.csproj index 9bf2f70..8e26581 100644 --- a/Shared/SZUAbsolventenverein.Module.AdminModules.Shared.csproj +++ b/Shared/SZUAbsolventenverein.Module.AdminModules.Shared.csproj @@ -12,6 +12,7 @@ +