diff --git a/roles/lib/files/FWO.Api.Client/APIcalls/modelling/addNwAppZone.graphql b/roles/lib/files/FWO.Api.Client/APIcalls/modelling/addNwAppZone.graphql index cc3b0d0f7..2a91fb556 100644 --- a/roles/lib/files/FWO.Api.Client/APIcalls/modelling/addNwAppZone.graphql +++ b/roles/lib/files/FWO.Api.Client/APIcalls/modelling/addNwAppZone.graphql @@ -1,9 +1,11 @@ mutation newAppZone( + $appId: Int! $name: String $idString: String $creator: String ) { insert_modelling_nwgroup(objects: { + app_id: $appId name: $name id_string: $idString creator: $creator diff --git a/roles/lib/files/FWO.Api.Client/APIcalls/modelling/getAppZonesByAppId.graphql b/roles/lib/files/FWO.Api.Client/APIcalls/modelling/getAppZonesByAppId.graphql new file mode 100644 index 000000000..f1c3b9463 --- /dev/null +++ b/roles/lib/files/FWO.Api.Client/APIcalls/modelling/getAppZonesByAppId.graphql @@ -0,0 +1,14 @@ +query getAppZonesByAppId($appId: Int!) { + modelling_nwgroup (where: { group_type: { _eq: 21 }, app_id: { _eq: $appId} } order_by: { name: asc }){ + id + app_id + name + id_string + is_deleted + nwobjects: nwobject_nwgroups{ + owner_network{ + ...appServerDetails + } + } + } +} diff --git a/roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs b/roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs index df38317fb..17ff0419f 100644 --- a/roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs +++ b/roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs @@ -92,7 +92,7 @@ public class ModellingQueries : Queries public static readonly string addHistoryEntry; public static readonly string newAppZone; - + public static readonly string getAppZonesByAppId; static ModellingQueries() { @@ -187,6 +187,7 @@ static ModellingQueries() addHistoryEntry = File.ReadAllText(QueryPath + "modelling/addHistoryEntry.graphql"); newAppZone = File.ReadAllText(QueryPath + "modelling/addNwAppZone.graphql"); + getAppZonesByAppId = appServerDetailsFragment + File.ReadAllText(QueryPath + "modelling/getAppZonesByAppId.graphql"); } catch (Exception exception) { diff --git a/roles/lib/files/FWO.Services/AppServerComparer.cs b/roles/lib/files/FWO.Services/AppServerComparer.cs new file mode 100644 index 000000000..74247c694 --- /dev/null +++ b/roles/lib/files/FWO.Services/AppServerComparer.cs @@ -0,0 +1,25 @@ +using FWO.Api.Data; + +namespace FWO.Services +{ + class AppServerComparer : IEqualityComparer + { + public bool Equals(ModellingAppServerWrapper x, ModellingAppServerWrapper y) + { + if (ReferenceEquals(x, y)) return true; + + if ( x is null || y is null ) + return false; + + return x.Content.Id == y.Content.Id; + } + + public int GetHashCode(ModellingAppServerWrapper appServerWrapper) + { + if ( appServerWrapper is null ) return 0; + int hashProductName = appServerWrapper.Content == null ? 0 : appServerWrapper.Content.GetHashCode(); + int hashProductCode = appServerWrapper.Content.GetHashCode(); + return hashProductName ^ hashProductCode; + } + } +} diff --git a/roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs b/roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs index dc9cb1e9b..f55926005 100644 --- a/roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs +++ b/roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs @@ -4,42 +4,89 @@ using FWO.Api.Client.Data; using FWO.Api.Client.Queries; using System.Text.Json; -using static System.Runtime.InteropServices.JavaScript.JSType; namespace FWO.Services { - public class ModellingAppZoneHandler : ModellingHandlerBase + public class ModellingAppZoneHandler(ApiConnection apiConnection, UserConfig userConfig, Action displayMessageInUi, bool addMode = false) : ModellingHandlerBase(apiConnection, userConfig, displayMessageInUi, addMode) { public ModellingNamingConvention NamingConvention = new(); public List AppZones { get; set; } = []; - public ModellingAppZoneHandler(ApiConnection apiConnection, UserConfig userConfig, Action displayMessageInUi) : - base(apiConnection, userConfig, displayMessageInUi) + public async Task CreateAppZones(string extAppId) { - - } - public async Task CreateAppZones(int appId) - { - List appServers = await apiConnection.SendQueryAsync>(ModellingQueries.getAppServers, new { appId = appId }); List owners = await apiConnection.SendQueryAsync>(OwnerQueries.getOwners); - FwoOwner? owner = owners.FirstOrDefault(_ => _.Id == appId); + FwoOwner? owner = owners.FirstOrDefault(_ => _.ExtAppId == extAppId); if (owner is null) { - string errorMessage = $"{userConfig.GetText("app_owner_not_found")}: App-Id: {appId}"; + string errorMessage = $"{userConfig.GetText("app_owner_not_found")}: External-App-Id: {extAppId}"; Exception exception = new ArgumentException(errorMessage); DisplayMessageInUi(exception, userConfig.GetText("network_modelling"), errorMessage, false); return; } + (bool success, List? existingAppZones) = await GetExistingAppZone(owner.Id); + + if (!success || existingAppZones is null) + return; + + ModellingAppZone appZone; + + if (existingAppZones.Count > 0) + { + appZone = existingAppZones[0]; + } + else + { + appZone = new() + { + AppId = owner.Id, + }; + AddMode = true; + } + + ApplyNamingConvention(owner.ExtAppId.ToUpper(), appZone); + + List appServers = await apiConnection.SendQueryAsync>(ModellingQueries.getAppServers, new { appId = owner.Id }); + List appServerWrappers = []; + foreach (ModellingAppServer appServer in appServers) { - ModellingAppZone appZone = new(); - appZone.AppServers.Add(new ModellingAppServerWrapper() { Content = appServer }); - ApplyNamingConvention(owner.ExtAppId.ToUpper(), appZone); + appServerWrappers.Add(new ModellingAppServerWrapper() { Content = appServer }); + } + + //Only new appservers that are not in the DB are selected + appZone.AppServers = appServerWrappers.Except(appZone.AppServers, new AppServerComparer()).ToList(); + + if (AddMode) + { await AddAppZoneToDb(appZone); } + else + { + //await UpdateAppZoneToDb(appZone); + } + + await AddAppServerToAppZone(appZone); + } + + private async Task<(bool, List?)> GetExistingAppZone(int appId) + { + try + { + List existingAppZones = await apiConnection.SendQueryAsync>(ModellingQueries.getAppZonesByAppId, new { appId = appId }); + + if (existingAppZones is not null) + return (true, existingAppZones); + + } + catch (Exception ex) + { + DisplayMessageInUi(ex, userConfig.GetText("app_zone_creation"), "", true); + } + + return (false, []); } private void ApplyNamingConvention(string extAppId, ModellingAppZone appZone) @@ -50,12 +97,13 @@ private void ApplyNamingConvention(string extAppId, ModellingAppZone appZone) appZone.Name = $"{NamingConvention.AppZone}{appZone.ManagedIdString.AppPart}"; } - private async Task AddAppZoneToDb(ModellingAppZone appZone) + private async Task AddAppZoneToDb(ModellingAppZone appZone) { try { var azVars = new { + appId = appZone.AppId, name = appZone.Name, idString = appZone.IdString, creator = "CreateAZObjects" @@ -63,27 +111,35 @@ private async Task AddAppZoneToDb(ModellingAppZone appZone) ReturnId[]? returnIds = ( await apiConnection.SendQueryAsync(ModellingQueries.newAppZone, azVars) ).ReturnIds; - if (returnIds != null) - { - appZone.Id = returnIds[0].NewId; + if (returnIds != null && returnIds.Length > 0) + return returnIds[0].NewId; + } + catch (Exception ex) + { + DisplayMessageInUi(ex, userConfig.GetText("app_zone_creation"), "", true); + } - foreach (var appServer in appZone.AppServers) - { - var nwobject_nwgroupVars = new - { - nwObjectId = appServer.Content.Id, - nwGroupId = appZone.Id - }; + return -1; + } - await apiConnection.SendQueryAsync(ModellingQueries.addNwObjectToNwGroup, nwobject_nwgroupVars); + private async Task UpdateAppZoneToDb(ModellingAppZone appZone) + { + throw new NotImplementedException(); + } - await LogChange(ModellingTypes.ChangeType.Insert, ModellingTypes.ModObjectType.AppZone, appZone.Id, $"New App Zone: {appZone.Display()}", appZone.AppId); - } - } - } - catch (Exception exception) + private async Task AddAppServerToAppZone(ModellingAppZone appZone) + { + foreach (ModellingAppServerWrapper appServer in appZone.AppServers) { - DisplayMessageInUi(exception, userConfig.GetText("app_zone_creation"), "", true); + var nwobject_nwgroupVars = new + { + nwObjectId = appServer.Content.Id, + nwGroupId = appZone.Id + }; + + await apiConnection.SendQueryAsync(ModellingQueries.addNwObjectToNwGroup, nwobject_nwgroupVars); + + await LogChange(ModellingTypes.ChangeType.Insert, ModellingTypes.ModObjectType.AppZone, appZone.Id, $"New App Zone: {appZone.Display()}", appZone.AppId); } } } diff --git a/roles/lib/files/FWO.Services/ModellingHandlerBase.cs b/roles/lib/files/FWO.Services/ModellingHandlerBase.cs index e04b6c28c..8ed38328e 100644 --- a/roles/lib/files/FWO.Services/ModellingHandlerBase.cs +++ b/roles/lib/files/FWO.Services/ModellingHandlerBase.cs @@ -40,11 +40,12 @@ public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig, IsOwner = isOwner; } - public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig, Action displayMessageInUi) + public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig, Action displayMessageInUi, bool addMode = false) { this.apiConnection = apiConnection; this.userConfig = userConfig; this.DisplayMessageInUi = displayMessageInUi; + this.AddMode = addMode; } public MarkupString DisplayButton(string text, string icon, string iconText = "", string objIcon = "") diff --git a/roles/ui/files/FWO.UI/Shared/MainLayout.razor b/roles/ui/files/FWO.UI/Shared/MainLayout.razor index ff9a0961c..7138edcd8 100644 --- a/roles/ui/files/FWO.UI/Shared/MainLayout.razor +++ b/roles/ui/files/FWO.UI/Shared/MainLayout.razor @@ -123,7 +123,7 @@ { ModellingAppZone appZone = new(); ModellingAppZoneHandler appZoneHandler = new(apiConnection, userConfig, DisplayMessageInUiFunction); - await appZoneHandler.CreateAppZones(5); + await appZoneHandler.CreateAppZones("App-4711"); } protected override void OnInitialized()