@@ -6,17 +6,20 @@ namespace LearningHub.Nhs.WebUI.Services
66 using System . Net . Http ;
77 using System . Text ;
88 using System . Threading . Tasks ;
9+ using LearningHub . Nhs . Caching ;
910 using LearningHub . Nhs . Models . Common ;
1011 using LearningHub . Nhs . Models . Entities . Activity ;
1112 using LearningHub . Nhs . Models . Entities . Resource ;
1213 using LearningHub . Nhs . Models . Enums ;
14+ using LearningHub . Nhs . Models . Extensions ;
1315 using LearningHub . Nhs . Models . Hierarchy ;
1416 using LearningHub . Nhs . Models . Resource ;
1517 using LearningHub . Nhs . Models . Resource . Contribute ;
1618 using LearningHub . Nhs . Models . Resource . ResourceDisplay ;
1719 using LearningHub . Nhs . Models . Validation ;
1820 using LearningHub . Nhs . WebUI . Interfaces ;
1921 using LearningHub . Nhs . WebUI . Models ;
22+ using Microsoft . AspNetCore . Http ;
2023 using Microsoft . Extensions . Logging ;
2124 using Microsoft . Extensions . Options ;
2225 using Newtonsoft . Json ;
@@ -29,20 +32,26 @@ public class ResourceService : BaseService<ResourceService>, IResourceService
2932 {
3033 private readonly Settings settings ;
3134 private readonly IAzureMediaService azureMediaService ;
35+ private readonly IHttpContextAccessor contextAccessor ;
36+ private readonly ICacheService cacheService ;
3237
3338 /// <summary>
3439 /// Initializes a new instance of the <see cref="ResourceService"/> class.
3540 /// </summary>
3641 /// <param name="learningHubHttpClient">Learning hub http client.</param>
3742 /// <param name="openApiHttpClient">The Open Api Http Client.</param>
3843 /// <param name="azureMediaService">Azure media services.</param>
44+ /// <param name="contextAccessor">The http context accessor.</param>
45+ /// <param name="cacheService">The cacheService.</param>
3946 /// <param name="logger">Logger.</param>
4047 /// <param name="settings">Settings.</param>
41- public ResourceService ( ILearningHubHttpClient learningHubHttpClient , IOpenApiHttpClient openApiHttpClient , IAzureMediaService azureMediaService , ILogger < ResourceService > logger , IOptions < Settings > settings )
48+ public ResourceService ( ILearningHubHttpClient learningHubHttpClient , IOpenApiHttpClient openApiHttpClient , IAzureMediaService azureMediaService , IHttpContextAccessor contextAccessor , ICacheService cacheService , ILogger < ResourceService > logger , IOptions < Settings > settings )
4249 : base ( learningHubHttpClient , openApiHttpClient , logger )
4350 {
4451 this . settings = settings . Value ;
4552 this . azureMediaService = azureMediaService ;
53+ this . contextAccessor = contextAccessor ;
54+ this . cacheService = cacheService ;
4655 }
4756
4857 /// <summary>
@@ -885,24 +894,8 @@ public async Task<LearningHubValidationResult> UnpublishResourceVersionAsync(int
885894 /// <returns>The <see cref="bool"/>.</returns>
886895 public async Task < bool > UserHasPublishedResourcesAsync ( )
887896 {
888- var client = await this . OpenApiHttpClient . GetClientAsync ( ) ;
889-
890- var request = $ "Resource/HasPublishedResources";
891- var response = await client . GetAsync ( request ) . ConfigureAwait ( false ) ;
892- bool hasResources = false ;
893- if ( response . IsSuccessStatusCode )
894- {
895- var result = response . Content . ReadAsStringAsync ( ) . Result ;
896- hasResources = bool . Parse ( result ) ;
897- }
898- else if ( response . StatusCode == System . Net . HttpStatusCode . Unauthorized
899- ||
900- response . StatusCode == System . Net . HttpStatusCode . Forbidden )
901- {
902- throw new Exception ( "AccessDenied" ) ;
903- }
904-
905- return hasResources ;
897+ var cacheKey = $ "{ this . contextAccessor . HttpContext . User . Identity . GetCurrentUserId ( ) } :UserHasPublishedResources";
898+ return await this . cacheService . GetOrFetchAsync ( "UserHasPublishedResources" , ( ) => this . HasPublishedResources ( ) ) ;
906899 }
907900
908901 /// <summary>
@@ -1340,5 +1333,32 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
13401333
13411334 return filePaths ;
13421335 }
1336+
1337+ /// <summary>
1338+ /// Check if the user has published resources.
1339+ /// </summary>
1340+ /// <returns>The bool.</returns>
1341+ /// <exception cref="Exception"></exception>
1342+ private async Task < bool > HasPublishedResources ( )
1343+ {
1344+ var client = await this . OpenApiHttpClient . GetClientAsync ( ) ;
1345+
1346+ var request = $ "Resource/HasPublishedResources";
1347+ var response = await client . GetAsync ( request ) . ConfigureAwait ( false ) ;
1348+ bool hasResources = false ;
1349+ if ( response . IsSuccessStatusCode )
1350+ {
1351+ var result = response . Content . ReadAsStringAsync ( ) . Result ;
1352+ hasResources = bool . Parse ( result ) ;
1353+ }
1354+ else if ( response . StatusCode == System . Net . HttpStatusCode . Unauthorized
1355+ ||
1356+ response . StatusCode == System . Net . HttpStatusCode . Forbidden )
1357+ {
1358+ throw new Exception ( "AccessDenied" ) ;
1359+ }
1360+
1361+ return hasResources ;
1362+ }
13431363 }
13441364}
0 commit comments