Skip to content

Commit 5eedd65

Browse files
author
Binon
committed
Moved entrolled courses function to openAPI
1 parent 5dab833 commit 5eedd65

File tree

14 files changed

+225
-256
lines changed

14 files changed

+225
-256
lines changed

LearningHub.Nhs.WebUI/Interfaces/IMoodleApiService.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,5 @@ public interface IMoodleApiService
2424
/// <param name="pageNumber">pageNumber.</param>
2525
/// <returns> List of MoodleCourseResponseModel.</returns>
2626
Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesAsync(int currentUserId, int pageNumber);
27-
28-
/// <summary>
29-
/// GetEnrolledCoursesAsync.
30-
/// </summary>
31-
/// <param name="userId">Moodle user id.</param>
32-
/// <param name="courseId">Moodle course id.</param>
33-
/// <param name="pageNumber">pageNumber.</param>
34-
/// <returns> List of MoodleCourseResponseModel.</returns>
35-
Task<MoodleCourseCompletionModel> GetCourseCompletionAsync(int userId, int courseId, int pageNumber);
3627
}
3728
}

LearningHub.Nhs.WebUI/Interfaces/IMoodleHttpClient.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

LearningHub.Nhs.WebUI/Services/DashboardService.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Net.Http;
6-
using System.Runtime.InteropServices.WindowsRuntime;
75
using System.Text;
86
using System.Threading.Tasks;
97
using LearningHub.Nhs.Models.Dashboard;
108
using LearningHub.Nhs.Models.Entities.Analytics;
11-
using LearningHub.Nhs.Models.Entities.Reporting;
129
using LearningHub.Nhs.Models.Moodle.API;
13-
using LearningHub.Nhs.Services.Interface;
1410
using LearningHub.Nhs.WebUI.Interfaces;
1511
using LearningHub.Nhs.WebUI.Models;
1612
using Microsoft.Extensions.Logging;
@@ -21,19 +17,15 @@
2117
/// </summary>
2218
public class DashboardService : BaseService<DashboardService>, IDashboardService
2319
{
24-
private readonly IMoodleHttpClient moodleHttpClient;
25-
2620
/// <summary>
2721
/// Initializes a new instance of the <see cref="DashboardService"/> class.
2822
/// </summary>
2923
/// <param name="learningHubHttpClient">learningHubHttpClient.</param>
3024
/// <param name="openApiHttpClient">The Open Api Http Client.</param>
3125
/// <param name="logger">logger.</param>
32-
/// <param name="moodleHttpClient">MoodleHttpClient.</param>
33-
public DashboardService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient, ILogger<DashboardService> logger, IMoodleHttpClient moodleHttpClient)
26+
public DashboardService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient, ILogger<DashboardService> logger)
3427
: base(learningHubHttpClient, openApiHttpClient, logger)
3528
{
36-
this.moodleHttpClient = moodleHttpClient;
3729
}
3830

3931
/// <summary>
@@ -132,7 +124,7 @@ public async Task<DashboardResourceResponseViewModel> GetResourcesAsync(string d
132124
public async Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesFromMoodleAsync(int currentUserId, int pageNumber)
133125
{
134126
List<MoodleCourseResponseModel> viewmodel = new List<MoodleCourseResponseModel> { };
135-
MoodleApiService moodleApiService = new MoodleApiService(this.moodleHttpClient, this.OpenApiHttpClient);
127+
MoodleApiService moodleApiService = new MoodleApiService(this.OpenApiHttpClient);
136128
viewmodel = await moodleApiService.GetEnrolledCoursesAsync(currentUserId, pageNumber);
137129
return viewmodel;
138130
}
@@ -144,7 +136,7 @@ public async Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesFromMoodleA
144136
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
145137
public async Task<int> GetMoodleUserIdAsync(int currentUserId)
146138
{
147-
MoodleApiService moodleApiService = new MoodleApiService(this.moodleHttpClient, this.OpenApiHttpClient);
139+
MoodleApiService moodleApiService = new MoodleApiService(this.OpenApiHttpClient);
148140
var moodleUserId = await moodleApiService.GetMoodleUserIdByUsernameAsync(currentUserId);
149141
return moodleUserId;
150142
}

LearningHub.Nhs.WebUI/Services/MoodleApiService.cs

Lines changed: 16 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,24 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Net.Http;
6-
using System.Text;
7-
using System.Text.Json;
85
using System.Threading.Tasks;
96
using LearningHub.Nhs.Models.Moodle.API;
10-
using LearningHub.Nhs.Services.Interface;
117
using LearningHub.Nhs.WebUI.Interfaces;
12-
using LearningHub.Nhs.WebUI.Models;
138
using Newtonsoft.Json;
14-
using MoodleCourseCompletionModel = Nhs.Models.Moodle.API.MoodleCourseCompletionModel;
159

1610
/// <summary>
1711
/// MoodleApiService.
1812
/// </summary>
1913
public class MoodleApiService : IMoodleApiService
2014
{
21-
private readonly IMoodleHttpClient moodleHttpClient;
2215
private readonly IOpenApiHttpClient openApiHttpClient;
2316

2417
/// <summary>
2518
/// Initializes a new instance of the <see cref="MoodleApiService"/> class.
2619
/// </summary>
27-
/// <param name="moodleHttpClient">moodleHttpClient.</param>
2820
/// <param name="openApiHttpClient">The Open Api Http Client.</param>
29-
public MoodleApiService(IMoodleHttpClient moodleHttpClient, IOpenApiHttpClient openApiHttpClient)
21+
public MoodleApiService(IOpenApiHttpClient openApiHttpClient)
3022
{
31-
this.moodleHttpClient = moodleHttpClient;
3223
this.openApiHttpClient = openApiHttpClient;
3324
}
3425

@@ -70,89 +61,37 @@ public async Task<int> GetMoodleUserIdByUsernameAsync(int currentUserId)
7061
/// <summary>
7162
/// GetEnrolledCoursesAsync.
7263
/// </summary>
73-
/// <param name="userId">Moodle user id.</param>
64+
/// <param name="currentUserId">Moodle user id.</param>
7465
/// <param name="pageNumber">The page Number.</param>
7566
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
76-
public async Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesAsync(int userId, int pageNumber)
67+
public async Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesAsync(int currentUserId, int pageNumber)
7768
{
78-
List<MoodleCourseResponseModel> viewmodel = new List<MoodleCourseResponseModel> { };
79-
MoodleApiService moodleApiService = new MoodleApiService(this.moodleHttpClient, this.openApiHttpClient);
69+
List<MoodleCourseResponseModel> viewmodel = new List<MoodleCourseResponseModel>();
8070

81-
var client = await this.moodleHttpClient.GetClient();
82-
string additionalParameters = $"userid={userId}";
83-
string defaultParameters = this.moodleHttpClient.GetDefaultParameters();
84-
string url = $"&wsfunction=core_enrol_get_users_courses&{additionalParameters}";
85-
86-
HttpResponseMessage response = await client.GetAsync("?" + defaultParameters + url);
87-
88-
if (response.IsSuccessStatusCode)
71+
try
8972
{
90-
var result = response.Content.ReadAsStringAsync().Result;
73+
var client = await this.openApiHttpClient.GetClientAsync();
9174

92-
using var document = JsonDocument.Parse(result);
93-
var root = document.RootElement;
75+
var request = $"Moodle/GetEnrolledCourses/{currentUserId}";
76+
var response = await client.GetAsync(request).ConfigureAwait(false);
9477

95-
// Check if it's a JSON object and contains "exception"
96-
if (!(root.ValueKind == JsonValueKind.Object && root.TryGetProperty("exception", out _)))
78+
if (response.IsSuccessStatusCode)
9779
{
80+
var result = response.Content.ReadAsStringAsync().Result;
9881
viewmodel = JsonConvert.DeserializeObject<List<MoodleCourseResponseModel>>(result);
99-
100-
foreach (var course in viewmodel)
101-
{
102-
course.CourseCompletionViewModel = await moodleApiService.GetCourseCompletionAsync(userId, course.Id.Value, pageNumber);
103-
}
10482
}
105-
else
83+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden)
10684
{
107-
// Contains error, handle it as needed.
85+
throw new Exception("AccessDenied");
10886
}
109-
}
110-
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
111-
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
112-
{
113-
throw new Exception("AccessDenied");
114-
}
115-
116-
return viewmodel;
117-
}
11887

119-
/// <summary>
120-
/// GetEnrolledCoursesAsync.
121-
/// </summary>
122-
/// <param name="userId">Moodle user id.</param>
123-
/// <param name="courseId">Moodle course id.</param>
124-
/// <param name="pageNumber">pageNumber.</param>
125-
/// <returns> List of MoodleCourseResponseModel.</returns>
126-
public async Task<MoodleCourseCompletionModel> GetCourseCompletionAsync(int userId, int courseId, int pageNumber)
127-
{
128-
MoodleCourseCompletionModel viewmodel = new MoodleCourseCompletionModel { };
129-
MoodleApiService moodleApiService = new MoodleApiService(this.moodleHttpClient, this.openApiHttpClient);
130-
131-
var client = await this.moodleHttpClient.GetClient();
132-
string additionalParameters = $"userid={userId}&courseid={courseId}";
133-
string defaultParameters = this.moodleHttpClient.GetDefaultParameters();
134-
string url = $"&wsfunction=core_completion_get_course_completion_status&{additionalParameters}";
135-
136-
HttpResponseMessage response = await client.GetAsync("?" + defaultParameters + url);
137-
138-
if (response.IsSuccessStatusCode)
139-
{
140-
var result = response.Content.ReadAsStringAsync().Result;
141-
142-
var canViewReport = JsonConvert.DeserializeObject<MoodleCompletionResponseModel>(result);
143-
144-
if (string.IsNullOrEmpty(canViewReport.Exception))
145-
{
146-
viewmodel = JsonConvert.DeserializeObject<MoodleCourseCompletionModel>(result);
147-
}
88+
return viewmodel;
14889
}
149-
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
150-
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
90+
catch (Exception ex)
15191
{
152-
throw new Exception("AccessDenied");
92+
// this.Logger.LogError(string.Format("Error occurred in GetSearchResultAsync: {0}", ex.Message));
93+
return viewmodel;
15394
}
154-
155-
return viewmodel;
15695
}
15796
}
15897
}

LearningHub.Nhs.WebUI/Services/MoodleHttpClient.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.

LearningHub.Nhs.WebUI/Startup/ServiceMappings.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.Net.Http;
44
using GDS.MultiPageFormData;
55
using LearningHub.Nhs.Models.OpenAthens;
6-
using LearningHub.Nhs.Services;
7-
using LearningHub.Nhs.Services.Interface;
86
using LearningHub.Nhs.WebUI.Filters;
97
using LearningHub.Nhs.WebUI.Helpers;
108
using LearningHub.Nhs.WebUI.Interfaces;
@@ -62,21 +60,13 @@ public static void AddLearningHubMappings(this IServiceCollection services, ICon
6260
ServerCertificateCustomValidationCallback =
6361
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
6462
});
65-
services.AddHttpClient<IMoodleHttpClient, MoodleHttpClient>()
66-
.ConfigurePrimaryHttpMessageHandler(
67-
() => new HttpClientHandler
68-
{
69-
ServerCertificateCustomValidationCallback =
70-
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
71-
});
7263
}
7364
else
7465
{
7566
services.AddHttpClient<ILearningHubHttpClient, LearningHubHttpClient>();
7667
services.AddHttpClient<IOpenApiHttpClient, OpenApiHttpClient>();
7768
services.AddHttpClient<IUserApiHttpClient, UserApiHttpClient>();
7869
services.AddHttpClient<ILearningHubReportApiClient, LearningHubReportApiClient>();
79-
services.AddHttpClient<IMoodleHttpClient, MoodleHttpClient>();
8070
}
8171

8272
// Config

LearningHub.Nhs.WebUI/Views/Shared/Tenant/LearningHub/_Layout.cshtml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,15 @@
170170
@functions {
171171
public async Task<bool> DisplayOffline()
172172
{
173-
return false;
174-
175-
// if (ViewContext.RouteData.Values["controller"].ToString() != "Offline" && User.Identity.IsAuthenticated)
176-
// {
177-
// var internalSystem = await this.internalSystemService.GetByIdAsync((int)InternalSystemType.LearningHub);
178-
179-
// return internalSystem.IsOffline;
180-
// }
181-
// else
182-
// {
183-
// return false;
184-
// }
173+
if (ViewContext.RouteData.Values["controller"].ToString() != "Offline" && User.Identity.IsAuthenticated)
174+
{
175+
var internalSystem = await this.internalSystemService.GetByIdAsync((int)InternalSystemType.LearningHub);
176+
177+
return internalSystem.IsOffline;
178+
}
179+
else
180+
{
181+
return false;
182+
}
185183
}
186184
}

0 commit comments

Comments
 (0)