11namespace LearningHub . Nhs . WebUI . Services
22{
33 using System ;
4- using System . Collections . Generic ;
54 using System . Net . Http ;
65 using System . Text ;
76 using System . Threading . Tasks ;
8- using elfhHub . Nhs . Models . Common ;
7+ using LearningHub . Nhs . Caching ;
98 using LearningHub . Nhs . Models . Common ;
109 using LearningHub . Nhs . Models . Databricks ;
10+ using LearningHub . Nhs . Models . Extensions ;
1111 using LearningHub . Nhs . Models . Paging ;
12- using LearningHub . Nhs . Models . Validation ;
1312 using LearningHub . Nhs . WebUI . Interfaces ;
13+ using Microsoft . AspNetCore . Http ;
1414 using Microsoft . Extensions . Logging ;
1515 using Newtonsoft . Json ;
1616
1919 /// </summary>
2020 public class ReportService : BaseService < ReportService > , IReportService
2121 {
22+ private readonly ICacheService cacheService ;
23+ private readonly IHttpContextAccessor contextAccessor ;
24+
2225 /// <summary>
2326 /// Initializes a new instance of the <see cref="ReportService"/> class.
2427 /// </summary>
28+ /// <param name="cacheService">The cache service.</param>
29+ /// <param name="contextAccessor">The contextAccessor.</param>
2530 /// <param name="learningHubHttpClient">The Web Api Http Client.</param>
2631 /// <param name="openApiHttpClient">The Open Api Http Client.</param>
2732 /// <param name="logger">logger.</param>
28- public ReportService ( ILearningHubHttpClient learningHubHttpClient , IOpenApiHttpClient openApiHttpClient , ILogger < ReportService > logger )
33+ public ReportService ( ICacheService cacheService , IHttpContextAccessor contextAccessor , ILearningHubHttpClient learningHubHttpClient , IOpenApiHttpClient openApiHttpClient , ILogger < ReportService > logger )
2934 : base ( learningHubHttpClient , openApiHttpClient , logger )
3035 {
36+ this . cacheService = cacheService ;
37+ this . contextAccessor = contextAccessor ;
3138 }
3239
3340 /// <summary>
@@ -36,26 +43,10 @@ public ReportService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpC
3643 /// <returns>The <see cref="T:Task{bool}"/>.</returns>
3744 public async Task < bool > GetReporterPermission ( )
3845 {
39- bool viewmodel = false ;
40-
41- var client = await this . OpenApiHttpClient . GetClientAsync ( ) ;
42-
43- var request = $ "Report/GetReporterPermission";
44- var response = await client . GetAsync ( request ) . ConfigureAwait ( false ) ;
45-
46- if ( response . IsSuccessStatusCode )
47- {
48- var result = response . Content . ReadAsStringAsync ( ) . Result ;
49- viewmodel = JsonConvert . DeserializeObject < bool > ( result ) ;
50- }
51- else if ( response . StatusCode == System . Net . HttpStatusCode . Unauthorized
52- ||
53- response . StatusCode == System . Net . HttpStatusCode . Forbidden )
54- {
55- throw new Exception ( "AccessDenied" ) ;
56- }
57-
58- return viewmodel ;
46+ bool response = false ;
47+ var cacheKey = $ "{ this . contextAccessor . HttpContext . User . Identity . GetCurrentUserId ( ) } :DatabricksReporter";
48+ response = await this . cacheService . GetOrFetchAsync ( cacheKey , this . FetchReporterPermission ) ;
49+ return response ;
5950 }
6051
6152 /// <summary>
@@ -190,5 +181,28 @@ public async Task<ReportHistoryModel> DownloadReport(int reportHistoryId)
190181
191182 return apiResponse ;
192183 }
184+
185+ private async Task < bool > FetchReporterPermission ( )
186+ {
187+ bool viewmodel = false ;
188+ var client = await this . OpenApiHttpClient . GetClientAsync ( ) ;
189+
190+ var request = $ "Report/GetReporterPermission";
191+ var response = await client . GetAsync ( request ) . ConfigureAwait ( false ) ;
192+
193+ if ( response . IsSuccessStatusCode )
194+ {
195+ var result = response . Content . ReadAsStringAsync ( ) . Result ;
196+ viewmodel = JsonConvert . DeserializeObject < bool > ( result ) ;
197+ }
198+ else if ( response . StatusCode == System . Net . HttpStatusCode . Unauthorized
199+ ||
200+ response . StatusCode == System . Net . HttpStatusCode . Forbidden )
201+ {
202+ throw new Exception ( "AccessDenied" ) ;
203+ }
204+
205+ return viewmodel ;
206+ }
193207 }
194208}
0 commit comments