Add edit device page with form for updating device details

This commit is contained in:
2025-09-13 02:38:34 +01:00
parent 64675a73e8
commit 62c9260db1
2 changed files with 148 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
@page "/admin/devices/edit/{Id:int}"
@using Aaru.CommonTypes.Enums
@attribute [Authorize]
@layout AdminLayout
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
<PageTitle>Edit Device</PageTitle>
@if(!isLoaded)
{
<div>Loading...</div>
}
else if(device == null)
{
<div class="alert alert-danger">Device not found.</div>
}
else
{
<EditForm Model="device" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="mb-3">
<label class="form-label">Manufacturer</label>
<InputText class="form-control" @bind-Value="device.Manufacturer" />
<ValidationMessage For="@(() => device.Manufacturer)" />
</div>
<div class="mb-3">
<label class="form-label">Model</label>
<InputText class="form-control" @bind-Value="device.Model" />
<ValidationMessage For="@(() => device.Model)" />
</div>
<div class="mb-3">
<label class="form-label">Revision</label>
<InputText class="form-control" @bind-Value="device.Revision" />
<ValidationMessage For="@(() => device.Revision)" />
</div>
<div class="mb-3 form-check">
<InputCheckbox class="form-check-input" @bind-Value="device.CompactFlash" />
<label class="form-check-label">Compact Flash</label>
</div>
<div class="mb-3">
<label class="form-label">Optimal Multiple Sectors Read</label>
<InputNumber class="form-control" @bind-Value="device.OptimalMultipleSectorsRead" />
<ValidationMessage For="@(() => device.OptimalMultipleSectorsRead)" />
</div>
<div class="mb-3 form-check">
<InputCheckbox class="form-check-input" @bind-Value="device.CanReadGdRomUsingSwapDisc" />
<label class="form-check-label">Can Read GD-ROM Using Swap Disc</label>
</div>
<div class="mb-3">
<label class="form-label">Type</label>
<InputSelect class="form-control" @bind-Value="device.Type">
@foreach(object? type in Enum.GetValues(typeof(DeviceType)))
{
<option value="@type">@type</option>
}
</InputSelect>
<ValidationMessage For="@(() => device.Type)" />
</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,84 @@
using System.ComponentModel.DataAnnotations;
using Aaru.CommonTypes.Enums;
using Aaru.Server.Database.Models;
using Microsoft.AspNetCore.Components;
using DbContext = Aaru.Server.Database.DbContext;
namespace Aaru.Server.Components.Admin.Pages.Devices;
public partial class Edit
{
DbContext db;
DeviceEditViewModel device;
bool isLoaded;
[Parameter]
public int Id { get; set; }
[Inject]
public NavigationManager Navigation { get; set; }
protected override async Task OnInitializedAsync()
{
db = DbContextFactory.CreateDbContext();
Device? entity = await db.Devices.FindAsync(Id);
if(entity != null)
{
device = new DeviceEditViewModel
{
Id = entity.Id,
Manufacturer = entity.Manufacturer,
Model = entity.Model,
Revision = entity.Revision,
CompactFlash = entity.CompactFlash,
OptimalMultipleSectorsRead = entity.OptimalMultipleSectorsRead,
CanReadGdRomUsingSwapDisc = entity.CanReadGdRomUsingSwapDisc ?? false,
Type = entity.Type
};
}
isLoaded = true;
}
protected async Task HandleValidSubmit()
{
Device? entity = await db.Devices.FindAsync(Id);
if(entity != null)
{
entity.Manufacturer = device.Manufacturer;
entity.Model = device.Model;
entity.Revision = device.Revision;
entity.CompactFlash = device.CompactFlash;
entity.OptimalMultipleSectorsRead = device.OptimalMultipleSectorsRead;
entity.CanReadGdRomUsingSwapDisc = device.CanReadGdRomUsingSwapDisc;
entity.Type = device.Type;
entity.ModifiedWhen = DateTime.UtcNow;
await db.SaveChangesAsync();
}
Navigation.NavigateTo("/admin/devices");
}
protected void GoBack()
{
Navigation.NavigateTo("/admin/devices");
}
public class DeviceEditViewModel
{
public int Id { get; set; }
[Required]
public string Manufacturer { get; set; }
[Required]
public string Model { get; set; }
public string Revision { get; set; }
public bool CompactFlash { get; set; }
[Range(0, int.MaxValue)]
public int OptimalMultipleSectorsRead { get; set; }
public bool CanReadGdRomUsingSwapDisc { get; set; }
[Required]
public DeviceType Type { get; set; }
}
}