feat(adminui): F15.2 batch 1 — Namespace + DriverInstance live-edit CRUD
Some checks failed
v2-ci / build (push) Failing after 34s
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
Some checks failed
v2-ci / build (push) Failing after 34s
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
Pattern proof for the live-edit forms gated by Phases A–D's read views.
Each entity gets a single edit page handling both create (route param
omitted) and update (route param present) modes, with RowVersion-based
optimistic concurrency checked against EF Core's
DbUpdateConcurrencyException.
Pattern:
- @page "/clusters/{id}/<thing>/new"
- @page "/clusters/{id}/<thing>/{rowId}"
- IsNew computed from rowId presence
- EditForm + DataAnnotations validation
- byte[] RowVersion stashed on FormModel; assigned to
Entry(e).Property(e => e.RowVersion).OriginalValue before SaveChanges
- Delete button (edit mode only) flows through the same RowVersion check
- Concurrency conflict surfaces as an inline error panel; user reloads
This batch:
- NamespaceEdit.razor — small entity, validates the pattern
- DriverEdit.razor — keystone for everything downstream
(Equipment/Tag/VirtualTag/ScriptedAlarm),
JSON config editor per Q1 with reformat
on save and validation pre-flight
- ClusterNamespaces row gains an Edit button + New action
- ClusterDrivers expanded view gains an Edit button + New action
Equipment/UnsArea/UnsLine/Tag/ACL/VirtualTag/ScriptedAlarm/Script forms
follow this same template in subsequent F15.2 batches.
All 9 integration tests still green; no v2 test regressions.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Drivers · <span class="mono">@ClusterId</span></h4>
|
||||
<a href="/clusters/@ClusterId/drivers/new" class="btn btn-primary btn-sm">New driver</a>
|
||||
</div>
|
||||
|
||||
<ClusterNav ClusterId="@ClusterId" ActiveTab="drivers" />
|
||||
@@ -43,6 +44,9 @@ else
|
||||
<span class="text-muted small ms-2">ns=@d.NamespaceId</span>
|
||||
</summary>
|
||||
<div style="padding:0 1rem 1rem">
|
||||
<div class="d-flex mb-2">
|
||||
<a href="/clusters/@ClusterId/drivers/@d.DriverInstanceId" class="btn btn-sm btn-outline-primary">Edit</a>
|
||||
</div>
|
||||
<pre class="mono small" style="background:var(--surface-2);padding:1rem;border-radius:4px;overflow:auto">@FormatJson(d.DriverConfig)</pre>
|
||||
@if (!string.IsNullOrWhiteSpace(d.ResilienceConfig))
|
||||
{
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Namespaces · <span class="mono">@ClusterId</span></h4>
|
||||
<a href="/clusters/@ClusterId/namespaces/new" class="btn btn-primary btn-sm">New namespace</a>
|
||||
</div>
|
||||
|
||||
<ClusterNav ClusterId="@ClusterId" ActiveTab="namespaces" />
|
||||
@@ -41,6 +42,7 @@ else
|
||||
<th>URI</th>
|
||||
<th>Status</th>
|
||||
<th>Notes</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -55,6 +57,7 @@ else
|
||||
else { <span class="chip chip-idle">Disabled</span> }
|
||||
</td>
|
||||
<td class="text-muted small">@(n.Notes ?? "")</td>
|
||||
<td><a href="/clusters/@ClusterId/namespaces/@n.NamespaceId" class="btn btn-sm btn-outline-primary">Edit</a></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
@page "/clusters/{ClusterId}/drivers/new"
|
||||
@page "/clusters/{ClusterId}/drivers/{DriverInstanceId}"
|
||||
@* Per Q1 of the AdminUI rebuild plan — JSON editor only, typed driver editors deferred.
|
||||
DriverInstance is the keystone for everything downstream (Equipment, Tag, VirtualTag,
|
||||
ScriptedAlarm all reference DriverInstanceId), so this is the second edit page after
|
||||
Namespace. *@
|
||||
@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 driver instance" : "Edit driver instance") · <span class="mono">@ClusterId</span></h4>
|
||||
<a href="/clusters/@ClusterId/drivers" class="btn btn-outline-secondary btn-sm">Cancel</a>
|
||||
</div>
|
||||
|
||||
<ClusterNav ClusterId="@ClusterId" ActiveTab="drivers" />
|
||||
|
||||
@if (!_loaded)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (!IsNew && _existing is null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Driver instance <span class="mono">@DriverInstanceId</span> was not found in cluster <span class="mono">@ClusterId</span>.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="driverEdit">
|
||||
<DataAnnotationsValidator />
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">@(IsNew ? "Identity" : $"Edit {_form.DriverInstanceId}")</div>
|
||||
<div style="padding:1rem">
|
||||
<div class="row">
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label" for="instId">DriverInstanceId</label>
|
||||
<InputText id="instId" @bind-Value="_form.DriverInstanceId" disabled="@(!IsNew)"
|
||||
class="form-control form-control-sm mono"
|
||||
placeholder="drv-modbus-line3-01" />
|
||||
</div>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label" for="name">Display name</label>
|
||||
<InputText id="name" @bind-Value="_form.Name" class="form-control form-control-sm"
|
||||
placeholder="Line 3 Modbus" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label" for="dtype">Driver type</label>
|
||||
<InputSelect id="dtype" @bind-Value="_form.DriverType" disabled="@(!IsNew)"
|
||||
class="form-select form-select-sm">
|
||||
<option value="ModbusTcp">ModbusTcp</option>
|
||||
<option value="AbCip">AbCip</option>
|
||||
<option value="AbLegacy">AbLegacy</option>
|
||||
<option value="S7">S7</option>
|
||||
<option value="TwinCat">TwinCat</option>
|
||||
<option value="Focas">Focas</option>
|
||||
<option value="OpcUaClient">OpcUaClient</option>
|
||||
<option value="Galaxy">Galaxy</option>
|
||||
<option value="Historian.Wonderware">Historian.Wonderware</option>
|
||||
</InputSelect>
|
||||
<div class="form-text">Cannot be changed after creation — drives the actor type that owns this instance.</div>
|
||||
</div>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label" for="ns">Namespace</label>
|
||||
<InputSelect id="ns" @bind-Value="_form.NamespaceId" class="form-select form-select-sm">
|
||||
@foreach (var ns in _namespaces)
|
||||
{
|
||||
<option value="@ns.NamespaceId">@ns.NamespaceId (@ns.Kind)</option>
|
||||
}
|
||||
</InputSelect>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="enabled">Enabled</label>
|
||||
<div class="form-check form-switch">
|
||||
<InputCheckbox id="enabled" @bind-Value="_form.Enabled" class="form-check-input" />
|
||||
<label class="form-check-label" for="enabled">Spawn this driver in deployments</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel rise mt-3" style="animation-delay:.08s">
|
||||
<div class="panel-head">Driver config (JSON)</div>
|
||||
<div style="padding:1rem">
|
||||
<InputTextArea @bind-Value="_form.DriverConfig" rows="12"
|
||||
class="form-control form-control-sm mono"
|
||||
placeholder='{ "endpoint": "10.0.0.42:502", "slaveId": 1 }' />
|
||||
<div class="form-text">Schemaless per driver type — validated server-side at deploy time. JSON is reformatted on save.</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel rise mt-3" style="animation-delay:.14s">
|
||||
<div class="panel-head">Resilience overrides (optional)</div>
|
||||
<div style="padding:1rem">
|
||||
<InputTextArea @bind-Value="_form.ResilienceConfig" rows="6"
|
||||
class="form-control form-control-sm mono"
|
||||
placeholder='Leave blank to use tier defaults' />
|
||||
<div class="form-text">Polly pipeline overrides per docs/v2/driver-stability.md — bulkhead, retry counts, breaker thresholds. Null = use the driver type's tier defaults.</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/drivers" 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? DriverInstanceId { get; set; }
|
||||
|
||||
private bool IsNew => string.IsNullOrEmpty(DriverInstanceId);
|
||||
|
||||
private FormModel _form = new();
|
||||
private DriverInstance? _existing;
|
||||
private List<Namespace> _namespaces = new();
|
||||
private bool _loaded;
|
||||
private bool _busy;
|
||||
private string? _error;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await using var db = await DbFactory.CreateDbContextAsync();
|
||||
_namespaces = await db.Namespaces.AsNoTracking()
|
||||
.Where(n => n.ClusterId == ClusterId)
|
||||
.OrderBy(n => n.NamespaceId)
|
||||
.ToListAsync();
|
||||
|
||||
if (IsNew)
|
||||
{
|
||||
_form = new FormModel
|
||||
{
|
||||
DriverInstanceId = "",
|
||||
Name = "",
|
||||
DriverType = "ModbusTcp",
|
||||
NamespaceId = _namespaces.FirstOrDefault()?.NamespaceId ?? "",
|
||||
Enabled = true,
|
||||
DriverConfig = "{}",
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
_existing = await db.DriverInstances.AsNoTracking()
|
||||
.FirstOrDefaultAsync(d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
|
||||
if (_existing is not null)
|
||||
{
|
||||
_form = new FormModel
|
||||
{
|
||||
DriverInstanceId = _existing.DriverInstanceId,
|
||||
Name = _existing.Name,
|
||||
DriverType = _existing.DriverType,
|
||||
NamespaceId = _existing.NamespaceId,
|
||||
Enabled = _existing.Enabled,
|
||||
DriverConfig = _existing.DriverConfig,
|
||||
ResilienceConfig = _existing.ResilienceConfig,
|
||||
RowVersion = _existing.RowVersion,
|
||||
};
|
||||
}
|
||||
}
|
||||
_loaded = true;
|
||||
}
|
||||
|
||||
private async Task SubmitAsync()
|
||||
{
|
||||
_busy = true;
|
||||
_error = null;
|
||||
try
|
||||
{
|
||||
var normalizedConfig = NormalizeJson(_form.DriverConfig);
|
||||
if (normalizedConfig is null)
|
||||
{
|
||||
_error = "DriverConfig is not valid JSON.";
|
||||
return;
|
||||
}
|
||||
var normalizedResilience = NormalizeOptionalJson(_form.ResilienceConfig);
|
||||
if (!string.IsNullOrWhiteSpace(_form.ResilienceConfig) && normalizedResilience is null)
|
||||
{
|
||||
_error = "ResilienceConfig is not valid JSON. Leave blank to use defaults.";
|
||||
return;
|
||||
}
|
||||
|
||||
await using var db = await DbFactory.CreateDbContextAsync();
|
||||
if (IsNew)
|
||||
{
|
||||
if (await db.DriverInstances.AnyAsync(d => d.DriverInstanceId == _form.DriverInstanceId))
|
||||
{
|
||||
_error = $"Driver instance '{_form.DriverInstanceId}' already exists.";
|
||||
return;
|
||||
}
|
||||
db.DriverInstances.Add(new DriverInstance
|
||||
{
|
||||
DriverInstanceId = _form.DriverInstanceId,
|
||||
ClusterId = ClusterId,
|
||||
NamespaceId = _form.NamespaceId,
|
||||
Name = _form.Name,
|
||||
DriverType = _form.DriverType,
|
||||
Enabled = _form.Enabled,
|
||||
DriverConfig = normalizedConfig,
|
||||
ResilienceConfig = normalizedResilience,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
var entity = await db.DriverInstances.FirstOrDefaultAsync(
|
||||
d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
|
||||
if (entity is null)
|
||||
{
|
||||
_error = "Row no longer exists.";
|
||||
return;
|
||||
}
|
||||
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
|
||||
entity.NamespaceId = _form.NamespaceId;
|
||||
entity.Name = _form.Name;
|
||||
entity.Enabled = _form.Enabled;
|
||||
entity.DriverConfig = normalizedConfig;
|
||||
entity.ResilienceConfig = normalizedResilience;
|
||||
}
|
||||
await db.SaveChangesAsync();
|
||||
Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
_error = "Another user changed this driver instance while you were editing. Reload to see the latest values, then re-apply your changes.";
|
||||
}
|
||||
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.DriverInstances.FirstOrDefaultAsync(
|
||||
d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
|
||||
if (entity is null)
|
||||
{
|
||||
Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
|
||||
return;
|
||||
}
|
||||
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
|
||||
db.DriverInstances.Remove(entity);
|
||||
await db.SaveChangesAsync();
|
||||
Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
_error = "Another user changed this driver instance while you were viewing it. Reload before deleting.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_error = $"Delete failed: {ex.Message}. (Likely because equipment/tags still reference this driver — remove them first.)";
|
||||
}
|
||||
finally
|
||||
{
|
||||
_busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static string? NormalizeJson(string? input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input)) return null;
|
||||
try
|
||||
{
|
||||
using var doc = System.Text.Json.JsonDocument.Parse(input);
|
||||
return System.Text.Json.JsonSerializer.Serialize(doc.RootElement);
|
||||
}
|
||||
catch { return null; }
|
||||
}
|
||||
|
||||
private static string? NormalizeOptionalJson(string? input) =>
|
||||
string.IsNullOrWhiteSpace(input) ? null : NormalizeJson(input);
|
||||
|
||||
private sealed class FormModel
|
||||
{
|
||||
[Required, RegularExpression("^[A-Za-z0-9_-]+$", ErrorMessage = "Use letters, digits, dash, underscore.")]
|
||||
public string DriverInstanceId { get; set; } = "";
|
||||
[Required]
|
||||
public string Name { get; set; } = "";
|
||||
[Required]
|
||||
public string DriverType { get; set; } = "ModbusTcp";
|
||||
[Required]
|
||||
public string NamespaceId { get; set; } = "";
|
||||
public bool Enabled { get; set; } = true;
|
||||
[Required]
|
||||
public string DriverConfig { get; set; } = "{}";
|
||||
public string? ResilienceConfig { get; set; }
|
||||
public byte[] RowVersion { get; set; } = [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
@page "/clusters/{ClusterId}/namespaces/new"
|
||||
@page "/clusters/{ClusterId}/namespaces/{NamespaceId}"
|
||||
@* Live-edit form pattern — one page handles both create (NamespaceId is null) and update.
|
||||
RowVersion is preserved across post-back so EF Core enforces last-write-wins; concurrency
|
||||
conflicts surface as a toast and reload the current row. *@
|
||||
@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
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">@(IsNew ? "New namespace" : "Edit namespace") · <span class="mono">@ClusterId</span></h4>
|
||||
<a href="/clusters/@ClusterId/namespaces" class="btn btn-outline-secondary btn-sm">Cancel</a>
|
||||
</div>
|
||||
|
||||
<ClusterNav ClusterId="@ClusterId" ActiveTab="namespaces" />
|
||||
|
||||
@if (!_loaded)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (!IsNew && _existing is null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Namespace <span class="mono">@NamespaceId</span> was not found in cluster <span class="mono">@ClusterId</span>.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="namespaceEdit">
|
||||
<DataAnnotationsValidator />
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">@(IsNew ? "Identity" : $"Edit {_form.NamespaceId}")</div>
|
||||
<div style="padding:1rem">
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="nsId">NamespaceId</label>
|
||||
<InputText id="nsId" @bind-Value="_form.NamespaceId" disabled="@(!IsNew)"
|
||||
class="form-control form-control-sm mono"
|
||||
placeholder="LINE3-equipment" />
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label" for="kind">Kind</label>
|
||||
<InputSelect id="kind" @bind-Value="_form.Kind" class="form-select form-select-sm">
|
||||
<option value="@NamespaceKind.Equipment">Equipment (raw signals)</option>
|
||||
<option value="@NamespaceKind.SystemPlatform">System Platform (Galaxy / MXAccess)</option>
|
||||
<option value="@NamespaceKind.Simulated">Simulated (replay — reserved)</option>
|
||||
</InputSelect>
|
||||
</div>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label" for="enabled">Enabled</label>
|
||||
<div class="form-check form-switch">
|
||||
<InputCheckbox id="enabled" @bind-Value="_form.Enabled" class="form-check-input" />
|
||||
<label class="form-check-label" for="enabled">Active in deployments</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="uri">NamespaceUri</label>
|
||||
<InputText id="uri" @bind-Value="_form.NamespaceUri" class="form-control form-control-sm mono"
|
||||
placeholder="urn:zb:warsaw-west:equipment" />
|
||||
<div class="form-text">Must be unique fleet-wide. Clients pin discovery here.</div>
|
||||
</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/namespaces" 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? NamespaceId { get; set; }
|
||||
|
||||
private bool IsNew => string.IsNullOrEmpty(NamespaceId);
|
||||
|
||||
private FormModel _form = new();
|
||||
private Namespace? _existing;
|
||||
private bool _loaded;
|
||||
private bool _busy;
|
||||
private string? _error;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (IsNew)
|
||||
{
|
||||
_form = new FormModel
|
||||
{
|
||||
NamespaceId = "",
|
||||
Kind = NamespaceKind.Equipment,
|
||||
NamespaceUri = "",
|
||||
Enabled = true,
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
await using var db = await DbFactory.CreateDbContextAsync();
|
||||
_existing = await db.Namespaces.AsNoTracking()
|
||||
.FirstOrDefaultAsync(n => n.ClusterId == ClusterId && n.NamespaceId == NamespaceId);
|
||||
if (_existing is not null)
|
||||
{
|
||||
_form = new FormModel
|
||||
{
|
||||
NamespaceId = _existing.NamespaceId,
|
||||
Kind = _existing.Kind,
|
||||
NamespaceUri = _existing.NamespaceUri,
|
||||
Enabled = _existing.Enabled,
|
||||
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.Namespaces.AnyAsync(n => n.NamespaceId == _form.NamespaceId))
|
||||
{
|
||||
_error = $"Namespace '{_form.NamespaceId}' already exists.";
|
||||
return;
|
||||
}
|
||||
db.Namespaces.Add(new Namespace
|
||||
{
|
||||
NamespaceId = _form.NamespaceId,
|
||||
ClusterId = ClusterId,
|
||||
Kind = _form.Kind,
|
||||
NamespaceUri = _form.NamespaceUri,
|
||||
Enabled = _form.Enabled,
|
||||
Notes = string.IsNullOrWhiteSpace(_form.Notes) ? null : _form.Notes,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
var entity = await db.Namespaces.FirstOrDefaultAsync(
|
||||
n => n.ClusterId == ClusterId && n.NamespaceId == NamespaceId);
|
||||
if (entity is null)
|
||||
{
|
||||
_error = "Row no longer exists.";
|
||||
return;
|
||||
}
|
||||
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
|
||||
entity.Kind = _form.Kind;
|
||||
entity.NamespaceUri = _form.NamespaceUri;
|
||||
entity.Enabled = _form.Enabled;
|
||||
entity.Notes = string.IsNullOrWhiteSpace(_form.Notes) ? null : _form.Notes;
|
||||
}
|
||||
await db.SaveChangesAsync();
|
||||
Nav.NavigateTo($"/clusters/{ClusterId}/namespaces");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
_error = "Another user changed this namespace while you were editing. Reload to see the latest values, then re-apply your changes.";
|
||||
}
|
||||
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.Namespaces.FirstOrDefaultAsync(
|
||||
n => n.ClusterId == ClusterId && n.NamespaceId == NamespaceId);
|
||||
if (entity is null)
|
||||
{
|
||||
Nav.NavigateTo($"/clusters/{ClusterId}/namespaces");
|
||||
return;
|
||||
}
|
||||
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
|
||||
db.Namespaces.Remove(entity);
|
||||
await db.SaveChangesAsync();
|
||||
Nav.NavigateTo($"/clusters/{ClusterId}/namespaces");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
_error = "Another user changed this namespace while you were viewing it. Reload before deleting.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_error = ex.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FormModel
|
||||
{
|
||||
[Required, RegularExpression("^[A-Za-z0-9_-]+$", ErrorMessage = "Use letters, digits, dash, underscore.")]
|
||||
public string NamespaceId { get; set; } = "";
|
||||
public NamespaceKind Kind { get; set; } = NamespaceKind.Equipment;
|
||||
[Required, RegularExpression("^urn:[A-Za-z0-9_:./-]+$", ErrorMessage = "Use a URN, e.g. urn:zb:warsaw-west:equipment.")]
|
||||
public string NamespaceUri { get; set; } = "";
|
||||
public bool Enabled { get; set; } = true;
|
||||
public string? Notes { get; set; }
|
||||
public byte[] RowVersion { get; set; } = [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user