Skip to content

Commit 58da868

Browse files
author
Binon
committed
tidied up some code
1 parent 5eedd65 commit 58da868

File tree

9 files changed

+92
-41
lines changed

9 files changed

+92
-41
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace LearningHub.Nhs.WebUI.Configuration
2+
{
3+
/// <summary>
4+
/// The Moodle Settings.
5+
/// </summary>
6+
public class MoodleApiConfig
7+
{
8+
/// <summary>
9+
/// Gets or sets the base url for the Moodle service.
10+
/// </summary>
11+
public string BaseUrl { get; set; } = null!;
12+
13+
/// <summary>
14+
/// Gets or sets the Web service Rest Format.
15+
/// </summary>
16+
public string MoodleWSRestFormat { get; set; } = null!;
17+
18+
/// <summary>
19+
/// Gets or sets the token.
20+
/// </summary>
21+
public string WSToken { get; set; } = null!;
22+
23+
/// <summary>
24+
/// Gets or sets the token.
25+
/// </summary>
26+
public string ApiPath { get; set; } = "webservice/rest/server.php";
27+
28+
/// <summary>
29+
/// Gets or sets the token.
30+
/// </summary>
31+
public string CoursePath { get; set; } = "course/view.php";
32+
}
33+
}

LearningHub.Nhs.WebUI/Interfaces/IMoodleApiService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,12 @@ 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+
/// GetCourseUrl.
30+
/// </summary>
31+
/// <param name="courseId">course Id. </param>
32+
/// <returns>return course URL.</returns>
33+
string GetCourseUrl(int courseId);
2734
}
2835
}

LearningHub.Nhs.WebUI/ServiceCollectionExtension.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur
7878

7979
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
8080

81+
var moodleApiSettings = configuration.GetSection("MoodleAPIConfig");
82+
services.Configure<MoodleApiConfig>(moodleApiSettings);
83+
8184
var settingsSection = configuration.GetSection("Settings");
8285

8386
services.Configure<Settings>(settingsSection);

LearningHub.Nhs.WebUI/Services/DashboardService.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
/// </summary>
1818
public class DashboardService : BaseService<DashboardService>, IDashboardService
1919
{
20+
private readonly IMoodleApiService moodleApiService;
21+
2022
/// <summary>
2123
/// Initializes a new instance of the <see cref="DashboardService"/> class.
2224
/// </summary>
2325
/// <param name="learningHubHttpClient">learningHubHttpClient.</param>
2426
/// <param name="openApiHttpClient">The Open Api Http Client.</param>
2527
/// <param name="logger">logger.</param>
26-
public DashboardService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient, ILogger<DashboardService> logger)
28+
/// <param name="moodleApiService">MoodleApiService.</param>
29+
public DashboardService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient, ILogger<DashboardService> logger, IMoodleApiService moodleApiService)
2730
: base(learningHubHttpClient, openApiHttpClient, logger)
2831
{
32+
this.moodleApiService = moodleApiService;
2933
}
3034

3135
/// <summary>
@@ -124,8 +128,7 @@ public async Task<DashboardResourceResponseViewModel> GetResourcesAsync(string d
124128
public async Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesFromMoodleAsync(int currentUserId, int pageNumber)
125129
{
126130
List<MoodleCourseResponseModel> viewmodel = new List<MoodleCourseResponseModel> { };
127-
MoodleApiService moodleApiService = new MoodleApiService(this.OpenApiHttpClient);
128-
viewmodel = await moodleApiService.GetEnrolledCoursesAsync(currentUserId, pageNumber);
131+
viewmodel = await this.moodleApiService.GetEnrolledCoursesAsync(currentUserId, pageNumber);
129132
return viewmodel;
130133
}
131134

@@ -136,8 +139,7 @@ public async Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesFromMoodleA
136139
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
137140
public async Task<int> GetMoodleUserIdAsync(int currentUserId)
138141
{
139-
MoodleApiService moodleApiService = new MoodleApiService(this.OpenApiHttpClient);
140-
var moodleUserId = await moodleApiService.GetMoodleUserIdByUsernameAsync(currentUserId);
142+
var moodleUserId = await this.moodleApiService.GetMoodleUserIdByUsernameAsync(currentUserId);
141143
return moodleUserId;
142144
}
143145

LearningHub.Nhs.WebUI/Services/MoodleApiService.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System.Collections.Generic;
55
using System.Threading.Tasks;
66
using LearningHub.Nhs.Models.Moodle.API;
7+
using LearningHub.Nhs.WebUI.Configuration;
78
using LearningHub.Nhs.WebUI.Interfaces;
9+
using Microsoft.Extensions.Options;
810
using Newtonsoft.Json;
911

1012
/// <summary>
@@ -13,14 +15,17 @@
1315
public class MoodleApiService : IMoodleApiService
1416
{
1517
private readonly IOpenApiHttpClient openApiHttpClient;
18+
private readonly MoodleApiConfig configuration;
1619

1720
/// <summary>
1821
/// Initializes a new instance of the <see cref="MoodleApiService"/> class.
1922
/// </summary>
2023
/// <param name="openApiHttpClient">The Open Api Http Client.</param>
21-
public MoodleApiService(IOpenApiHttpClient openApiHttpClient)
24+
/// <param name="configuration">configuration.</param>
25+
public MoodleApiService(IOpenApiHttpClient openApiHttpClient, IOptions<MoodleApiConfig> configuration)
2226
{
2327
this.openApiHttpClient = openApiHttpClient;
28+
this.configuration = configuration.Value;
2429
}
2530

2631
/// <summary>
@@ -93,5 +98,17 @@ public async Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesAsync(int c
9398
return viewmodel;
9499
}
95100
}
101+
102+
/// <summary>
103+
/// GetCourseUrl.
104+
/// </summary>
105+
/// <param name="courseId">course Id. </param>
106+
/// <returns>return course URL.</returns>
107+
public string GetCourseUrl(int courseId)
108+
{
109+
var apiBaseUrl = this.configuration.BaseUrl;
110+
string path = this.configuration.CoursePath;
111+
return $"{apiBaseUrl}{path}?id={courseId}";
112+
}
96113
}
97114
}

LearningHub.Nhs.WebUI/Startup/ServiceMappings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public static void AddLearningHubMappings(this IServiceCollection services, ICon
9898
services.AddSingleton<IRoadMapService, RoadMapService>();
9999
services.AddSingleton<IMyLearningService, MyLearningService>();
100100
services.AddSingleton<IDashboardService, DashboardService>();
101+
services.AddSingleton<IMoodleApiService, MoodleApiService>();
101102
services.AddSingleton<IContentService, ContentService>();
102103
services.AddScoped<IPartialFileUploadService, PartialFileUploadService>();
103104
services.AddScoped<IRoleService, RoleService>();

LearningHub.Nhs.WebUI/Views/Home/_CourseEnrolled.cshtml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
@using LearningHub.Nhs.WebUI.Extensions
44
@using LearningHub.Nhs.WebUI.Helpers
55
@model MoodleCourseResponseModel
6-
@inject Microsoft.Extensions.Configuration.IConfiguration Configuration;
6+
@inject LearningHub.Nhs.WebUI.Interfaces.IMoodleApiService moodleApiService;
77

88
@{
99
bool providerExists = false;
1010
string cardStyle = "card-provider-details--blank";
1111

1212
string GetMoodleCourseUrl(int courseId)
1313
{
14-
var apiBaseUrl = Configuration["MoodleAPIConfig:BaseUrl"];
15-
string path = $"course/view.php";
16-
string returnUrl = $@"{apiBaseUrl}{path}?id={courseId}";
17-
18-
return returnUrl;
14+
return moodleApiService.GetCourseUrl(courseId);
1915
}
2016
}
2117
<div class="nhsuk-card dashboard-card">

LearningHub.Nhs.WebUI/Views/Search/_ResourceSearchResult.cshtml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@model LearningHub.Nhs.WebUI.Models.Search.SearchResultViewModel
2-
@inject Microsoft.Extensions.Configuration.IConfiguration Configuration;
2+
@inject LearningHub.Nhs.WebUI.Interfaces.IMoodleApiService moodleApiService;
33

44
@using System.Linq;
55
@using System.Web;
@@ -28,19 +28,30 @@
2828
&query={searchSignalQueryEncoded}&title={payload?.DocumentFields?.Title}";
2929
}
3030

31-
string GetMoodleCourseUrl(string courseId)
31+
string GetMoodleCourseUrl(string courseIdWithPrefix)
3232
{
33-
var prefix = "M";
34-
if (courseId.StartsWith(prefix))
33+
const string prefix = "M";
34+
35+
if (string.IsNullOrWhiteSpace(courseIdWithPrefix))
36+
{
37+
return string.Empty;
38+
}
39+
40+
if (!courseIdWithPrefix.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
3541
{
36-
courseId = courseId.Replace(prefix, ""); ;
42+
return string.Empty;
3743
}
3844

39-
var apiBaseUrl = Configuration["MoodleAPIConfig:BaseUrl"];
40-
string path = $"course/view.php";
41-
string returnUrl = $@"{apiBaseUrl}{path}?id={courseId}";
45+
var courseIdPart = courseIdWithPrefix.Substring(prefix.Length);
4246

43-
return returnUrl;
47+
if (int.TryParse(courseIdPart, out int courseId))
48+
{
49+
return moodleApiService.GetCourseUrl(courseId);
50+
}
51+
else
52+
{
53+
return string.Empty;
54+
}
4455
}
4556

4657
bool showCatalogueFieldsInResources = ViewBag.ShowCatalogueFieldsInResources == null || ViewBag.ShowCatalogueFieldsInResources == true;

OpenAPI/LearningHub.Nhs.OpenApi/Controllers/MoodleController.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,14 @@
1616
public class MoodleController : Controller
1717
{
1818
private readonly IMoodleApiService moodleService;
19-
private readonly MoodleConfig moodleConfig;
2019

2120
/// <summary>
2221
/// Initializes a new instance of the <see cref="MoodleController"/> class.
2322
/// </summary>
2423
/// <param name="moodleService">The moodle service.</param>
25-
/// <param name="moodleConfig">Moodel config.</param>
26-
public MoodleController(IMoodleApiService moodleService, IOptions<MoodleConfig> moodleConfig)
24+
public MoodleController(IMoodleApiService moodleService)
2725
{
2826
this.moodleService = moodleService;
29-
this.moodleConfig = moodleConfig.Value;
30-
}
31-
32-
/// <summary>
33-
/// The GetSafeMoodleConfig.
34-
/// </summary>
35-
/// <returns>The <see cref="Task{IActionResult}"/>.</returns>
36-
[HttpGet]
37-
[Route("GetSafeMoodleConfig")]
38-
public IActionResult GetSafeMoodleConfig()
39-
{
40-
// Only expose safe config values
41-
return Ok(new
42-
{
43-
apiBaseUrl = this.moodleConfig.ApiBaseUrl,
44-
coursePath = this.moodleConfig.CoursePath,
45-
});
4627
}
4728

4829
/// <summary>

0 commit comments

Comments
 (0)