From 6ebd049c4dc36d33a2a41740eb8dc3cb430e26af Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 13 Sep 2025 03:35:18 +0100 Subject: [PATCH] Add edit functionality for device statistics with form validation --- .../Admin/Pages/DeviceStats/Edit.razor | 46 ++++++++++++ .../Admin/Pages/DeviceStats/Edit.razor.cs | 73 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor create mode 100644 Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor.cs diff --git a/Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor b/Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor new file mode 100644 index 00000000..050876b5 --- /dev/null +++ b/Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor @@ -0,0 +1,46 @@ +@page "/admin/device-stats/edit/{Id:int}" +@attribute [Authorize] +@layout AdminLayout + +@inject Microsoft.EntityFrameworkCore.IDbContextFactory DbContextFactory + +Edit Device Statistic + +@if(!isLoaded) +{ +
Loading...
+} +else if(deviceStat == null) +{ +
Device statistic not found.
+} +else +{ + + + +
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+ + +
+} + diff --git a/Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor.cs b/Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor.cs new file mode 100644 index 00000000..42039f23 --- /dev/null +++ b/Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor.cs @@ -0,0 +1,73 @@ +using System.ComponentModel.DataAnnotations; +using Aaru.Server.Database.Models; +using Microsoft.AspNetCore.Components; +using DbContext = Aaru.Server.Database.DbContext; + +namespace Aaru.Server.Components.Admin.Pages.DeviceStats; + +public partial class Edit : ComponentBase +{ + DbContext db; + + DeviceStatEditViewModel deviceStat; + bool isLoaded; + [Parameter] + public int Id { get; set; } + + + [Inject] + public NavigationManager Navigation { get; set; } + + protected override async Task OnInitializedAsync() + { + db = await DbContextFactory.CreateDbContextAsync(); + DeviceStat? entity = await db.DeviceStats.FindAsync(Id); + + if(entity != null) + { + deviceStat = new DeviceStatEditViewModel + { + Id = entity.Id, + Manufacturer = entity.Manufacturer, + Model = entity.Model, + Revision = entity.Revision, + Bus = entity.Bus + }; + } + + isLoaded = true; + } + + protected async Task HandleValidSubmit() + { + DeviceStat? entity = await db.DeviceStats.FindAsync(Id); + + if(entity != null) + { + entity.Manufacturer = deviceStat.Manufacturer; + entity.Model = deviceStat.Model; + entity.Revision = deviceStat.Revision; + entity.Bus = deviceStat.Bus; + await db.SaveChangesAsync(); + } + + Navigation.NavigateTo("/admin/device-stats"); + } + + protected void GoBack() + { + Navigation.NavigateTo("/admin/device-stats"); + } + + public class DeviceStatEditViewModel + { + public int Id { get; set; } + [Required] + public string? Manufacturer { get; set; } + [Required] + public string? Model { get; set; } + public string? Revision { get; set; } + [Required] + public string Bus { get; set; } + } +} \ No newline at end of file