From 58f4ba88f7d421b03b8c6136a0af6729781929d6 Mon Sep 17 00:00:00 2001 From: sadq Date: Sun, 8 Sep 2024 23:42:38 +0330 Subject: [PATCH] fix clause validator dependency and inject it in search service --- .../Resources.Designer.cs | 204 --------------- .../Resources.resx | 108 -------- .../Resources.Designer.cs | 246 ------------------ RelationshipAnalysis.Test/Resources.resx | 130 --------- .../ClauseValidatorServiceTests.cs | 108 ++++++++ RelationshipAnalysis/Program.cs | 1 + RelationshipAnalysis/Resources.Designer.cs | 80 +++--- .../Abstraction/IClauseValidatorService.cs | 13 + .../GraphServices/ClauseValidatorService.cs | 49 ++++ .../Graph/GraphSearcherService.cs | 168 +++++------- 10 files changed, 277 insertions(+), 830 deletions(-) delete mode 100644 RelationshipAnalysis.Integration.Test/Resources.Designer.cs delete mode 100644 RelationshipAnalysis.Integration.Test/Resources.resx delete mode 100644 RelationshipAnalysis.Test/Resources.Designer.cs delete mode 100644 RelationshipAnalysis.Test/Resources.resx create mode 100644 RelationshipAnalysis.Test/Services/GraphServices/ClauseValidatorServiceTests.cs create mode 100644 RelationshipAnalysis/Services/GraphServices/Abstraction/IClauseValidatorService.cs create mode 100644 RelationshipAnalysis/Services/GraphServices/ClauseValidatorService.cs diff --git a/RelationshipAnalysis.Integration.Test/Resources.Designer.cs b/RelationshipAnalysis.Integration.Test/Resources.Designer.cs deleted file mode 100644 index 434b002..0000000 --- a/RelationshipAnalysis.Integration.Test/Resources.Designer.cs +++ /dev/null @@ -1,204 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace RelationshipAnalysis.Integration.Test { - using System; - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] - [System.Diagnostics.DebuggerNonUserCodeAttribute()] - [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources { - - private static System.Resources.ResourceManager resourceMan; - - private static System.Globalization.CultureInfo resourceCulture; - - [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() { - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Resources.ResourceManager ResourceManager { - get { - if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager("RelationshipAnalysis.Integration.Test.Resources", typeof(Resources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - internal static string ValidFileMessage { - get { - return ResourceManager.GetString("ValidFileMessage", resourceCulture); - } - } - - internal static string FailedAddRecordsMessage { - get { - return ResourceManager.GetString("FailedAddRecordsMessage", resourceCulture); - } - } - - internal static string InvalidHeaderAttribute { - get { - return ResourceManager.GetString("InvalidHeaderAttribute", resourceCulture); - } - } - - internal static string TwoSameHeadersMessage { - get { - return ResourceManager.GetString("TwoSameHeadersMessage", resourceCulture); - } - } - - internal static string InvalidNodeCategory { - get { - return ResourceManager.GetString("InvalidNodeCategory", resourceCulture); - } - } - - internal static string SuccessfulNodeAdditionMessage { - get { - return ResourceManager.GetString("SuccessfulNodeAdditionMessage", resourceCulture); - } - } - - internal static string NoFileUploadedMessage { - get { - return ResourceManager.GetString("NoFileUploadedMessage", resourceCulture); - } - } - - internal static string EmailExistsMessage { - get { - return ResourceManager.GetString("EmailExistsMessage", resourceCulture); - } - } - - internal static string SuccessfulLogoutMessage { - get { - return ResourceManager.GetString("SuccessfulLogoutMessage", resourceCulture); - } - } - - internal static string InvalidPasswordMessage { - get { - return ResourceManager.GetString("InvalidPasswordMessage", resourceCulture); - } - } - - internal static string UserNotFoundMessage { - get { - return ResourceManager.GetString("UserNotFoundMessage", resourceCulture); - } - } - - internal static string WrongOldPasswordMessage { - get { - return ResourceManager.GetString("WrongOldPasswordMessage", resourceCulture); - } - } - - internal static string OldPasswordRequired { - get { - return ResourceManager.GetString("OldPasswordRequired", resourceCulture); - } - } - - internal static string NewPasswordRequired { - get { - return ResourceManager.GetString("NewPasswordRequired", resourceCulture); - } - } - - internal static string UsernameRequired { - get { - return ResourceManager.GetString("UsernameRequired", resourceCulture); - } - } - - internal static string PasswordRequired { - get { - return ResourceManager.GetString("PasswordRequired", resourceCulture); - } - } - - internal static string SuccessfulUpdateUserMessage { - get { - return ResourceManager.GetString("SuccessfulUpdateUserMessage", resourceCulture); - } - } - - internal static string SuccessfulDeleteUserMessage { - get { - return ResourceManager.GetString("SuccessfulDeleteUserMessage", resourceCulture); - } - } - - internal static string UsernameExistsMessage { - get { - return ResourceManager.GetString("UsernameExistsMessage", resourceCulture); - } - } - - internal static string SucceddfulCreateUser { - get { - return ResourceManager.GetString("SucceddfulCreateUser", resourceCulture); - } - } - - internal static string EmptyRolesMessage { - get { - return ResourceManager.GetString("EmptyRolesMessage", resourceCulture); - } - } - - internal static string InvalidRolesListMessage { - get { - return ResourceManager.GetString("InvalidRolesListMessage", resourceCulture); - } - } - - internal static string SuccessfulUpdateRolesMessage { - get { - return ResourceManager.GetString("SuccessfulUpdateRolesMessage", resourceCulture); - } - } - - internal static string LoginFailedMessage { - get { - return ResourceManager.GetString("LoginFailedMessage", resourceCulture); - } - } - - internal static string SuccessfulLoginMessage { - get { - return ResourceManager.GetString("SuccessfulLoginMessage", resourceCulture); - } - } - - internal static string DeleteAccountAccessErrorMessage { - get { - return ResourceManager.GetString("DeleteAccountAccessErrorMessage", resourceCulture); - } - } - } -} diff --git a/RelationshipAnalysis.Integration.Test/Resources.resx b/RelationshipAnalysis.Integration.Test/Resources.resx deleted file mode 100644 index 6f03822..0000000 --- a/RelationshipAnalysis.Integration.Test/Resources.resx +++ /dev/null @@ -1,108 +0,0 @@ - - - - - - File is valid! - - - Add records failed! - - - Entered header is invalid - - - Two headers have the same title! - - - Entered node category is invalid - - - Nodes added successfully - - - No file uploaded! - - - Chosen email already exists - - - Logout was successful! - - - Password must contain one digit from 1 to 9, one lowercase letter, one uppercase letter, one special - character, no space, and it must be 8-16 characters long. - - - - Could not find user! - - - Entered old password is wrong - - - Old password is required - - - New password is required - - - Username is required! - - - Password is required! - - - User updated successfully! - - - User Deleted successfully! - - - - - - - - text/microsoft-resx - - - 1.3 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, - PublicKeyToken=b77a5c561934e089 - - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, - PublicKeyToken=b77a5c561934e089 - - - - Chosen username already exists - - - User Created Successfuly! - - - Roles cant be empty! - - - Roles are invalid! - - - User roles updated successfuly! - - - Login Failed! - - - Login was successful! - - - You cant delete your account! - - \ No newline at end of file diff --git a/RelationshipAnalysis.Test/Resources.Designer.cs b/RelationshipAnalysis.Test/Resources.Designer.cs deleted file mode 100644 index 7b293ca..0000000 --- a/RelationshipAnalysis.Test/Resources.Designer.cs +++ /dev/null @@ -1,246 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace RelationshipAnalysis.Test { - using System; - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] - [System.Diagnostics.DebuggerNonUserCodeAttribute()] - [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources { - - private static System.Resources.ResourceManager resourceMan; - - private static System.Globalization.CultureInfo resourceCulture; - - [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() { - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Resources.ResourceManager ResourceManager { - get { - if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager("RelationshipAnalysis.Test.Resources", typeof(Resources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - internal static string InvalidTargetNodeCategory { - get { - return ResourceManager.GetString("InvalidTargetNodeCategory", resourceCulture); - } - } - - internal static string InvalidSourceNodeCategory { - get { - return ResourceManager.GetString("InvalidSourceNodeCategory", resourceCulture); - } - } - - internal static string InvalidEdgeCategory { - get { - return ResourceManager.GetString("InvalidEdgeCategory", resourceCulture); - } - } - - internal static string SuccessfulEdgeAdditionMessage { - get { - return ResourceManager.GetString("SuccessfulEdgeAdditionMessage", resourceCulture); - } - } - - internal static string ValidFileMessage { - get { - return ResourceManager.GetString("ValidFileMessage", resourceCulture); - } - } - - internal static string FailedAddRecordsMessage { - get { - return ResourceManager.GetString("FailedAddRecordsMessage", resourceCulture); - } - } - - internal static string InvalidHeaderAttribute { - get { - return ResourceManager.GetString("InvalidHeaderAttribute", resourceCulture); - } - } - - internal static string TwoSameHeadersMessage { - get { - return ResourceManager.GetString("TwoSameHeadersMessage", resourceCulture); - } - } - - internal static string InvalidNodeCategory { - get { - return ResourceManager.GetString("InvalidNodeCategory", resourceCulture); - } - } - - internal static string SuccessfulNodeAdditionMessage { - get { - return ResourceManager.GetString("SuccessfulNodeAdditionMessage", resourceCulture); - } - } - - internal static string NoFileUploadedMessage { - get { - return ResourceManager.GetString("NoFileUploadedMessage", resourceCulture); - } - } - - internal static string EmailExistsMessage { - get { - return ResourceManager.GetString("EmailExistsMessage", resourceCulture); - } - } - - internal static string SuccessfulLogoutMessage { - get { - return ResourceManager.GetString("SuccessfulLogoutMessage", resourceCulture); - } - } - - internal static string InvalidPasswordMessage { - get { - return ResourceManager.GetString("InvalidPasswordMessage", resourceCulture); - } - } - - internal static string UserNotFoundMessage { - get { - return ResourceManager.GetString("UserNotFoundMessage", resourceCulture); - } - } - - internal static string WrongOldPasswordMessage { - get { - return ResourceManager.GetString("WrongOldPasswordMessage", resourceCulture); - } - } - - internal static string OldPasswordRequired { - get { - return ResourceManager.GetString("OldPasswordRequired", resourceCulture); - } - } - - internal static string NewPasswordRequired { - get { - return ResourceManager.GetString("NewPasswordRequired", resourceCulture); - } - } - - internal static string UsernameRequired { - get { - return ResourceManager.GetString("UsernameRequired", resourceCulture); - } - } - - internal static string PasswordRequired { - get { - return ResourceManager.GetString("PasswordRequired", resourceCulture); - } - } - - internal static string SuccessfulUpdateUserMessage { - get { - return ResourceManager.GetString("SuccessfulUpdateUserMessage", resourceCulture); - } - } - - internal static string SuccessfulDeleteUserMessage { - get { - return ResourceManager.GetString("SuccessfulDeleteUserMessage", resourceCulture); - } - } - - internal static string UsernameExistsMessage { - get { - return ResourceManager.GetString("UsernameExistsMessage", resourceCulture); - } - } - - internal static string SucceddfulCreateUser { - get { - return ResourceManager.GetString("SucceddfulCreateUser", resourceCulture); - } - } - - internal static string EmptyRolesMessage { - get { - return ResourceManager.GetString("EmptyRolesMessage", resourceCulture); - } - } - - internal static string InvalidRolesListMessage { - get { - return ResourceManager.GetString("InvalidRolesListMessage", resourceCulture); - } - } - - internal static string SuccessfulUpdateRolesMessage { - get { - return ResourceManager.GetString("SuccessfulUpdateRolesMessage", resourceCulture); - } - } - - internal static string LoginFailedMessage { - get { - return ResourceManager.GetString("LoginFailedMessage", resourceCulture); - } - } - - internal static string SuccessfulLoginMessage { - get { - return ResourceManager.GetString("SuccessfulLoginMessage", resourceCulture); - } - } - - internal static string DeleteAccountAccessErrorMessage { - get { - return ResourceManager.GetString("DeleteAccountAccessErrorMessage", resourceCulture); - } - } - - internal static string NullDtoErrorMessage { - get { - return ResourceManager.GetString("NullDtoErrorMessage", resourceCulture); - } - } - - internal static string NotUniqueCategoryNameErrorMessage { - get { - return ResourceManager.GetString("NotUniqueCategoryNameErrorMessage", resourceCulture); - } - } - - internal static string SuccessfulCreateCategory { - get { - return ResourceManager.GetString("SuccessfulCreateCategory", resourceCulture); - } - } - } -} diff --git a/RelationshipAnalysis.Test/Resources.resx b/RelationshipAnalysis.Test/Resources.resx deleted file mode 100644 index a3c6f68..0000000 --- a/RelationshipAnalysis.Test/Resources.resx +++ /dev/null @@ -1,130 +0,0 @@ - - - - - Target Node category is invalid! - - - Source Node category is invalid! - - - Edge category is invalid! - - - Edges added successfully! - - - - - File is valid! - - - Add records failed! - - - Entered header is invalid - - - Two headers have the same title! - - - Entered node category is invalid - - - Nodes added successfully - - - No file uploaded! - - - Chosen email already exists - - - Logout was successful! - - - Password must contain one digit from 1 to 9, one lowercase letter, one uppercase letter, one special - character, no space, and it must be 8-16 characters long. - - - - Could not find user! - - - Entered old password is wrong - - - Old password is required - - - New password is required - - - Username is required! - - - Password is required! - - - User updated successfully! - - - User Deleted successfully! - - - - - - - - text/microsoft-resx - - - 1.3 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, - PublicKeyToken=b77a5c561934e089 - - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, - PublicKeyToken=b77a5c561934e089 - - - - Chosen username already exists - - - User Created Successfuly! - - - Roles cant be empty! - - - Roles are invalid! - - - User roles updated successfuly! - - - Login Failed! - - - Login was successful! - - - You cant delete your account! - - - Input Dto can not be null! - - - Category Name is not unique! - - - Category Created successfylly! - - \ No newline at end of file diff --git a/RelationshipAnalysis.Test/Services/GraphServices/ClauseValidatorServiceTests.cs b/RelationshipAnalysis.Test/Services/GraphServices/ClauseValidatorServiceTests.cs new file mode 100644 index 0000000..c07f75c --- /dev/null +++ b/RelationshipAnalysis.Test/Services/GraphServices/ClauseValidatorServiceTests.cs @@ -0,0 +1,108 @@ + +using RelationshipAnalysis.Dto.Graph; +using RelationshipAnalysis.Enums; +using RelationshipAnalysis.Services.GraphServices; + +namespace RelationshipAnalysis.Test.Services.GraphServices; + +public class ClauseValidatorServiceTests +{ + private readonly ClauseValidatorService _sut; + + public ClauseValidatorServiceTests() + { + _sut = new ClauseValidatorService(); + } + + [Fact] + public async Task AreClausesValid_ShouldReturnSuccess_WhenAllClausesAreValid() + { + // Arrange + var searchGraphDto = new SearchGraphDto + { + SourceCategoryClauses = new Dictionary { { "validSource", "value" } }, + TargetCategoryClauses = new Dictionary { { "validTarget", "value" } }, + EdgeCategoryClauses = new Dictionary { { "validEdge", "value" } } + }; + + var sourceAttributes = new List { "validSource" }; + var targetAttributes = new List { "validTarget" }; + var edgeAttributes = new List { "validEdge" }; + + // Act + var result = await _sut.AreClausesValid(searchGraphDto, sourceAttributes, targetAttributes, edgeAttributes); + + // Assert + Assert.Equal(StatusCodeType.Success, result.StatusCode); + Assert.Null(result.Data); + } + + [Fact] + public async Task AreClausesValid_ShouldReturnNotFound_WhenSourceCategoryClausesAreInvalid() + { + // Arrange + var searchGraphDto = new SearchGraphDto + { + SourceCategoryClauses = new Dictionary { { "invalidSource", "value" } }, + TargetCategoryClauses = new Dictionary { { "validTarget", "value" } }, + EdgeCategoryClauses = new Dictionary { { "validEdge", "value" } } + }; + + var sourceAttributes = new List { "validSource" }; + var targetAttributes = new List { "validTarget" }; + var edgeAttributes = new List { "validEdge" }; + + // Act + var result = await _sut.AreClausesValid(searchGraphDto, sourceAttributes, targetAttributes, edgeAttributes); + + // Assert + Assert.Equal(StatusCodeType.NotFound, result.StatusCode); + Assert.Equal(Resources.InvalidClauseInSourceCategory, result.Data.Message); + } + + [Fact] + public async Task AreClausesValid_ShouldReturnNotFound_WhenTargetCategoryClausesAreInvalid() + { + // Arrange + var searchGraphDto = new SearchGraphDto + { + SourceCategoryClauses = new Dictionary { { "validSource", "value" } }, + TargetCategoryClauses = new Dictionary { { "invalidTarget", "value" } }, + EdgeCategoryClauses = new Dictionary { { "validEdge", "value" } } + }; + + var sourceAttributes = new List { "validSource" }; + var targetAttributes = new List { "validTarget" }; + var edgeAttributes = new List { "validEdge" }; + + // Act + var result = await _sut.AreClausesValid(searchGraphDto, sourceAttributes, targetAttributes, edgeAttributes); + + // Assert + Assert.Equal(StatusCodeType.NotFound, result.StatusCode); + Assert.Equal(Resources.InvalidClauseInDestinationCategory, result.Data.Message); + } + + [Fact] + public async Task AreClausesValid_ShouldReturnNotFound_WhenEdgeCategoryClausesAreInvalid() + { + // Arrange + var searchGraphDto = new SearchGraphDto + { + SourceCategoryClauses = new Dictionary { { "validSource", "value" } }, + TargetCategoryClauses = new Dictionary { { "validTarget", "value" } }, + EdgeCategoryClauses = new Dictionary { { "invalidEdge", "value" } } + }; + + var sourceAttributes = new List { "validSource" }; + var targetAttributes = new List { "validTarget" }; + var edgeAttributes = new List { "validEdge" }; + + // Act + var result = await _sut.AreClausesValid(searchGraphDto, sourceAttributes, targetAttributes, edgeAttributes); + + // Assert + Assert.Equal(StatusCodeType.NotFound, result.StatusCode); + Assert.Equal(Resources.InvalidClauseInDestinationCategory, result.Data.Message); + } +} diff --git a/RelationshipAnalysis/Program.cs b/RelationshipAnalysis/Program.cs index 67d8dda..b565464 100644 --- a/RelationshipAnalysis/Program.cs +++ b/RelationshipAnalysis/Program.cs @@ -116,6 +116,7 @@ .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddKeyedSingleton("node") .AddKeyedSingleton("edge") .AddKeyedSingleton("node") diff --git a/RelationshipAnalysis/Resources.Designer.cs b/RelationshipAnalysis/Resources.Designer.cs index a73de16..2038af7 100644 --- a/RelationshipAnalysis/Resources.Designer.cs +++ b/RelationshipAnalysis/Resources.Designer.cs @@ -14,18 +14,18 @@ namespace RelationshipAnalysis { [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [System.Diagnostics.DebuggerNonUserCodeAttribute()] [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources { + public class Resources { private static System.Resources.ResourceManager resourceMan; private static System.Globalization.CultureInfo resourceCulture; [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() { + public Resources() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Resources.ResourceManager ResourceManager { + public static System.Resources.ResourceManager ResourceManager { get { if (object.Equals(null, resourceMan)) { System.Resources.ResourceManager temp = new System.Resources.ResourceManager("RelationshipAnalysis.Resources", typeof(Resources).Assembly); @@ -36,7 +36,7 @@ internal static System.Resources.ResourceManager ResourceManager { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Globalization.CultureInfo Culture { + public static System.Globalization.CultureInfo Culture { get { return resourceCulture; } @@ -45,211 +45,211 @@ internal static System.Globalization.CultureInfo Culture { } } - internal static string InvalidPasswordMessage { + public static string InvalidPasswordMessage { get { return ResourceManager.GetString("InvalidPasswordMessage", resourceCulture); } } - internal static string InvalidClauseInSourceCategory { + public static string InvalidClauseInSourceCategory { get { return ResourceManager.GetString("InvalidClauseInSourceCategory", resourceCulture); } } - internal static string InvalidClauseInDestinationCategory { + public static string InvalidClauseInDestinationCategory { get { return ResourceManager.GetString("InvalidClauseInDestinationCategory", resourceCulture); } } - internal static string InvalidClauseInEdgeCategory { + public static string InvalidClauseInEdgeCategory { get { return ResourceManager.GetString("InvalidClauseInEdgeCategory", resourceCulture); } } - internal static string SuccessfulLogoutMessage { + public static string SuccessfulLogoutMessage { get { return ResourceManager.GetString("SuccessfulLogoutMessage", resourceCulture); } } - internal static string UserNotFoundMessage { + public static string UserNotFoundMessage { get { return ResourceManager.GetString("UserNotFoundMessage", resourceCulture); } } - internal static string WrongOldPasswordMessage { + public static string WrongOldPasswordMessage { get { return ResourceManager.GetString("WrongOldPasswordMessage", resourceCulture); } } - internal static string NoFileUploadedMessage { + public static string NoFileUploadedMessage { get { return ResourceManager.GetString("NoFileUploadedMessage", resourceCulture); } } - internal static string SuccessfulNodeAdditionMessage { + public static string SuccessfulNodeAdditionMessage { get { return ResourceManager.GetString("SuccessfulNodeAdditionMessage", resourceCulture); } } - internal static string TwoSameHeadersMessage { + public static string TwoSameHeadersMessage { get { return ResourceManager.GetString("TwoSameHeadersMessage", resourceCulture); } } - internal static string InvalidNodeCategory { + public static string InvalidNodeCategory { get { return ResourceManager.GetString("InvalidNodeCategory", resourceCulture); } } - internal static string InvalidHeaderAttribute { + public static string InvalidHeaderAttribute { get { return ResourceManager.GetString("InvalidHeaderAttribute", resourceCulture); } } - internal static string FailedAddRecordsMessage { + public static string FailedAddRecordsMessage { get { return ResourceManager.GetString("FailedAddRecordsMessage", resourceCulture); } } - internal static string ValidFileMessage { + public static string ValidFileMessage { get { return ResourceManager.GetString("ValidFileMessage", resourceCulture); } } - internal static string OldPasswordRequired { + public static string OldPasswordRequired { get { return ResourceManager.GetString("OldPasswordRequired", resourceCulture); } } - internal static string NewPasswordRequired { + public static string NewPasswordRequired { get { return ResourceManager.GetString("NewPasswordRequired", resourceCulture); } } - internal static string UsernameRequired { + public static string UsernameRequired { get { return ResourceManager.GetString("UsernameRequired", resourceCulture); } } - internal static string PasswordRequired { + public static string PasswordRequired { get { return ResourceManager.GetString("PasswordRequired", resourceCulture); } } - internal static string SuccessfulUpdateUserMessage { + public static string SuccessfulUpdateUserMessage { get { return ResourceManager.GetString("SuccessfulUpdateUserMessage", resourceCulture); } } - internal static string SuccessfulDeleteUserMessage { + public static string SuccessfulDeleteUserMessage { get { return ResourceManager.GetString("SuccessfulDeleteUserMessage", resourceCulture); } } - internal static string UsernameExistsMessage { + public static string UsernameExistsMessage { get { return ResourceManager.GetString("UsernameExistsMessage", resourceCulture); } } - internal static string EmailExistsMessage { + public static string EmailExistsMessage { get { return ResourceManager.GetString("EmailExistsMessage", resourceCulture); } } - internal static string SucceddfulCreateUser { + public static string SucceddfulCreateUser { get { return ResourceManager.GetString("SucceddfulCreateUser", resourceCulture); } } - internal static string EmptyRolesMessage { + public static string EmptyRolesMessage { get { return ResourceManager.GetString("EmptyRolesMessage", resourceCulture); } } - internal static string InvalidRolesListMessage { + public static string InvalidRolesListMessage { get { return ResourceManager.GetString("InvalidRolesListMessage", resourceCulture); } } - internal static string SuccessfulUpdateRolesMessage { + public static string SuccessfulUpdateRolesMessage { get { return ResourceManager.GetString("SuccessfulUpdateRolesMessage", resourceCulture); } } - internal static string LoginFailedMessage { + public static string LoginFailedMessage { get { return ResourceManager.GetString("LoginFailedMessage", resourceCulture); } } - internal static string SuccessfulLoginMessage { + public static string SuccessfulLoginMessage { get { return ResourceManager.GetString("SuccessfulLoginMessage", resourceCulture); } } - internal static string DeleteAccountAccessErrorMessage { + public static string DeleteAccountAccessErrorMessage { get { return ResourceManager.GetString("DeleteAccountAccessErrorMessage", resourceCulture); } } - internal static string NullDtoErrorMessage { + public static string NullDtoErrorMessage { get { return ResourceManager.GetString("NullDtoErrorMessage", resourceCulture); } } - internal static string NotUniqueCategoryNameErrorMessage { + public static string NotUniqueCategoryNameErrorMessage { get { return ResourceManager.GetString("NotUniqueCategoryNameErrorMessage", resourceCulture); } } - internal static string SuccessfulCreateCategory { + public static string SuccessfulCreateCategory { get { return ResourceManager.GetString("SuccessfulCreateCategory", resourceCulture); } } - internal static string SuccessfulEdgeAdditionMessage { + public static string SuccessfulEdgeAdditionMessage { get { return ResourceManager.GetString("SuccessfulEdgeAdditionMessage", resourceCulture); } } - internal static string InvalidEdgeCategory { + public static string InvalidEdgeCategory { get { return ResourceManager.GetString("InvalidEdgeCategory", resourceCulture); } } - internal static string InvalidSourceNodeCategory { + public static string InvalidSourceNodeCategory { get { return ResourceManager.GetString("InvalidSourceNodeCategory", resourceCulture); } } - internal static string InvalidTargetNodeCategory { + public static string InvalidTargetNodeCategory { get { return ResourceManager.GetString("InvalidTargetNodeCategory", resourceCulture); } diff --git a/RelationshipAnalysis/Services/GraphServices/Abstraction/IClauseValidatorService.cs b/RelationshipAnalysis/Services/GraphServices/Abstraction/IClauseValidatorService.cs new file mode 100644 index 0000000..1f46dd2 --- /dev/null +++ b/RelationshipAnalysis/Services/GraphServices/Abstraction/IClauseValidatorService.cs @@ -0,0 +1,13 @@ +using RelationshipAnalysis.Dto; +using RelationshipAnalysis.Dto.Graph; + +namespace RelationshipAnalysis.Services.GraphServices.Abstraction; + +public interface IClauseValidatorService +{ + Task> AreClausesValid( + SearchGraphDto searchGraphDto, + List sourceAttributes, + List targetAttributes, + List edgeAttributes); +} \ No newline at end of file diff --git a/RelationshipAnalysis/Services/GraphServices/ClauseValidatorService.cs b/RelationshipAnalysis/Services/GraphServices/ClauseValidatorService.cs new file mode 100644 index 0000000..1996173 --- /dev/null +++ b/RelationshipAnalysis/Services/GraphServices/ClauseValidatorService.cs @@ -0,0 +1,49 @@ +using RelationshipAnalysis.Dto; +using RelationshipAnalysis.Dto.Graph; +using RelationshipAnalysis.Enums; +using RelationshipAnalysis.Services.GraphServices.Abstraction; + +namespace RelationshipAnalysis.Services.GraphServices +{ + public class ClauseValidatorService : IClauseValidatorService + { + public async Task> AreClausesValid( + SearchGraphDto searchGraphDto, + List sourceAttributes, + List targetAttributes, + List edgeAttributes) + { + if (!searchGraphDto.SourceCategoryClauses.Keys.All(item => sourceAttributes.Contains(item))) + return NotFoundResult(Resources.InvalidClauseInSourceCategory); + + if (!searchGraphDto.TargetCategoryClauses.Keys.All(item => targetAttributes.Contains(item))) + return NotFoundResult(Resources.InvalidClauseInDestinationCategory); + + if (!searchGraphDto.EdgeCategoryClauses.Keys.All(item => edgeAttributes.Contains(item))) + return NotFoundResult(Resources.InvalidClauseInDestinationCategory); + + return SuccessResult(); + } + + private ActionResponse SuccessResult() + { + return new ActionResponse + { + StatusCode = StatusCodeType.Success, + Data = null + }; + } + + private ActionResponse NotFoundResult(string message) + { + return new ActionResponse + { + StatusCode = StatusCodeType.NotFound, + Data = new GraphDto + { + Message = message + } + }; + } + } +} \ No newline at end of file diff --git a/RelationshipAnalysis/Services/GraphServices/Graph/GraphSearcherService.cs b/RelationshipAnalysis/Services/GraphServices/Graph/GraphSearcherService.cs index 90b36a6..5101a21 100644 --- a/RelationshipAnalysis/Services/GraphServices/Graph/GraphSearcherService.cs +++ b/RelationshipAnalysis/Services/GraphServices/Graph/GraphSearcherService.cs @@ -6,126 +6,90 @@ using RelationshipAnalysis.Services.GraphServices.Graph.Abstraction; using ApplicationDbContext = RelationshipAnalysis.Context.ApplicationDbContext; -namespace RelationshipAnalysis.Services.GraphServices.Graph; - -public class GraphSearcherService( - IGraphDtoCreator graphDtoCreator, - IServiceProvider serviceProvider, - [FromKeyedServices("node")] IAttributesReceiver nodeCategoryReceiver, - [FromKeyedServices("edge")] IAttributesReceiver edgeCategoryReceiver) : IGraphSearcherService +namespace RelationshipAnalysis.Services.GraphServices.Graph { - public async Task> Search(SearchGraphDto searchGraphDto) + public class GraphSearcherService( + IGraphDtoCreator graphDtoCreator, + IServiceProvider serviceProvider, + [FromKeyedServices("node")] IAttributesReceiver nodeCategoryReceiver, + [FromKeyedServices("edge")] IAttributesReceiver edgeCategoryReceiver, + IClauseValidatorService clauseValidatorService) : IGraphSearcherService { - using var scope = serviceProvider.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - - - var sourceAttributes = await nodeCategoryReceiver.GetAllAttributes(searchGraphDto.SourceCategoryName); - var targetAttributes = await nodeCategoryReceiver.GetAllAttributes(searchGraphDto.TargetCategoryName); - var edgeAttributes = await edgeCategoryReceiver.GetAllAttributes(searchGraphDto.EdgeCategoryName); - var validation = await AreClausesValid(searchGraphDto, sourceAttributes, targetAttributes, edgeAttributes); - if (validation.StatusCode != StatusCodeType.Success) return validation; + public async Task> Search(SearchGraphDto searchGraphDto) + { + using var scope = serviceProvider.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); - var sourceNodes = await context.Nodes - .Where(n => searchGraphDto.SourceCategoryName == n.NodeCategory.NodeCategoryName).ToListAsync(); - var targetNodes = await context.Nodes - .Where(n => searchGraphDto.TargetCategoryName == n.NodeCategory.NodeCategoryName).ToListAsync(); + var sourceAttributes = await nodeCategoryReceiver.GetAllAttributes(searchGraphDto.SourceCategoryName); + var targetAttributes = await nodeCategoryReceiver.GetAllAttributes(searchGraphDto.TargetCategoryName); + var edgeAttributes = await edgeCategoryReceiver.GetAllAttributes(searchGraphDto.EdgeCategoryName); - sourceNodes = sourceNodes.Where(sn => IsNodeValid(sn, searchGraphDto.SourceCategoryClauses)).ToList(); - targetNodes = targetNodes.Where(tn => IsNodeValid(tn, searchGraphDto.TargetCategoryClauses)).ToList(); + var validation = await clauseValidatorService.AreClausesValid(searchGraphDto, sourceAttributes, targetAttributes, edgeAttributes); + if (validation.StatusCode != StatusCodeType.Success) return validation; - var edges = await GetValidEdges(sourceNodes, targetNodes, searchGraphDto.SourceCategoryName, - searchGraphDto.TargetCategoryName, searchGraphDto.EdgeCategoryName); + var sourceNodes = await context.Nodes + .Where(n => searchGraphDto.SourceCategoryName == n.NodeCategory.NodeCategoryName).ToListAsync(); + var targetNodes = await context.Nodes + .Where(n => searchGraphDto.TargetCategoryName == n.NodeCategory.NodeCategoryName).ToListAsync(); - edges = edges.Where(e => IsEdgeValid(e, searchGraphDto.EdgeCategoryClauses)).ToList(); + sourceNodes = sourceNodes.Where(sn => IsNodeValid(sn, searchGraphDto.SourceCategoryClauses)).ToList(); + targetNodes = targetNodes.Where(tn => IsNodeValid(tn, searchGraphDto.TargetCategoryClauses)).ToList(); - validation.Data = graphDtoCreator.CreateResultGraphDto(sourceNodes.Union(targetNodes).ToList(), edges); - return validation; - } + var edges = await GetValidEdges(sourceNodes, targetNodes, searchGraphDto.SourceCategoryName, + searchGraphDto.TargetCategoryName, searchGraphDto.EdgeCategoryName); + edges = edges.Where(e => IsEdgeValid(e, searchGraphDto.EdgeCategoryClauses)).ToList(); - private bool IsNodeValid(Models.Graph.Node.Node node, Dictionary clauses) - { - var attributeValues = new Dictionary(); - node.Values.ToList().ForEach(nv => attributeValues.Add(nv.NodeAttribute.NodeAttributeName, nv.ValueData)); - - foreach (var kvp in clauses) - { - var actualValue = attributeValues[kvp.Key]; - if (!actualValue.StartsWith(kvp.Value)) return false; + validation.Data = graphDtoCreator.CreateResultGraphDto(sourceNodes.Union(targetNodes).ToList(), edges); + return validation; } - return true; - } - - private bool IsEdgeValid(Models.Graph.Edge.Edge edge, Dictionary clauses) - { - var attributeValues = new Dictionary(); - edge.EdgeValues.ToList().ForEach(ev => attributeValues.Add(ev.EdgeAttribute.EdgeAttributeName, ev.ValueData)); - - foreach (var kvp in clauses) + private bool IsNodeValid(Models.Graph.Node.Node node, Dictionary clauses) { - var actualValue = attributeValues[kvp.Key]; - if (!actualValue.StartsWith(kvp.Value)) return false; - } - - return true; - } - - private async Task> GetValidEdges(List sourceNodes, - List targetNodes, string sourceCategory, string targetCategory, - string edgeCategory) - { - var sourceNodeIds = sourceNodes.Select(n => n.NodeId).ToList(); - var targetNodeIds = targetNodes.Select(n => n.NodeId).ToList(); - - using var scope = serviceProvider.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - - var edges = await context.Edges.Include(e => e.EdgeValues) - .ThenInclude(ev => ev.EdgeAttribute) - .Where(e => edgeCategory == e.EdgeCategory.EdgeCategoryName && - sourceNodeIds.Contains(e.EdgeSourceNodeId) && - targetNodeIds.Contains(e.EdgeDestinationNodeId)) - .ToListAsync(); - return edges; - } - - private async Task> AreClausesValid(SearchGraphDto searchGraphDto, - List sourceAttributes, List targetAttributes, - List edgeAttributes) - { - if (!searchGraphDto.SourceCategoryClauses.Keys.All(item => sourceAttributes.Contains(item))) - return NotFoundResult(Resources.InvalidClauseInSourceCategory); + var attributeValues = new Dictionary(); + node.Values.ToList().ForEach(nv => attributeValues.Add(nv.NodeAttribute.NodeAttributeName, nv.ValueData)); - if (!searchGraphDto.TargetCategoryClauses.Keys.All(item => targetAttributes.Contains(item))) - return NotFoundResult(Resources.InvalidClauseInDestinationCategory); - - if (!searchGraphDto.EdgeCategoryClauses.Keys.All(item => edgeAttributes.Contains(item))) - return NotFoundResult(Resources.InvalidClauseInDestinationCategory); + foreach (var kvp in clauses) + { + var actualValue = attributeValues[kvp.Key]; + if (!actualValue.StartsWith(kvp.Value)) return false; + } - return SuccessResult(); - } + return true; + } - private ActionResponse SuccessResult() - { - return new ActionResponse + private bool IsEdgeValid(Models.Graph.Edge.Edge edge, Dictionary clauses) { - StatusCode = StatusCodeType.Success, - Data = null - }; - } + var attributeValues = new Dictionary(); + edge.EdgeValues.ToList().ForEach(ev => attributeValues.Add(ev.EdgeAttribute.EdgeAttributeName, ev.ValueData)); - private ActionResponse NotFoundResult(string message) - { - return new ActionResponse - { - StatusCode = StatusCodeType.NotFound, - Data = new GraphDto + foreach (var kvp in clauses) { - Message = message + var actualValue = attributeValues[kvp.Key]; + if (!actualValue.StartsWith(kvp.Value)) return false; } - }; + + return true; + } + + private async Task> GetValidEdges(List sourceNodes, + List targetNodes, string sourceCategory, string targetCategory, + string edgeCategory) + { + var sourceNodeIds = sourceNodes.Select(n => n.NodeId).ToList(); + var targetNodeIds = targetNodes.Select(n => n.NodeId).ToList(); + + using var scope = serviceProvider.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + + var edges = await context.Edges.Include(e => e.EdgeValues) + .ThenInclude(ev => ev.EdgeAttribute) + .Where(e => edgeCategory == e.EdgeCategory.EdgeCategoryName && + sourceNodeIds.Contains(e.EdgeSourceNodeId) && + targetNodeIds.Contains(e.EdgeDestinationNodeId)) + .ToListAsync(); + return edges; + } } -} \ No newline at end of file +}