From 901a1f2f4f3b7f596d79e48461a309b729e304af Mon Sep 17 00:00:00 2001 From: "luca.weidmann@gmx.de" Date: Fri, 8 Nov 2024 12:41:24 +0100 Subject: [PATCH] [+] Create App Zones(WIP) #2597 --- .../APIcalls/modelling/addNwAppZone.graphql | 16 ++++ .../FWO.Api.Client/Data/ModellingAppZone.cs | 21 +++++ .../Queries/ModellingQueries.cs | 4 + .../FWO.Services/ModellingAppZoneHandler.cs | 77 +++++++++++++++++++ .../FWO.Services/ModellingHandlerBase.cs | 8 +- roles/ui/files/FWO.UI/Shared/MainLayout.razor | 14 +++- 6 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 roles/lib/files/FWO.Api.Client/APIcalls/modelling/addNwAppZone.graphql create mode 100644 roles/lib/files/FWO.Api.Client/Data/ModellingAppZone.cs create mode 100644 roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs diff --git a/roles/lib/files/FWO.Api.Client/APIcalls/modelling/addNwAppZone.graphql b/roles/lib/files/FWO.Api.Client/APIcalls/modelling/addNwAppZone.graphql new file mode 100644 index 000000000..cc3b0d0f7 --- /dev/null +++ b/roles/lib/files/FWO.Api.Client/APIcalls/modelling/addNwAppZone.graphql @@ -0,0 +1,16 @@ +mutation newAppZone( + $name: String + $idString: String + $creator: String + ) { + insert_modelling_nwgroup(objects: { + name: $name + id_string: $idString + creator: $creator + group_type: 21 + }) { + returning { + newId: id + } + } +} diff --git a/roles/lib/files/FWO.Api.Client/Data/ModellingAppZone.cs b/roles/lib/files/FWO.Api.Client/Data/ModellingAppZone.cs new file mode 100644 index 000000000..822a40c3f --- /dev/null +++ b/roles/lib/files/FWO.Api.Client/Data/ModellingAppZone.cs @@ -0,0 +1,21 @@ +using FWO.Api.Data; + +namespace FWO.Api.Client.Data +{ + public class ModellingAppZone : ModellingAppRole + { + public ModellingAppZone() + { + + } + + public ModellingAppZone(ModellingAppZone appZone) : base(appZone) + { + Comment = appZone.Comment; + Creator = appZone.Creator; + CreationDate = appZone.CreationDate; + AppServers = appZone.AppServers; + Area = appZone.Area; + } + } +} diff --git a/roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs b/roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs index 5a8539dec..df38317fb 100644 --- a/roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs +++ b/roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs @@ -91,6 +91,8 @@ public class ModellingQueries : Queries public static readonly string getHistoryForApp; public static readonly string addHistoryEntry; + public static readonly string newAppZone; + static ModellingQueries() { @@ -183,6 +185,8 @@ static ModellingQueries() getHistory = File.ReadAllText(QueryPath + "modelling/getHistory.graphql"); getHistoryForApp = File.ReadAllText(QueryPath + "modelling/getHistoryForApp.graphql"); addHistoryEntry = File.ReadAllText(QueryPath + "modelling/addHistoryEntry.graphql"); + + newAppZone = File.ReadAllText(QueryPath + "modelling/addNwAppZone.graphql"); } catch (Exception exception) { diff --git a/roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs b/roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs new file mode 100644 index 000000000..693d4f7f9 --- /dev/null +++ b/roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs @@ -0,0 +1,77 @@ +using FWO.Api.Client; +using FWO.Api.Data; +using FWO.Config.Api; +using FWO.Api.Client.Data; +using FWO.Api.Client.Queries; +using System.Text.Json; + +namespace FWO.Services +{ + public class ModellingAppZoneHandler : ModellingHandlerBase + { + public ModellingNamingConvention NamingConvention = new(); + public List AppZones { get; set; } = []; + + public ModellingAppZoneHandler(ApiConnection apiConnection, UserConfig userConfig) : base(apiConnection, userConfig) + { + + } + 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); + + foreach (ModellingAppServer appServer in appServers) + { + ModellingAppZone appZone = new(); + appZone.AppServers.Add(new ModellingAppServerWrapper() { Content = appServer }); + ApplyNamingConvention(owner.ExtAppId.ToUpper(), appZone); + await AddAppZoneToDb(appZone); + } + } + + private void ApplyNamingConvention(string extAppId, ModellingAppZone appZone) + { + NamingConvention = JsonSerializer.Deserialize(userConfig.ModNamingConvention) ?? new(); + appZone.ManagedIdString.NamingConvention = NamingConvention; + appZone.ManagedIdString.SetAppPartFromExtId(extAppId); + appZone.Name = $"{NamingConvention.AppZone}{appZone.ManagedIdString.AppPart}"; + } + + private async Task AddAppZoneToDb(ModellingAppZone appZone) + { + var azVars = new + { + name = appZone.Name, + idString = appZone.IdString, + creator = "CreateAZObjects" + }; + + ReturnId[]? returnIds = ( await apiConnection.SendQueryAsync(ModellingQueries.newAppZone, azVars) ).ReturnIds; + + if (returnIds != null) + { + appZone.Id = returnIds[0].NewId; + + await LogChange(ModellingTypes.ChangeType.Insert, ModellingTypes.ModObjectType.AppZone, appZone.Id, + $"New App Zone: {appZone.Display()}", appZone.AppId); + + foreach (var appServer in appZone.AppServers) + { + var nwobject_nwgroupVars = new + { + nwObjectId = appServer.Content.Id, + nwGroupId = appZone.Id + }; + + await apiConnection.SendQueryAsync(ModellingQueries.addNwObjectToNwGroup, nwobject_nwgroupVars); + + await LogChange(ModellingTypes.ChangeType.Assign, ModellingTypes.ModObjectType.AppRole, appZone.Id, + $"Added App Server {appServer.Content.Display()} to App Role: {appZone.Display()}", Application.Id); + } + } + } + } +} diff --git a/roles/lib/files/FWO.Services/ModellingHandlerBase.cs b/roles/lib/files/FWO.Services/ModellingHandlerBase.cs index b5a368e8c..8fe3da663 100644 --- a/roles/lib/files/FWO.Services/ModellingHandlerBase.cs +++ b/roles/lib/files/FWO.Services/ModellingHandlerBase.cs @@ -39,7 +39,13 @@ public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig, ReadOnly = readOnly; IsOwner = isOwner; } - + + public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig) + { + this.apiConnection = apiConnection; + this.userConfig = userConfig; + } + public MarkupString DisplayButton(string text, string icon, string iconText = "", string objIcon = "") { return DisplayButton(userConfig, text, icon, iconText, objIcon); diff --git a/roles/ui/files/FWO.UI/Shared/MainLayout.razor b/roles/ui/files/FWO.UI/Shared/MainLayout.razor index a226781f1..66073d0e9 100644 --- a/roles/ui/files/FWO.UI/Shared/MainLayout.razor +++ b/roles/ui/files/FWO.UI/Shared/MainLayout.razor @@ -14,6 +14,8 @@ @using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage @using RestSharp @using FWO.Services +@*luca testing*@ +@using FWO.Api.Client.Data @inject ApiConnection apiConnection @inject GlobalConfig globalConfig @@ -31,11 +33,13 @@
@* DON'T DELETE THESE BUTTONS / They might be needed for testing *@ -@* + @* *@ + @*Luca testing *@ + @Body @@ -114,6 +118,14 @@ private Timer? UIMessageQueTimer; + //Luca testing + private async Task test() + { + ModellingAppZone appZone = new(); + ModellingAppZoneHandler appZoneHandler = new(apiConnection, userConfig); + await appZoneHandler.CreateAppZones(5); + } + protected override void OnInitialized() { user = authenticationStateTask!.Result.User;