Skip to content

Commit 4b0c624

Browse files
committed
Modified permission and navigation check to scoped, making it one request per page load
1 parent 9b600dd commit 4b0c624

File tree

4 files changed

+107
-7
lines changed

4 files changed

+107
-7
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace LearningHub.Nhs.WebUI.Interfaces
2+
{
3+
using System.Security.Claims;
4+
using System.Security.Principal;
5+
using System.Threading.Tasks;
6+
using LearningHub.Nhs.WebUI.Models;
7+
8+
/// <summary>
9+
/// Defines the <see cref="INavigationRequestService" />.
10+
/// </summary>
11+
public interface INavigationRequestService
12+
{
13+
/// <summary>
14+
/// GetNavigationModelAsync.
15+
/// </summary>
16+
/// <param name="user">user.</param>
17+
/// <param name="forceRefresh">forceRefresh.</param>
18+
/// <param name="controllerName">controllerName.</param>
19+
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
20+
Task<NavigationModel> GetNavigationModelAsync(IPrincipal user, bool forceRefresh, string controllerName);
21+
22+
/// <summary>
23+
/// GetNotificationCountAsync.
24+
/// </summary>
25+
/// <param name="userId">userId.</param>
26+
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
27+
Task<int> GetNotificationCountAsync(int userId);
28+
}
29+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace LearningHub.Nhs.WebUI.Services
2+
{
3+
using System.Security.Claims;
4+
using System.Security.Principal;
5+
using System.Threading.Tasks;
6+
using LearningHub.Nhs.WebUI.Interfaces;
7+
using LearningHub.Nhs.WebUI.Models;
8+
9+
/// <summary>
10+
/// NavigationRequestService.
11+
/// </summary>
12+
public class NavigationRequestService : INavigationRequestService
13+
{
14+
private NavigationModel navigationModel;
15+
private int? notificationCount;
16+
17+
private INotificationService notificationService;
18+
private INavigationPermissionService permissionService;
19+
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="NavigationRequestService"/> class.
22+
/// </summary>
23+
/// <param name="permissionService">permissionService.</param>
24+
/// <param name="notificationService">notificationService.</param>
25+
public NavigationRequestService(
26+
INavigationPermissionService permissionService,
27+
INotificationService notificationService)
28+
{
29+
this.permissionService = permissionService;
30+
this.notificationService = notificationService;
31+
}
32+
33+
/// <summary>
34+
/// The GetNavigationModelAsync method.
35+
/// </summary>
36+
/// <param name="user">user.</param>
37+
/// <param name="forceRefresh">forceRefresh.</param>
38+
/// <param name="controllerName">controllerName.</param>
39+
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
40+
public async Task<NavigationModel> GetNavigationModelAsync(
41+
IPrincipal user,
42+
bool forceRefresh,
43+
string controllerName)
44+
{
45+
if (this.navigationModel != null)
46+
{
47+
return this.navigationModel;
48+
}
49+
50+
this.navigationModel = await this.permissionService.GetNavigationModelAsync(user, forceRefresh, controllerName);
51+
return this.navigationModel;
52+
}
53+
54+
/// <summary>
55+
/// The GetNotificationCountAsync.
56+
/// </summary>
57+
/// <param name="userId">userId.</param>
58+
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
59+
public async Task<int> GetNotificationCountAsync(int userId)
60+
{
61+
if (this.notificationCount.HasValue)
62+
{
63+
return this.notificationCount.Value;
64+
}
65+
66+
this.notificationCount = await this.notificationService.GetUserUnreadNotificationCountAsync(userId);
67+
return this.notificationCount.Value;
68+
}
69+
}
70+
}

LearningHub.Nhs.WebUI/Startup/ServiceMappings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public static void AddLearningHubMappings(this IServiceCollection services, ICon
8787
services.AddScoped<ICountryService, CountryService>();
8888
services.AddScoped<IJobRoleService, JobRoleService>();
8989
services.AddScoped<IGradeService, GradeService>();
90+
services.AddScoped<INavigationRequestService, NavigationRequestService>();
9091
services.AddScoped<ISpecialtyService, SpecialtyService>();
9192
services.AddScoped<ILocationService, LocationService>();
9293
services.AddScoped<ITermsAndConditionsService, TermsAndConditionsService>();

LearningHub.Nhs.WebUI/ViewComponents/NavigationItemsViewComponent.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
public class NavigationItemsViewComponent : ViewComponent
1414
{
1515
private readonly ICacheService cacheService;
16-
private INotificationService notificationService;
16+
private INavigationRequestService navigationRequestService;
1717
private INavigationPermissionService permissionService;
1818

1919
/// <summary>
2020
/// Initializes a new instance of the <see cref="NavigationItemsViewComponent"/> class.
2121
/// </summary>
22-
/// <param name="permissionService">Permission service.</param>
23-
/// <param name="notificationService">Notificatin service.</param>
22+
/// <param name="navigationRequestService">navigation Request Service.</param>
23+
/// <param name="permissionService">permissionService.</param>
2424
/// <param name="cacheService">The cacheService<see cref="ICacheService"/>.</param>
25-
public NavigationItemsViewComponent(INavigationPermissionService permissionService, INotificationService notificationService, ICacheService cacheService)
25+
public NavigationItemsViewComponent(INavigationRequestService navigationRequestService, INavigationPermissionService permissionService, ICacheService cacheService)
2626
{
27+
this.navigationRequestService = navigationRequestService;
2728
this.permissionService = permissionService;
28-
this.notificationService = notificationService;
2929
this.cacheService = cacheService;
3030
}
3131

@@ -58,9 +58,9 @@ public async Task<IViewComponentResult> InvokeAsync(string navView = "Default",
5858

5959
var (cacheExists, _) = await this.cacheService.TryGetAsync<string>($"{userId}:LoginWizard");
6060

61-
model = await this.permissionService.GetNavigationModelAsync(this.User, !cacheExists, controllerName);
61+
model = await this.navigationRequestService.GetNavigationModelAsync(this.User, !cacheExists, controllerName);
6262

63-
model.NotificationCount = await this.notificationService.GetUserUnreadNotificationCountAsync(userId);
63+
model.NotificationCount = await this.navigationRequestService.GetNotificationCountAsync(userId);
6464
}
6565

6666
return await Task.FromResult<IViewComponentResult>(this.View(navView, model));

0 commit comments

Comments
 (0)