Skip to content

Commit b089d86

Browse files
author
Binon
committed
exception check
1 parent fd2937b commit b089d86

File tree

2 files changed

+9221
-3
lines changed

2 files changed

+9221
-3
lines changed

LearningHub.Nhs.WebUI/Services/MoodleApiService.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Net.Http;
6+
using System.Text.Json;
67
using System.Threading.Tasks;
78
using LearningHub.Nhs.Models.Entities.Reporting;
89
using LearningHub.Nhs.Services.Interface;
@@ -49,11 +50,23 @@ public async Task<List<MoodleCourseResponseViewModel>> GetEnrolledCoursesAsync(i
4950
if (response.IsSuccessStatusCode)
5051
{
5152
var result = response.Content.ReadAsStringAsync().Result;
52-
viewmodel = JsonConvert.DeserializeObject<List<MoodleCourseResponseViewModel>>(result);
5353

54-
foreach (var course in viewmodel)
54+
using var document = JsonDocument.Parse(result);
55+
var root = document.RootElement;
56+
57+
// Check if it's a JSON object and contains "exception"
58+
if (!(root.ValueKind == JsonValueKind.Object && root.TryGetProperty("exception", out _)))
59+
{
60+
viewmodel = JsonConvert.DeserializeObject<List<MoodleCourseResponseViewModel>>(result);
61+
62+
foreach (var course in viewmodel)
63+
{
64+
course.CourseCompletionViewModel = await moodleApiService.GetCourseCompletionAsync(userId, course.Id.Value, pageNumber);
65+
}
66+
}
67+
else
5568
{
56-
course.CourseCompletionViewModel = await moodleApiService.GetCourseCompletionAsync(userId, course.Id.Value, pageNumber);
69+
// Contains error, handle it as needed.
5770
}
5871
}
5972
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||

0 commit comments

Comments
 (0)