Add edit functionality for device statistics with form validation

This commit is contained in:
2025-09-13 03:35:18 +01:00
parent 5fbd375b42
commit 6ebd049c4d
2 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
@page "/admin/device-stats/edit/{Id:int}"
@attribute [Authorize]
@layout AdminLayout
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
<PageTitle>Edit Device Statistic</PageTitle>
@if(!isLoaded)
{
<div>Loading...</div>
}
else if(deviceStat == null)
{
<div class="alert alert-danger">Device statistic not found.</div>
}
else
{
<EditForm Model="deviceStat" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="mb-3">
<label class="form-label">Manufacturer</label>
<InputText class="form-control" @bind-Value="deviceStat.Manufacturer" />
<ValidationMessage For="@(() => deviceStat.Manufacturer)" />
</div>
<div class="mb-3">
<label class="form-label">Model</label>
<InputText class="form-control" @bind-Value="deviceStat.Model" />
<ValidationMessage For="@(() => deviceStat.Model)" />
</div>
<div class="mb-3">
<label class="form-label">Revision</label>
<InputText class="form-control" @bind-Value="deviceStat.Revision" />
<ValidationMessage For="@(() => deviceStat.Revision)" />
</div>
<div class="mb-3">
<label class="form-label">Bus</label>
<InputText class="form-control" @bind-Value="deviceStat.Bus" />
<ValidationMessage For="@(() => deviceStat.Bus)" />
</div>
<button type="submit" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-secondary ms-2" @onclick="GoBack">Cancel</button>
</EditForm>
}

View File

@@ -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; }
}
}