mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Add edit functionality for device statistics with form validation
This commit is contained in:
46
Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor
Normal file
46
Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor
Normal 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>
|
||||||
|
}
|
||||||
|
|
||||||
73
Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor.cs
Normal file
73
Aaru.Server/Components/Admin/Pages/DeviceStats/Edit.razor.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user