@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 [Authorize(Policy = AdminUiPolicies.ConfigEditor)] @rendermode RenderMode.InteractiveServer @using Microsoft.AspNetCore.Components.Forms @using Microsoft.EntityFrameworkCore @using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Raw @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 DbFactory @inject NavigationManager Nav @inject AuthenticationStateProvider AuthState Edit cluster · OtOpcUa

Edit cluster · @ClusterId

Cancel
@if (!_loaded) {

Loading…

} else if (_existing is null) {
Cluster @ClusterId was not found.
} else {

Identity

Immutable after creation. Operator-visible everywhere; renames would invalidate every downstream reference.

Topology

@if (!string.IsNullOrWhiteSpace(_error)) {
@_error
}
Cancel
}
@code { [Parameter] public string ClusterId { get; set; } = ""; private FormModel _form = new(); private ServerCluster? _existing; private bool _loaded; private bool _busy; private bool _confirmDeleteVisible; 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, RowVersion = _existing.RowVersion, }; } _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; } // #998 (review C-9): the token loaded with the form, so a stale form cannot overwrite a concurrent edit. db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion; 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 (DbUpdateConcurrencyException) { _error = "Another user changed this cluster while you were editing. Reload the page and re-apply your change."; } 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; } public byte[] RowVersion { get; set; } = []; } }