From 5f58cf11639b47fdb3c807b37fbc1a29edc5ea13 Mon Sep 17 00:00:00 2001 From: David Ellams Date: Fri, 9 Jun 2023 13:41:54 -0500 Subject: [PATCH] - Fixed bugs in SetAndActivateCurrentStorageProvider method in ProviderManager making it even more robust with improved error handling etc. - Started adding new GetAllAvatarNames methods to AvatarController in ONode.WebAPI. - Fixed a bug in ResetPassword method in AvatarService where the message was being set in the Result property instead of the Message one. - Removed Connect and Disconnect methods in the Neo4jOASIS Provider. - Re-wrote ActivateProvider and DeActivateProvider methods in Neo4jOASIS Provider fixing a number of bugs and improved error handling/reporting etc. --- .../Managers/ProviderManager.cs | 10 +-- .../Controllers/AvatarController.cs | 29 +++++++++ .../Services/AvatarService.cs | 3 +- .../Neo4jOASIS.cs | 65 ++++++++++++++++--- 4 files changed, 91 insertions(+), 16 deletions(-) diff --git a/NextGenSoftware.OASIS.API.Core/Managers/ProviderManager.cs b/NextGenSoftware.OASIS.API.Core/Managers/ProviderManager.cs index 83ee22c6..c9ee5374 100644 --- a/NextGenSoftware.OASIS.API.Core/Managers/ProviderManager.cs +++ b/NextGenSoftware.OASIS.API.Core/Managers/ProviderManager.cs @@ -328,10 +328,10 @@ public static OASISResult SetAndActivateCurrentStoragePro { OASISResult deactivateProviderResult = DeActivateProvider(CurrentStorageProvider); - if (deactivateProviderResult.IsError) + if (deactivateProviderResult != null && deactivateProviderResult.IsError || deactivateProviderResult == null) { - // result.IsError = true; // TODO: Think its not an error as long as it can activate a provider below? - result.Message = deactivateProviderResult.Message; + result.IsWarning = true; // TODO: Think its not an error as long as it can activate a provider below? + result.Message = deactivateProviderResult != null ? deactivateProviderResult.Message : "Unknown error (deactivateProviderResult was null!)"; } } @@ -341,10 +341,10 @@ public static OASISResult SetAndActivateCurrentStoragePro OASISResult activateProviderResult = ActivateProvider(CurrentStorageProvider); - if (activateProviderResult.IsError) + if (activateProviderResult != null && activateProviderResult.IsError || activateProviderResult == null) { result.IsError = true; - result.Message = activateProviderResult.Message; + result.Message = activateProviderResult != null ? activateProviderResult.Message : "Unknown error (activateProviderResult was null!)"; } if (setGlobally) diff --git a/NextGenSoftware.OASIS.API.ONODE.WebAPI/Controllers/AvatarController.cs b/NextGenSoftware.OASIS.API.ONODE.WebAPI/Controllers/AvatarController.cs index cd90e70c..fae897bc 100644 --- a/NextGenSoftware.OASIS.API.ONODE.WebAPI/Controllers/AvatarController.cs +++ b/NextGenSoftware.OASIS.API.ONODE.WebAPI/Controllers/AvatarController.cs @@ -666,6 +666,7 @@ public async Task>> GetAllAv /// /// [Authorize(AvatarType.Wizard)] + //[Authorize] [HttpGet("get-all-avatars")] public async Task>> GetAll() { @@ -688,6 +689,34 @@ public async Task>> GetAll(Provide return await GetAll(); } + /// + /// Get's all avatars within The OASIS. + /// Only works for logged in & authenticated Wizards (Admins). Use Authenticate endpoint first to obtain a JWT Token. + /// + /// + [Authorize] + [HttpGet("get-all-avatar-names")] + public async Task>> GetAllAvatarNames() + { + return HttpResponseHelper.FormatResponse(await Program.AvatarManager.LoadAllAvatarsAsync()); + } + + /// + /// Get's all avatars within The OASIS. + /// Only works for logged in & authenticated Wizards (Admins). Use Authenticate endpoint first to obtain a JWT Token. + /// Pass in the provider you wish to use. Set the setglobally flag to false for this provider to be used only for this request or true for it to be used for all future requests too. + /// + /// + /// + /// + [Authorize] + [HttpGet("get-all-avatar-names/{providerType}/{setGlobally}")] + public async Task>> GetAllAvatarNames(ProviderType providerType, bool setGlobally = false) + { + GetAndActivateProvider(providerType, setGlobally); + return await GetAll(); + } + /// /// Get's the avatar for the given id. /// Only works for logged in users. Use Authenticate endpoint first to obtain a JWT Token. diff --git a/NextGenSoftware.OASIS.API.ONODE.WebAPI/Services/AvatarService.cs b/NextGenSoftware.OASIS.API.ONODE.WebAPI/Services/AvatarService.cs index eddeae1e..64da6e35 100644 --- a/NextGenSoftware.OASIS.API.ONODE.WebAPI/Services/AvatarService.cs +++ b/NextGenSoftware.OASIS.API.ONODE.WebAPI/Services/AvatarService.cs @@ -348,7 +348,8 @@ public async Task> ResetPassword(ResetPasswordRequest model) return response; } - response.Result = "Password reset successful, you can now login"; + response.Message = "Password reset successful, you can now login"; + response.Result = response.Message; } else ErrorHandling.HandleError(ref response, $"Error occured in ResetPassword loading all avatars. Reason: {avatarsResult.Message}", avatarsResult.DetailedMessage); diff --git a/NextGenSoftware.OASIS.API.Providers.Neo4jOASIS.Aura/Neo4jOASIS.cs b/NextGenSoftware.OASIS.API.Providers.Neo4jOASIS.Aura/Neo4jOASIS.cs index 00f57896..b3d62603 100644 --- a/NextGenSoftware.OASIS.API.Providers.Neo4jOASIS.Aura/Neo4jOASIS.cs +++ b/NextGenSoftware.OASIS.API.Providers.Neo4jOASIS.Aura/Neo4jOASIS.cs @@ -33,8 +33,10 @@ public Neo4jOASIS(string host, string username, string password) Password = password; } - private async Task Connect() + /* + private async Task> ConnectAsync() { + OASISResult try { _driver = GraphDatabase.Driver(Host, AuthTokens.Basic(Username, Password)); @@ -48,21 +50,22 @@ private async Task Connect() return false; } } - private async Task Disconnect() + private async Task DisconnectAsync() { //TODO: Find if there is a disconnect/shutdown function? await _driver.CloseAsync(); _driver = null; - } + }*/ + public override OASISResult ActivateProvider() { OASISResult result = new OASISResult(); try { - Connect().Wait(); - base.ActivateProvider(); - result.Result = true; + _driver = GraphDatabase.Driver(Host, AuthTokens.Basic(Username, Password)); + _driver.VerifyConnectivityAsync().Wait(); + result = base.ActivateProvider(); } catch (Exception ex) { @@ -71,15 +74,16 @@ public override OASISResult ActivateProvider() return result; } - public override OASISResult DeActivateProvider() + + public override async Task> ActivateProviderAsync() { OASISResult result = new OASISResult(); try { - Disconnect().Wait(); - base.DeActivateProvider(); - result.Result = true; + _driver = GraphDatabase.Driver(Host, AuthTokens.Basic(Username, Password)); + await _driver.VerifyConnectivityAsync(); + result = await base.ActivateProviderAsync(); } catch (Exception ex) { @@ -88,6 +92,47 @@ public override OASISResult DeActivateProvider() return result; } + + public override OASISResult DeActivateProvider() + { + OASISResult result = new OASISResult(); + + try + { + if (_driver != null) + _driver.CloseAsync().Wait(); + + _driver = null; + result = base.DeActivateProvider(); + } + catch (Exception ex) + { + ErrorHandling.HandleError(ref result, $"Unknwon error occured whilst dactivating neo4j provider: {ex}"); + } + + return result; + } + + public override async Task> DeActivateProviderAsync() + { + OASISResult result = new OASISResult(); + + try + { + if (_driver != null) + await _driver.CloseAsync(); + + _driver = null; + result = await base.DeActivateProviderAsync(); + } + catch (Exception ex) + { + ErrorHandling.HandleError(ref result, $"Unknwon error occured whilst dactivating neo4j provider: {ex}"); + } + + return result; + } + private static void WithDatabase(SessionConfigBuilder sessionConfigBuilder) { //var neo4jVersion = System.Environment.GetEnvironmentVariable("NEO4J_VERSION") ?? "";