From 79220b0c5a5d0ad95dee3962ae59630dd870774b Mon Sep 17 00:00:00 2001 From: Arunima George Date: Tue, 10 Sep 2024 12:44:37 +0100 Subject: [PATCH] TD-4703: Fixed unknown error occuring during pagination --- .../Controllers/SearchController.cs | 9 +++------ .../IRoleUserGroupRepository.cs | 8 ++++++++ .../RoleUserGroupRepository.cs | 14 ++++++++++++++ .../ICatalogueService.cs | 8 ++++++++ .../LearningHub.Nhs.Services/CatalogueService.cs | 11 +++++++++++ 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/WebAPI/LearningHub.Nhs.API/Controllers/SearchController.cs b/WebAPI/LearningHub.Nhs.API/Controllers/SearchController.cs index 9d046bc7b..ab4e417d9 100644 --- a/WebAPI/LearningHub.Nhs.API/Controllers/SearchController.cs +++ b/WebAPI/LearningHub.Nhs.API/Controllers/SearchController.cs @@ -257,14 +257,13 @@ private async Task GetSearchResults(SearchRequestModel searchRe continue; } - var roleUserGroups = this.catalogueService.GetRoleUserGroupsForCatalogue(catalogue.NodeId, true); - document.CatalogueUrl = catalogue.Url; document.CatalogueBadgeUrl = catalogue.BadgeUrl; document.CatalogueName = catalogue.Name; if (catalogue.RestrictedAccess) { + var roleUserGroups = await this.catalogueService.GetRoleUserGroupsForCatalogueSearch(catalogue.NodeId, this.CurrentUserId); document.CatalogueRestrictedAccess = catalogue.RestrictedAccess; document.CatalogueHasAccess = roleUserGroups.Any(x => x.UserGroup.UserUserGroup.Any(y => y.UserId == this.CurrentUserId) && (x.RoleId == (int)RoleEnum.Editor || x.RoleId == (int)RoleEnum.LocalAdmin || x.RoleId == (int)RoleEnum.Reader)); @@ -347,8 +346,6 @@ private async Task GetCatalogueSearchResults(Catalogue continue; } - var roleUserGroups = this.catalogueService.GetRoleUserGroupsForCatalogue(catalogue.NodeId, true); - // catalogue.No document.Url = catalogue.Url; document.BannerUrl = catalogue.BannerUrl; @@ -357,6 +354,7 @@ private async Task GetCatalogueSearchResults(Catalogue document.NodePathId = catalogue.NodePathId; if (catalogue.RestrictedAccess) { + var roleUserGroups = await this.catalogueService.GetRoleUserGroupsForCatalogueSearch(catalogue.NodeId, this.CurrentUserId); document.RestrictedAccess = catalogue.RestrictedAccess; document.HasAccess = roleUserGroups.Any(x => x.UserGroup.UserUserGroup.Any(y => y.UserId == this.CurrentUserId) && (x.RoleId == (int)RoleEnum.Editor || x.RoleId == (int)RoleEnum.LocalAdmin || x.RoleId == (int)RoleEnum.Reader)); @@ -419,8 +417,6 @@ private async Task GetAllCatalogueResults(AllCatalo continue; } - var roleUserGroups = this.catalogueService.GetRoleUserGroupsForCatalogue(catalogue.NodeId, true); - // catalogue.No document.Url = catalogue.Url; document.BannerUrl = catalogue.BannerUrl; @@ -430,6 +426,7 @@ private async Task GetAllCatalogueResults(AllCatalo if (catalogue.RestrictedAccess) { + var roleUserGroups = await this.catalogueService.GetRoleUserGroupsForCatalogueSearch(catalogue.NodeId, this.CurrentUserId); document.RestrictedAccess = catalogue.RestrictedAccess; document.HasAccess = roleUserGroups.Any(x => x.UserGroup.UserUserGroup.Any(y => y.UserId == this.CurrentUserId) && (x.RoleId == (int)RoleEnum.Editor || x.RoleId == (int)RoleEnum.LocalAdmin || x.RoleId == (int)RoleEnum.Reader)); diff --git a/WebAPI/LearningHub.Nhs.Repository.Interface/IRoleUserGroupRepository.cs b/WebAPI/LearningHub.Nhs.Repository.Interface/IRoleUserGroupRepository.cs index 0a807c18d..cdffec209 100644 --- a/WebAPI/LearningHub.Nhs.Repository.Interface/IRoleUserGroupRepository.cs +++ b/WebAPI/LearningHub.Nhs.Repository.Interface/IRoleUserGroupRepository.cs @@ -55,5 +55,13 @@ public interface IRoleUserGroupRepository : IGenericRepository /// The userGroupId. /// A list of RoleUserGroupViewModel. Task> GetRoleUserGroupViewModelsByUserId(int userId); + + /// + /// The get all for search. + /// + /// The catalogueNodeId. + /// The userId. + /// The . + Task> GetAllforSearch(int catalogueNodeId, int userId); } } diff --git a/WebAPI/LearningHub.Nhs.Repository/RoleUserGroupRepository.cs b/WebAPI/LearningHub.Nhs.Repository/RoleUserGroupRepository.cs index 906c335cc..74a284696 100644 --- a/WebAPI/LearningHub.Nhs.Repository/RoleUserGroupRepository.cs +++ b/WebAPI/LearningHub.Nhs.Repository/RoleUserGroupRepository.cs @@ -63,6 +63,20 @@ public async Task GetByRoleIdUserGroupIdScopeIdAsync(int roleId, .Where(n => n.Deleted == false); } + /// + /// The get all for Search. + /// + /// The catalogueNodeId. + /// The userId. + /// The . + public async Task> GetAllforSearch(int catalogueNodeId, int userId) + { + return await this.DbContext.RoleUserGroup.Where(rug => rug.Scope.CatalogueNodeId == catalogueNodeId) + .Include(n => n.UserGroup).ThenInclude(u => u.UserUserGroup.Where(p => p.UserId == userId)) + .Include(n => n.Scope).AsNoTracking() + .ToListAsync(); + } + /// /// The get by role id and catalogue id. /// diff --git a/WebAPI/LearningHub.Nhs.Services.Interface/ICatalogueService.cs b/WebAPI/LearningHub.Nhs.Services.Interface/ICatalogueService.cs index d3d8c6122..69dc89908 100644 --- a/WebAPI/LearningHub.Nhs.Services.Interface/ICatalogueService.cs +++ b/WebAPI/LearningHub.Nhs.Services.Interface/ICatalogueService.cs @@ -192,6 +192,14 @@ public interface ICatalogueService /// The roleUserGroups. List GetRoleUserGroupsForCatalogue(int catalogueNodeId, bool includeUser = false); + /// + /// The GetRolesForCatalogueSearch. + /// + /// The catalogueNodeId. + /// The current user. + /// The roleUserGroups. + Task> GetRoleUserGroupsForCatalogueSearch(int catalogueNodeId, int userId); + /// /// The GetLatestCatalogueAccessRequestAsync. /// diff --git a/WebAPI/LearningHub.Nhs.Services/CatalogueService.cs b/WebAPI/LearningHub.Nhs.Services/CatalogueService.cs index 8510c49e1..f6ca53582 100644 --- a/WebAPI/LearningHub.Nhs.Services/CatalogueService.cs +++ b/WebAPI/LearningHub.Nhs.Services/CatalogueService.cs @@ -404,6 +404,17 @@ public List GetRoleUserGroupsForCatalogue(int catalogueNodeId, bo return query.Where(x => x.Scope.CatalogueNodeId == catalogueNodeId).ToList(); } + /// + /// The GetRolesForCatalogueSearch. + /// + /// The catalogueNodeId. + /// The current user. + /// The roleUserGroups. + public async Task> GetRoleUserGroupsForCatalogueSearch(int catalogueNodeId, int userId) + { + return await this.roleUserGroupRepository.GetAllforSearch(catalogueNodeId, userId); + } + /// /// The RequestAccessAsync. ///