|
2 | 2 | { |
3 | 3 | using System; |
4 | 4 | using System.Collections.Generic; |
5 | | - using System.Net.Http; |
6 | | - using System.Text; |
7 | | - using System.Text.Json; |
8 | 5 | using System.Threading.Tasks; |
9 | 6 | using LearningHub.Nhs.Models.Moodle.API; |
10 | | - using LearningHub.Nhs.Services.Interface; |
11 | 7 | using LearningHub.Nhs.WebUI.Interfaces; |
12 | | - using LearningHub.Nhs.WebUI.Models; |
13 | 8 | using Newtonsoft.Json; |
14 | | - using MoodleCourseCompletionModel = Nhs.Models.Moodle.API.MoodleCourseCompletionModel; |
15 | 9 |
|
16 | 10 | /// <summary> |
17 | 11 | /// MoodleApiService. |
18 | 12 | /// </summary> |
19 | 13 | public class MoodleApiService : IMoodleApiService |
20 | 14 | { |
21 | | - private readonly IMoodleHttpClient moodleHttpClient; |
22 | 15 | private readonly IOpenApiHttpClient openApiHttpClient; |
23 | 16 |
|
24 | 17 | /// <summary> |
25 | 18 | /// Initializes a new instance of the <see cref="MoodleApiService"/> class. |
26 | 19 | /// </summary> |
27 | | - /// <param name="moodleHttpClient">moodleHttpClient.</param> |
28 | 20 | /// <param name="openApiHttpClient">The Open Api Http Client.</param> |
29 | | - public MoodleApiService(IMoodleHttpClient moodleHttpClient, IOpenApiHttpClient openApiHttpClient) |
| 21 | + public MoodleApiService(IOpenApiHttpClient openApiHttpClient) |
30 | 22 | { |
31 | | - this.moodleHttpClient = moodleHttpClient; |
32 | 23 | this.openApiHttpClient = openApiHttpClient; |
33 | 24 | } |
34 | 25 |
|
@@ -70,89 +61,37 @@ public async Task<int> GetMoodleUserIdByUsernameAsync(int currentUserId) |
70 | 61 | /// <summary> |
71 | 62 | /// GetEnrolledCoursesAsync. |
72 | 63 | /// </summary> |
73 | | - /// <param name="userId">Moodle user id.</param> |
| 64 | + /// <param name="currentUserId">Moodle user id.</param> |
74 | 65 | /// <param name="pageNumber">The page Number.</param> |
75 | 66 | /// <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) |
77 | 68 | { |
78 | | - List<MoodleCourseResponseModel> viewmodel = new List<MoodleCourseResponseModel> { }; |
79 | | - MoodleApiService moodleApiService = new MoodleApiService(this.moodleHttpClient, this.openApiHttpClient); |
| 69 | + List<MoodleCourseResponseModel> viewmodel = new List<MoodleCourseResponseModel>(); |
80 | 70 |
|
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 |
89 | 72 | { |
90 | | - var result = response.Content.ReadAsStringAsync().Result; |
| 73 | + var client = await this.openApiHttpClient.GetClientAsync(); |
91 | 74 |
|
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); |
94 | 77 |
|
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) |
97 | 79 | { |
| 80 | + var result = response.Content.ReadAsStringAsync().Result; |
98 | 81 | 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 | | - } |
104 | 82 | } |
105 | | - else |
| 83 | + else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden) |
106 | 84 | { |
107 | | - // Contains error, handle it as needed. |
| 85 | + throw new Exception("AccessDenied"); |
108 | 86 | } |
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 | | - } |
118 | 87 |
|
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; |
148 | 89 | } |
149 | | - else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || |
150 | | - response.StatusCode == System.Net.HttpStatusCode.Forbidden) |
| 90 | + catch (Exception ex) |
151 | 91 | { |
152 | | - throw new Exception("AccessDenied"); |
| 92 | + // this.Logger.LogError(string.Format("Error occurred in GetSearchResultAsync: {0}", ex.Message)); |
| 93 | + return viewmodel; |
153 | 94 | } |
154 | | - |
155 | | - return viewmodel; |
156 | 95 | } |
157 | 96 | } |
158 | 97 | } |
0 commit comments