feat(adminui): F15.2 batch 2 — topology entity CRUD
Some checks failed
v2-ci / build (push) Failing after 52s
v2-ci / unit-tests (tests/Core/ZB.MOM.WW.OtOpcUa.Cluster.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Security.Tests) (push) Has been skipped
v2-ci / integration (push) Has been skipped

Same single-page edit-or-create pattern as batch 1, applied to the
foundational topology entities. After this batch the whole hierarchy
(cluster → nodes → UNS areas → UNS lines → namespaces → drivers) is
fully editable through the UI.

- ClusterEdit.razor                  /clusters/{id}/edit
  Update + delete for an existing cluster. NodeCount stays coupled to
  RedundancyMode (None→1, Warm/Hot→2). ModifiedBy taken from
  AuthenticationStateProvider.
- NodeEdit.razor                     /clusters/{id}/nodes/{new|nodeId}
  Full ClusterNode CRUD. ApplicationUri uniqueness is enforced by EF
  index; ServiceLevelBase defaults to 200 (primary preference) on
  create; per-node DriverConfigOverridesJson validated as JSON.
- UnsAreaEdit.razor                  /clusters/{id}/uns/areas/{new|id}
- UnsLineEdit.razor                  /clusters/{id}/uns/lines/{new|id}
  UNS structure CRUD; Lines pick their parent Area from a select that
  loads the cluster's areas.

List pages updated:
- ClusterOverview now shows an "Edit cluster" button + a "New node"
  action on the nodes panel + per-row Edit buttons.
- ClusterUns gains New/Edit affordances for both Areas and Lines.

All 9 integration tests still green; no regressions.
This commit is contained in:
Joseph Doherty
2026-05-26 08:18:49 -04:00
parent 5ae67a48ba
commit 45740578c9
6 changed files with 842 additions and 6 deletions

View File

@@ -0,0 +1,200 @@
@page "/clusters/{ClusterId}/edit"
@* Edit page for an existing ServerCluster. The /clusters/new route lives in NewCluster.razor;
this page handles only the update case so the form can disable ClusterId (immutable). *@
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using System.ComponentModel.DataAnnotations
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject NavigationManager Nav
@inject AuthenticationStateProvider AuthState
<div class="d-flex justify-content-between align-items-center mb-3">
<h4 class="mb-0">Edit cluster &middot; <span class="mono">@ClusterId</span></h4>
<a href="/clusters/@ClusterId" class="btn btn-outline-secondary btn-sm">Cancel</a>
</div>
<ClusterNav ClusterId="@ClusterId" ActiveTab="overview" />
@if (!_loaded)
{
<p>Loading…</p>
}
else if (_existing is null)
{
<section class="panel notice rise" style="animation-delay:.02s">
Cluster <span class="mono">@ClusterId</span> was not found.
</section>
}
else
{
<EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="clusterEdit">
<DataAnnotationsValidator />
<section class="panel rise" style="animation-delay:.02s">
<div class="panel-head">Identity</div>
<div style="padding:1rem">
<div class="mb-3">
<label class="form-label">ClusterId</label>
<input class="form-control form-control-sm mono" value="@ClusterId" disabled />
<div class="form-text">Immutable after creation. Operator-visible everywhere; renames would invalidate every downstream reference.</div>
</div>
<div class="mb-3">
<label class="form-label" for="name">Name</label>
<InputText id="name" @bind-Value="_form.Name" class="form-control form-control-sm" />
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label" for="enterprise">Enterprise</label>
<InputText id="enterprise" @bind-Value="_form.Enterprise" class="form-control form-control-sm" />
</div>
<div class="col-md-6 mb-3">
<label class="form-label" for="site">Site</label>
<InputText id="site" @bind-Value="_form.Site" class="form-control form-control-sm" />
</div>
</div>
<div class="mb-3">
<label class="form-label">Enabled</label>
<div class="form-check form-switch">
<InputCheckbox @bind-Value="_form.Enabled" class="form-check-input" />
<label class="form-check-label">Spawn drivers + serve endpoints in deployments</label>
</div>
</div>
</div>
</section>
<section class="panel rise mt-3" style="animation-delay:.08s">
<div class="panel-head">Topology</div>
<div style="padding:1rem">
<div class="mb-3">
<label class="form-label" for="redundancy">Redundancy mode</label>
<InputSelect id="redundancy" @bind-Value="_form.RedundancyMode" class="form-select form-select-sm">
<option value="@RedundancyMode.None">None (1 node)</option>
<option value="@RedundancyMode.Warm">Warm (2 nodes)</option>
<option value="@RedundancyMode.Hot">Hot (2 nodes)</option>
</InputSelect>
</div>
<div class="mb-3">
<label class="form-label" for="notes">Notes</label>
<InputTextArea id="notes" @bind-Value="_form.Notes" class="form-control form-control-sm" rows="3" />
</div>
</div>
</section>
@if (!string.IsNullOrWhiteSpace(_error))
{
<div class="panel notice mt-3" style="border-color:var(--alert)">@_error</div>
}
<div class="mt-3 d-flex gap-2">
<button type="submit" class="btn btn-primary" disabled="@_busy">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
Save changes
</button>
<a href="/clusters/@ClusterId" class="btn btn-outline-secondary">Cancel</a>
<button type="button" class="btn btn-outline-danger ms-auto" @onclick="DeleteAsync" disabled="@_busy">
Delete cluster
</button>
</div>
</EditForm>
}
@code {
[Parameter] public string ClusterId { get; set; } = "";
private FormModel _form = new();
private ServerCluster? _existing;
private bool _loaded;
private bool _busy;
private string? _error;
protected override async Task OnInitializedAsync()
{
await using var db = await DbFactory.CreateDbContextAsync();
_existing = await db.ServerClusters.AsNoTracking()
.FirstOrDefaultAsync(c => c.ClusterId == ClusterId);
if (_existing is not null)
{
_form = new FormModel
{
Name = _existing.Name,
Enterprise = _existing.Enterprise,
Site = _existing.Site,
RedundancyMode = _existing.RedundancyMode,
Enabled = _existing.Enabled,
Notes = _existing.Notes,
};
}
_loaded = true;
}
private async Task SubmitAsync()
{
_busy = true;
_error = null;
try
{
await using var db = await DbFactory.CreateDbContextAsync();
var entity = await db.ServerClusters.FirstOrDefaultAsync(c => c.ClusterId == ClusterId);
if (entity is null) { _error = "Cluster no longer exists."; return; }
var auth = await AuthState.GetAuthenticationStateAsync();
entity.Name = _form.Name;
entity.Enterprise = _form.Enterprise;
entity.Site = _form.Site;
entity.RedundancyMode = _form.RedundancyMode;
entity.NodeCount = _form.RedundancyMode == RedundancyMode.None ? (byte)1 : (byte)2;
entity.Enabled = _form.Enabled;
entity.Notes = string.IsNullOrWhiteSpace(_form.Notes) ? null : _form.Notes;
entity.ModifiedAt = DateTime.UtcNow;
entity.ModifiedBy = auth.User.Identity?.Name ?? "(anonymous)";
await db.SaveChangesAsync();
Nav.NavigateTo($"/clusters/{ClusterId}");
}
catch (Exception ex)
{
_error = ex.Message;
}
finally
{
_busy = false;
}
}
private async Task DeleteAsync()
{
_busy = true;
_error = null;
try
{
await using var db = await DbFactory.CreateDbContextAsync();
var entity = await db.ServerClusters.FirstOrDefaultAsync(c => c.ClusterId == ClusterId);
if (entity is null) { Nav.NavigateTo("/clusters"); return; }
db.ServerClusters.Remove(entity);
await db.SaveChangesAsync();
Nav.NavigateTo("/clusters");
}
catch (Exception ex)
{
_error = $"Delete failed: {ex.Message}. Likely because nodes, namespaces, drivers, or other rows still reference this cluster — remove them first.";
}
finally
{
_busy = false;
}
}
private sealed class FormModel
{
[Required] public string Name { get; set; } = "";
[Required] public string Enterprise { get; set; } = "";
[Required] public string Site { get; set; } = "";
public RedundancyMode RedundancyMode { get; set; } = RedundancyMode.None;
public bool Enabled { get; set; } = true;
public string? Notes { get; set; }
}
}

View File

@@ -26,7 +26,8 @@ else
<span class="mono text-muted">@_cluster.ClusterId</span>
@if (!_cluster.Enabled) { <span class="chip chip-idle ms-2">Disabled</span> }
</div>
<div>
<div class="d-flex gap-2">
<a href="/clusters/@ClusterId/edit" class="btn btn-outline-secondary btn-sm">Edit cluster</a>
<a href="/deployments" class="btn btn-outline-primary btn-sm">Deployments</a>
</div>
</div>
@@ -69,7 +70,10 @@ else
</section>
<section class="panel rise mt-3" style="animation-delay:.14s">
<div class="panel-head">Nodes</div>
<div class="panel-head d-flex align-items-center">
<span>Nodes</span>
<a href="/clusters/@ClusterId/nodes/new" class="btn btn-sm btn-outline-primary ms-auto">New node</a>
</div>
@if (_nodes is null || _nodes.Count == 0)
{
<div style="padding:1rem" class="text-muted">No nodes registered.</div>
@@ -85,6 +89,7 @@ else
<th>OPC UA port</th>
<th>ApplicationUri</th>
<th class="num">ServiceLevel base</th>
<th></th>
</tr>
</thead>
<tbody>
@@ -96,6 +101,7 @@ else
<td class="num">@n.OpcUaPort</td>
<td><span class="mono small">@n.ApplicationUri</span></td>
<td class="num">@n.ServiceLevelBase</td>
<td><a href="/clusters/@ClusterId/nodes/@n.NodeId" class="btn btn-sm btn-outline-primary">Edit</a></td>
</tr>
}
</tbody>

View File

@@ -25,7 +25,10 @@ else
</section>
<section class="panel rise mt-3" style="animation-delay:.08s">
<div class="panel-head">Areas (level 3) &middot; @_areas.Count</div>
<div class="panel-head d-flex align-items-center">
<span>Areas (level 3) &middot; @_areas.Count</span>
<a href="/clusters/@ClusterId/uns/areas/new" class="btn btn-sm btn-outline-primary ms-auto">New area</a>
</div>
@if (_areas.Count == 0)
{
<div style="padding:1rem" class="text-muted">No areas defined.</div>
@@ -34,7 +37,7 @@ else
{
<div class="table-wrap">
<table class="data-table">
<thead><tr><th>UnsAreaId</th><th>Name</th><th>Notes</th></tr></thead>
<thead><tr><th>UnsAreaId</th><th>Name</th><th>Notes</th><th></th></tr></thead>
<tbody>
@foreach (var a in _areas)
{
@@ -42,6 +45,7 @@ else
<td><span class="mono">@a.UnsAreaId</span></td>
<td>@a.Name</td>
<td class="text-muted small">@(a.Notes ?? "")</td>
<td><a href="/clusters/@ClusterId/uns/areas/@a.UnsAreaId" class="btn btn-sm btn-outline-primary">Edit</a></td>
</tr>
}
</tbody>
@@ -51,7 +55,10 @@ else
</section>
<section class="panel rise mt-3" style="animation-delay:.14s">
<div class="panel-head">Lines (level 4) &middot; @_lines.Count</div>
<div class="panel-head d-flex align-items-center">
<span>Lines (level 4) &middot; @_lines.Count</span>
<a href="/clusters/@ClusterId/uns/lines/new" class="btn btn-sm btn-outline-primary ms-auto">New line</a>
</div>
@if (_lines.Count == 0)
{
<div style="padding:1rem" class="text-muted">No lines defined.</div>
@@ -60,7 +67,7 @@ else
{
<div class="table-wrap">
<table class="data-table">
<thead><tr><th>UnsLineId</th><th>Name</th><th>Area</th><th>Notes</th></tr></thead>
<thead><tr><th>UnsLineId</th><th>Name</th><th>Area</th><th>Notes</th><th></th></tr></thead>
<tbody>
@foreach (var l in _lines)
{
@@ -69,6 +76,7 @@ else
<td>@l.Name</td>
<td><span class="mono">@l.UnsAreaId</span></td>
<td class="text-muted small">@(l.Notes ?? "")</td>
<td><a href="/clusters/@ClusterId/uns/lines/@l.UnsLineId" class="btn btn-sm btn-outline-primary">Edit</a></td>
</tr>
}
</tbody>

View File

@@ -0,0 +1,268 @@
@page "/clusters/{ClusterId}/nodes/new"
@page "/clusters/{ClusterId}/nodes/{NodeId}"
@* ClusterNode CRUD. ApplicationUri is fleet-wide unique — the EF unique index enforces this
at SaveChanges. ServiceLevelBase defaults: 200 primary, 150 secondary. *@
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using System.ComponentModel.DataAnnotations
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject NavigationManager Nav
@inject AuthenticationStateProvider AuthState
<div class="d-flex justify-content-between align-items-center mb-3">
<h4 class="mb-0">@(IsNew ? "New node" : "Edit node") &middot; <span class="mono">@ClusterId</span></h4>
<a href="/clusters/@ClusterId" class="btn btn-outline-secondary btn-sm">Cancel</a>
</div>
<ClusterNav ClusterId="@ClusterId" ActiveTab="overview" />
@if (!_loaded)
{
<p>Loading…</p>
}
else if (!IsNew && _existing is null)
{
<section class="panel notice rise" style="animation-delay:.02s">
Node <span class="mono">@NodeId</span> was not found in cluster <span class="mono">@ClusterId</span>.
</section>
}
else
{
<EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="nodeEdit">
<DataAnnotationsValidator />
<section class="panel rise" style="animation-delay:.02s">
<div class="panel-head">Identity</div>
<div style="padding:1rem">
<div class="mb-3">
<label class="form-label" for="nodeId">NodeId</label>
<InputText id="nodeId" @bind-Value="_form.NodeId" disabled="@(!IsNew)"
class="form-control form-control-sm mono"
placeholder="LINE3-OPCUA-A" />
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label" for="host">Host</label>
<InputText id="host" @bind-Value="_form.Host" class="form-control form-control-sm mono"
placeholder="line3-opc-a.plant.local" />
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="port">OPC UA port</label>
<InputNumber id="port" @bind-Value="_form.OpcUaPort" class="form-control form-control-sm" />
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="dashport">Dashboard port</label>
<InputNumber id="dashport" @bind-Value="_form.DashboardPort" class="form-control form-control-sm" />
</div>
</div>
<div class="mb-3">
<label class="form-label" for="uri">ApplicationUri</label>
<InputText id="uri" @bind-Value="_form.ApplicationUri" class="form-control form-control-sm mono"
placeholder="urn:zb:warsaw-west:line3:opc-a" />
<div class="form-text">Must be unique fleet-wide. Clients pin trust here — never silently rewrite based on Host.</div>
</div>
</div>
</section>
<section class="panel rise mt-3" style="animation-delay:.08s">
<div class="panel-head">Redundancy + behaviour</div>
<div style="padding:1rem">
<div class="row">
<div class="col-md-4 mb-3">
<label class="form-label" for="slbase">ServiceLevel base</label>
<InputNumber id="slbase" @bind-Value="_form.ServiceLevelBase" class="form-control form-control-sm" />
<div class="form-text">200 = primary preference, 150 = secondary preference. Live ServiceLevel adjusts down on faults.</div>
</div>
<div class="col-md-4 mb-3">
<label class="form-label">Enabled</label>
<div class="form-check form-switch">
<InputCheckbox @bind-Value="_form.Enabled" class="form-check-input" />
<label class="form-check-label">Join the Akka cluster + serve endpoints</label>
</div>
</div>
</div>
<div class="mb-3">
<label class="form-label" for="overrides">Driver config overrides (JSON, optional)</label>
<InputTextArea id="overrides" @bind-Value="_form.DriverConfigOverridesJson" rows="6"
class="form-control form-control-sm mono"
placeholder='{ "drv-modbus-line3-01": { "endpoint": "10.0.0.43:502" } }' />
<div class="form-text">Per-node merge over cluster-level <span class="mono">DriverInstance.DriverConfig</span>. Minimal by design — heavy node-specific config is a smell.</div>
</div>
</div>
</section>
@if (!string.IsNullOrWhiteSpace(_error))
{
<div class="panel notice mt-3" style="border-color:var(--alert)">@_error</div>
}
<div class="mt-3 d-flex gap-2">
<button type="submit" class="btn btn-primary" disabled="@_busy">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
@(IsNew ? "Create" : "Save changes")
</button>
<a href="/clusters/@ClusterId" class="btn btn-outline-secondary">Cancel</a>
@if (!IsNew)
{
<button type="button" class="btn btn-outline-danger ms-auto" @onclick="DeleteAsync" disabled="@_busy">
Delete
</button>
}
</div>
</EditForm>
}
@code {
[Parameter] public string ClusterId { get; set; } = "";
[Parameter] public string? NodeId { get; set; }
private bool IsNew => string.IsNullOrEmpty(NodeId);
private FormModel _form = new();
private ClusterNode? _existing;
private bool _loaded;
private bool _busy;
private string? _error;
protected override async Task OnInitializedAsync()
{
if (IsNew)
{
_form = new FormModel
{
NodeId = "",
Host = "",
OpcUaPort = 4840,
DashboardPort = 8081,
ApplicationUri = "",
ServiceLevelBase = 200,
Enabled = true,
};
}
else
{
await using var db = await DbFactory.CreateDbContextAsync();
_existing = await db.ClusterNodes.AsNoTracking()
.FirstOrDefaultAsync(n => n.ClusterId == ClusterId && n.NodeId == NodeId);
if (_existing is not null)
{
_form = new FormModel
{
NodeId = _existing.NodeId,
Host = _existing.Host,
OpcUaPort = _existing.OpcUaPort,
DashboardPort = _existing.DashboardPort,
ApplicationUri = _existing.ApplicationUri,
ServiceLevelBase = _existing.ServiceLevelBase,
Enabled = _existing.Enabled,
DriverConfigOverridesJson = _existing.DriverConfigOverridesJson,
};
}
}
_loaded = true;
}
private async Task SubmitAsync()
{
_busy = true;
_error = null;
try
{
if (!string.IsNullOrWhiteSpace(_form.DriverConfigOverridesJson))
{
try { using var _ = System.Text.Json.JsonDocument.Parse(_form.DriverConfigOverridesJson); }
catch { _error = "DriverConfigOverridesJson is not valid JSON."; return; }
}
await using var db = await DbFactory.CreateDbContextAsync();
if (IsNew)
{
if (await db.ClusterNodes.AnyAsync(n => n.NodeId == _form.NodeId))
{
_error = $"Node '{_form.NodeId}' already exists.";
return;
}
var auth = await AuthState.GetAuthenticationStateAsync();
db.ClusterNodes.Add(new ClusterNode
{
NodeId = _form.NodeId,
ClusterId = ClusterId,
Host = _form.Host,
OpcUaPort = _form.OpcUaPort,
DashboardPort = _form.DashboardPort,
ApplicationUri = _form.ApplicationUri,
ServiceLevelBase = _form.ServiceLevelBase,
Enabled = _form.Enabled,
DriverConfigOverridesJson = string.IsNullOrWhiteSpace(_form.DriverConfigOverridesJson) ? null : _form.DriverConfigOverridesJson,
CreatedAt = DateTime.UtcNow,
CreatedBy = auth.User.Identity?.Name ?? "(anonymous)",
});
}
else
{
var entity = await db.ClusterNodes.FirstOrDefaultAsync(
n => n.ClusterId == ClusterId && n.NodeId == NodeId);
if (entity is null) { _error = "Row no longer exists."; return; }
entity.Host = _form.Host;
entity.OpcUaPort = _form.OpcUaPort;
entity.DashboardPort = _form.DashboardPort;
entity.ApplicationUri = _form.ApplicationUri;
entity.ServiceLevelBase = _form.ServiceLevelBase;
entity.Enabled = _form.Enabled;
entity.DriverConfigOverridesJson = string.IsNullOrWhiteSpace(_form.DriverConfigOverridesJson) ? null : _form.DriverConfigOverridesJson;
}
await db.SaveChangesAsync();
Nav.NavigateTo($"/clusters/{ClusterId}");
}
catch (Exception ex)
{
_error = ex.Message;
}
finally
{
_busy = false;
}
}
private async Task DeleteAsync()
{
if (IsNew) return;
_busy = true;
_error = null;
try
{
await using var db = await DbFactory.CreateDbContextAsync();
var entity = await db.ClusterNodes.FirstOrDefaultAsync(
n => n.ClusterId == ClusterId && n.NodeId == NodeId);
if (entity is null) { Nav.NavigateTo($"/clusters/{ClusterId}"); return; }
db.ClusterNodes.Remove(entity);
await db.SaveChangesAsync();
Nav.NavigateTo($"/clusters/{ClusterId}");
}
catch (Exception ex)
{
_error = ex.Message;
}
finally
{
_busy = false;
}
}
private sealed class FormModel
{
[Required, RegularExpression("^[A-Za-z0-9_-]+$")]
public string NodeId { get; set; } = "";
[Required] public string Host { get; set; } = "";
[Range(1, 65535)] public int OpcUaPort { get; set; } = 4840;
[Range(1, 65535)] public int DashboardPort { get; set; } = 8081;
[Required, RegularExpression("^urn:[A-Za-z0-9_:./-]+$")]
public string ApplicationUri { get; set; } = "";
[Range(0, 255)] public byte ServiceLevelBase { get; set; } = 200;
public bool Enabled { get; set; } = true;
public string? DriverConfigOverridesJson { get; set; }
}
}

View File

@@ -0,0 +1,167 @@
@page "/clusters/{ClusterId}/uns/areas/new"
@page "/clusters/{ClusterId}/uns/areas/{UnsAreaId}"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using System.ComponentModel.DataAnnotations
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject NavigationManager Nav
<div class="d-flex justify-content-between align-items-center mb-3">
<h4 class="mb-0">@(IsNew ? "New UNS area" : "Edit UNS area") &middot; <span class="mono">@ClusterId</span></h4>
<a href="/clusters/@ClusterId/uns" class="btn btn-outline-secondary btn-sm">Cancel</a>
</div>
<ClusterNav ClusterId="@ClusterId" ActiveTab="uns" />
@if (!_loaded)
{
<p>Loading…</p>
}
else if (!IsNew && _existing is null)
{
<section class="panel notice rise" style="animation-delay:.02s">
Area <span class="mono">@UnsAreaId</span> was not found.
</section>
}
else
{
<EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="unsAreaEdit">
<DataAnnotationsValidator />
<section class="panel rise" style="animation-delay:.02s">
<div class="panel-head">UNS area (level 3)</div>
<div style="padding:1rem">
<div class="mb-3">
<label class="form-label" for="aid">UnsAreaId</label>
<InputText id="aid" @bind-Value="_form.UnsAreaId" disabled="@(!IsNew)"
class="form-control form-control-sm mono" />
</div>
<div class="mb-3">
<label class="form-label" for="name">Name</label>
<InputText id="name" @bind-Value="_form.Name" class="form-control form-control-sm" />
</div>
<div class="mb-3">
<label class="form-label" for="notes">Notes</label>
<InputTextArea id="notes" @bind-Value="_form.Notes" class="form-control form-control-sm" rows="3" />
</div>
</div>
</section>
@if (!string.IsNullOrWhiteSpace(_error))
{
<div class="panel notice mt-3" style="border-color:var(--alert)">@_error</div>
}
<div class="mt-3 d-flex gap-2">
<button type="submit" class="btn btn-primary" disabled="@_busy">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
@(IsNew ? "Create" : "Save changes")
</button>
<a href="/clusters/@ClusterId/uns" class="btn btn-outline-secondary">Cancel</a>
@if (!IsNew)
{
<button type="button" class="btn btn-outline-danger ms-auto" @onclick="DeleteAsync" disabled="@_busy">Delete</button>
}
</div>
</EditForm>
}
@code {
[Parameter] public string ClusterId { get; set; } = "";
[Parameter] public string? UnsAreaId { get; set; }
private bool IsNew => string.IsNullOrEmpty(UnsAreaId);
private FormModel _form = new();
private UnsArea? _existing;
private bool _loaded;
private bool _busy;
private string? _error;
protected override async Task OnInitializedAsync()
{
if (!IsNew)
{
await using var db = await DbFactory.CreateDbContextAsync();
_existing = await db.UnsAreas.AsNoTracking()
.FirstOrDefaultAsync(a => a.ClusterId == ClusterId && a.UnsAreaId == UnsAreaId);
if (_existing is not null)
{
_form = new FormModel
{
UnsAreaId = _existing.UnsAreaId,
Name = _existing.Name,
Notes = _existing.Notes,
RowVersion = _existing.RowVersion,
};
}
}
_loaded = true;
}
private async Task SubmitAsync()
{
_busy = true; _error = null;
try
{
await using var db = await DbFactory.CreateDbContextAsync();
if (IsNew)
{
if (await db.UnsAreas.AnyAsync(a => a.UnsAreaId == _form.UnsAreaId))
{ _error = $"Area '{_form.UnsAreaId}' already exists."; return; }
db.UnsAreas.Add(new UnsArea
{
UnsAreaId = _form.UnsAreaId,
ClusterId = ClusterId,
Name = _form.Name,
Notes = string.IsNullOrWhiteSpace(_form.Notes) ? null : _form.Notes,
});
}
else
{
var entity = await db.UnsAreas.FirstOrDefaultAsync(
a => a.ClusterId == ClusterId && a.UnsAreaId == UnsAreaId);
if (entity is null) { _error = "Row no longer exists."; return; }
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
entity.Name = _form.Name;
entity.Notes = string.IsNullOrWhiteSpace(_form.Notes) ? null : _form.Notes;
}
await db.SaveChangesAsync();
Nav.NavigateTo($"/clusters/{ClusterId}/uns");
}
catch (DbUpdateConcurrencyException) { _error = "Another user changed this area while you were editing. Reload to see the latest values."; }
catch (Exception ex) { _error = ex.Message; }
finally { _busy = false; }
}
private async Task DeleteAsync()
{
if (IsNew) return;
_busy = true; _error = null;
try
{
await using var db = await DbFactory.CreateDbContextAsync();
var entity = await db.UnsAreas.FirstOrDefaultAsync(
a => a.ClusterId == ClusterId && a.UnsAreaId == UnsAreaId);
if (entity is null) { Nav.NavigateTo($"/clusters/{ClusterId}/uns"); return; }
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
db.UnsAreas.Remove(entity);
await db.SaveChangesAsync();
Nav.NavigateTo($"/clusters/{ClusterId}/uns");
}
catch (DbUpdateConcurrencyException) { _error = "Another user changed this area while you were viewing it."; }
catch (Exception ex) { _error = $"Delete failed: {ex.Message}. Likely because lines still reference this area — remove them first."; }
finally { _busy = false; }
}
private sealed class FormModel
{
[Required, RegularExpression("^[A-Za-z0-9_-]+$")] public string UnsAreaId { get; set; } = "";
[Required] public string Name { get; set; } = "";
public string? Notes { get; set; }
public byte[] RowVersion { get; set; } = [];
}
}

View File

@@ -0,0 +1,187 @@
@page "/clusters/{ClusterId}/uns/lines/new"
@page "/clusters/{ClusterId}/uns/lines/{UnsLineId}"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using System.ComponentModel.DataAnnotations
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject NavigationManager Nav
<div class="d-flex justify-content-between align-items-center mb-3">
<h4 class="mb-0">@(IsNew ? "New UNS line" : "Edit UNS line") &middot; <span class="mono">@ClusterId</span></h4>
<a href="/clusters/@ClusterId/uns" class="btn btn-outline-secondary btn-sm">Cancel</a>
</div>
<ClusterNav ClusterId="@ClusterId" ActiveTab="uns" />
@if (!_loaded)
{
<p>Loading…</p>
}
else if (!IsNew && _existing is null)
{
<section class="panel notice rise" style="animation-delay:.02s">
Line <span class="mono">@UnsLineId</span> was not found.
</section>
}
else
{
<EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="unsLineEdit">
<DataAnnotationsValidator />
<section class="panel rise" style="animation-delay:.02s">
<div class="panel-head">UNS line (level 4)</div>
<div style="padding:1rem">
<div class="mb-3">
<label class="form-label" for="lid">UnsLineId</label>
<InputText id="lid" @bind-Value="_form.UnsLineId" disabled="@(!IsNew)"
class="form-control form-control-sm mono" />
</div>
<div class="mb-3">
<label class="form-label" for="area">Parent area</label>
<InputSelect id="area" @bind-Value="_form.UnsAreaId" class="form-select form-select-sm">
@foreach (var area in _areas)
{
<option value="@area.UnsAreaId">@area.UnsAreaId &mdash; @area.Name</option>
}
</InputSelect>
</div>
<div class="mb-3">
<label class="form-label" for="name">Name</label>
<InputText id="name" @bind-Value="_form.Name" class="form-control form-control-sm" />
</div>
<div class="mb-3">
<label class="form-label" for="notes">Notes</label>
<InputTextArea id="notes" @bind-Value="_form.Notes" class="form-control form-control-sm" rows="3" />
</div>
</div>
</section>
@if (!string.IsNullOrWhiteSpace(_error))
{
<div class="panel notice mt-3" style="border-color:var(--alert)">@_error</div>
}
<div class="mt-3 d-flex gap-2">
<button type="submit" class="btn btn-primary" disabled="@_busy">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
@(IsNew ? "Create" : "Save changes")
</button>
<a href="/clusters/@ClusterId/uns" class="btn btn-outline-secondary">Cancel</a>
@if (!IsNew)
{
<button type="button" class="btn btn-outline-danger ms-auto" @onclick="DeleteAsync" disabled="@_busy">Delete</button>
}
</div>
</EditForm>
}
@code {
[Parameter] public string ClusterId { get; set; } = "";
[Parameter] public string? UnsLineId { get; set; }
private bool IsNew => string.IsNullOrEmpty(UnsLineId);
private FormModel _form = new();
private UnsLine? _existing;
private List<UnsArea> _areas = new();
private bool _loaded;
private bool _busy;
private string? _error;
protected override async Task OnInitializedAsync()
{
await using var db = await DbFactory.CreateDbContextAsync();
_areas = await db.UnsAreas.AsNoTracking()
.Where(a => a.ClusterId == ClusterId)
.OrderBy(a => a.UnsAreaId)
.ToListAsync();
if (!IsNew)
{
_existing = await db.UnsLines.AsNoTracking()
.FirstOrDefaultAsync(l => l.UnsLineId == UnsLineId);
if (_existing is not null)
{
_form = new FormModel
{
UnsLineId = _existing.UnsLineId,
UnsAreaId = _existing.UnsAreaId,
Name = _existing.Name,
Notes = _existing.Notes,
RowVersion = _existing.RowVersion,
};
}
}
else
{
_form.UnsAreaId = _areas.FirstOrDefault()?.UnsAreaId ?? "";
}
_loaded = true;
}
private async Task SubmitAsync()
{
_busy = true; _error = null;
try
{
await using var db = await DbFactory.CreateDbContextAsync();
if (IsNew)
{
if (await db.UnsLines.AnyAsync(l => l.UnsLineId == _form.UnsLineId))
{ _error = $"Line '{_form.UnsLineId}' already exists."; return; }
db.UnsLines.Add(new UnsLine
{
UnsLineId = _form.UnsLineId,
UnsAreaId = _form.UnsAreaId,
Name = _form.Name,
Notes = string.IsNullOrWhiteSpace(_form.Notes) ? null : _form.Notes,
});
}
else
{
var entity = await db.UnsLines.FirstOrDefaultAsync(l => l.UnsLineId == UnsLineId);
if (entity is null) { _error = "Row no longer exists."; return; }
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
entity.UnsAreaId = _form.UnsAreaId;
entity.Name = _form.Name;
entity.Notes = string.IsNullOrWhiteSpace(_form.Notes) ? null : _form.Notes;
}
await db.SaveChangesAsync();
Nav.NavigateTo($"/clusters/{ClusterId}/uns");
}
catch (DbUpdateConcurrencyException) { _error = "Another user changed this line while you were editing."; }
catch (Exception ex) { _error = ex.Message; }
finally { _busy = false; }
}
private async Task DeleteAsync()
{
if (IsNew) return;
_busy = true; _error = null;
try
{
await using var db = await DbFactory.CreateDbContextAsync();
var entity = await db.UnsLines.FirstOrDefaultAsync(l => l.UnsLineId == UnsLineId);
if (entity is null) { Nav.NavigateTo($"/clusters/{ClusterId}/uns"); return; }
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
db.UnsLines.Remove(entity);
await db.SaveChangesAsync();
Nav.NavigateTo($"/clusters/{ClusterId}/uns");
}
catch (DbUpdateConcurrencyException) { _error = "Another user changed this line while you were viewing it."; }
catch (Exception ex) { _error = $"Delete failed: {ex.Message}. Likely because equipment still references this line — remove or re-home it first."; }
finally { _busy = false; }
}
private sealed class FormModel
{
[Required, RegularExpression("^[A-Za-z0-9_-]+$")] public string UnsLineId { get; set; } = "";
[Required] public string UnsAreaId { get; set; } = "";
[Required] public string Name { get; set; } = "";
public string? Notes { get; set; }
public byte[] RowVersion { get; set; } = [];
}
}