Skip to content

Commit b72e356

Browse files
authored
Merge pull request #2694 from TechnologyEnhancedLearning/Develop/feature/TD-3905-RegisterController-refactor
TD-3905-RegisterController - refactor
2 parents a168d2a + 91d66ec commit b72e356

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

DigitalLearningSolutions.Web.Tests/Controllers/Register/RegisterControllerTests.cs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
{
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
5-
using DigitalLearningSolutions.Data.DataServices;
6-
using DigitalLearningSolutions.Data.DataServices.UserDataService;
75
using DigitalLearningSolutions.Data.Enums;
86
using DigitalLearningSolutions.Data.Exceptions;
97
using DigitalLearningSolutions.Data.Models.Register;
@@ -28,27 +26,25 @@ public class RegisterControllerTests
2826
{
2927
private const string IpAddress = "1.1.1.1";
3028
private const int SupervisorDelegateId = 1;
31-
private ICentresDataService centresDataService = null!;
29+
private ICentresService centresService = null!;
3230
private RegisterController controller = null!;
3331
private ICryptoService cryptoService = null!;
3432
private IFeatureManager featureManager = null!;
35-
private IJobGroupsDataService jobGroupsDataService = null!;
33+
private IJobGroupsService jobGroupsService = null!;
3634
private PromptsService promptsService = null!;
3735
private IRegistrationService registrationService = null!;
3836
private HttpRequest request = null!;
3937
private ISupervisorDelegateService supervisorDelegateService = null!;
40-
private IUserDataService userDataService = null!;
4138
private IUserService userService = null!;
4239
private ISupervisorService supervisorService = null!;
4340

4441
[SetUp]
4542
public void Setup()
4643
{
47-
centresDataService = A.Fake<ICentresDataService>();
44+
centresService = A.Fake<ICentresService>();
4845
cryptoService = A.Fake<ICryptoService>();
49-
jobGroupsDataService = A.Fake<IJobGroupsDataService>();
46+
jobGroupsService = A.Fake<IJobGroupsService>();
5047
registrationService = A.Fake<IRegistrationService>();
51-
userDataService = A.Fake<IUserDataService>();
5248
userService = A.Fake<IUserService>();
5349
promptsService = A.Fake<PromptsService>();
5450
featureManager = A.Fake<IFeatureManager>();
@@ -57,14 +53,13 @@ public void Setup()
5753
request = A.Fake<HttpRequest>();
5854

5955
controller = new RegisterController(
60-
centresDataService,
61-
jobGroupsDataService,
56+
centresService,
57+
jobGroupsService,
6258
registrationService,
6359
cryptoService,
6460
promptsService,
6561
featureManager,
6662
supervisorDelegateService,
67-
userDataService,
6863
userService,
6964
supervisorService
7065
)
@@ -104,7 +99,7 @@ public void IndexGet_with_no_centreId_shows_index_page()
10499
var result = controller.Index();
105100

106101
// Then
107-
A.CallTo(() => centresDataService.GetCentreName(A<int>._)).MustNotHaveHappened();
102+
A.CallTo(() => centresService.GetCentreName(A<int>._)).MustNotHaveHappened();
108103

109104
using (new AssertionScope())
110105
{
@@ -120,7 +115,7 @@ public void IndexGet_with_centreId_shows_index_page()
120115
const int centreId = 1;
121116
const string centreName = "centre";
122117
const string inviteId = "invite";
123-
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns(centreName);
118+
A.CallTo(() => centresService.GetCentreName(centreId)).Returns(centreName);
124119

125120
// When
126121
var result = controller.Index(centreId, inviteId);
@@ -139,13 +134,13 @@ public void IndexGet_with_invalid_centreId_param_shows_error()
139134
{
140135
// Given
141136
const int centreId = 7;
142-
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns(null);
137+
A.CallTo(() => centresService.GetCentreName(centreId)).Returns(null);
143138

144139
// When
145140
var result = controller.Index(centreId);
146141

147142
// Then
148-
A.CallTo(() => centresDataService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
143+
A.CallTo(() => centresService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
149144
result.Should().BeNotFoundResult();
150145
}
151146

@@ -156,14 +151,13 @@ public void IndexGet_while_logged_in_redirects_to_register_at_new_centre_journey
156151
const int centreId = 1;
157152
const string inviteId = "invite";
158153
var authenticatedController = new RegisterController(
159-
centresDataService,
160-
jobGroupsDataService,
154+
centresService,
155+
jobGroupsService,
161156
registrationService,
162157
cryptoService,
163158
promptsService,
164159
featureManager,
165160
supervisorDelegateService,
166-
userDataService,
167161
userService,
168162
supervisorService
169163
).WithDefaultContext().WithMockUser(true);
@@ -181,13 +175,13 @@ public void Start_with_invalid_centreId_param_shows_error()
181175
{
182176
// Given
183177
const int centreId = 7;
184-
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns(null);
178+
A.CallTo(() => centresService.GetCentreName(centreId)).Returns(null);
185179

186180
// When
187181
var result = controller.Start(centreId);
188182

189183
// Then
190-
A.CallTo(() => centresDataService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
184+
A.CallTo(() => centresService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
191185
result.Should().BeNotFoundResult();
192186
}
193187

@@ -196,13 +190,13 @@ public void Start_with_valid_centreId_param_sets_data_correctly()
196190
{
197191
// Given
198192
const int centreId = 7;
199-
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns("Some centre");
193+
A.CallTo(() => centresService.GetCentreName(centreId)).Returns("Some centre");
200194

201195
// When
202196
var result = controller.Start(centreId);
203197

204198
// Then
205-
A.CallTo(() => centresDataService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
199+
A.CallTo(() => centresService.GetCentreName(centreId)).MustHaveHappened(1, Times.Exactly);
206200
var data = controller.TempData.Peek<DelegateRegistrationData>()!;
207201
data.Centre.Should().Be(centreId);
208202
data.IsCentreSpecificRegistration.Should().BeTrue();
@@ -216,7 +210,7 @@ public void Start_with_no_centreId_param_allows_normal_registration()
216210
var result = controller.Start();
217211

218212
// Then
219-
A.CallTo(() => centresDataService.GetCentreName(A<int>._)).MustNotHaveHappened();
213+
A.CallTo(() => centresService.GetCentreName(A<int>._)).MustNotHaveHappened();
220214
var data = controller.TempData.Peek<DelegateRegistrationData>()!;
221215
data.Centre.Should().BeNull();
222216
data.IsCentreSpecificRegistration.Should().BeFalse();
@@ -231,19 +225,18 @@ public void Start_while_logged_in_redirects_to_register_at_new_centre_journey()
231225
const string centreName = "centre";
232226
const string inviteId = "invite";
233227
var authenticatedController = new RegisterController(
234-
centresDataService,
235-
jobGroupsDataService,
228+
centresService,
229+
jobGroupsService,
236230
registrationService,
237231
cryptoService,
238232
promptsService,
239233
featureManager,
240234
supervisorDelegateService,
241-
userDataService,
242235
userService,
243236
supervisorService
244237
).WithDefaultContext().WithMockUser(true);
245238

246-
A.CallTo(() => centresDataService.GetCentreName(centreId)).Returns(centreName);
239+
A.CallTo(() => centresService.GetCentreName(centreId)).Returns(centreName);
247240

248241
// When
249242
var result = authenticatedController.Start(centreId, inviteId);
@@ -502,7 +495,7 @@ int userId
502495
);
503496

504497
A.CallTo(
505-
() => userDataService.GetUserIdFromUsername(candidateNumber)
498+
() => userService.GetUserIdFromUsername(candidateNumber)
506499
)
507500
.Returns(userId);
508501
}

DigitalLearningSolutions.Web/Controllers/Register/RegisterController.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ namespace DigitalLearningSolutions.Web.Controllers.Register
33
using System;
44
using System.Collections.Generic;
55
using System.Threading.Tasks;
6-
using DigitalLearningSolutions.Data.DataServices;
7-
using DigitalLearningSolutions.Data.DataServices.UserDataService;
86
using DigitalLearningSolutions.Data.Enums;
97
using DigitalLearningSolutions.Data.Exceptions;
108
using DigitalLearningSolutions.Web.Attributes;
@@ -23,38 +21,35 @@ namespace DigitalLearningSolutions.Web.Controllers.Register
2321
[SetSelectedTab(nameof(NavMenuTab.Register))]
2422
public class RegisterController : Controller
2523
{
26-
private readonly ICentresDataService centresDataService;
24+
private readonly ICentresService centresService;
2725
private readonly ICryptoService cryptoService;
2826
private readonly IFeatureManager featureManager;
29-
private readonly IJobGroupsDataService jobGroupsDataService;
27+
private readonly IJobGroupsService jobGroupsService;
3028
private readonly PromptsService promptsService;
3129
private readonly IRegistrationService registrationService;
3230
private readonly ISupervisorDelegateService supervisorDelegateService;
33-
private readonly IUserDataService userDataService;
3431
private readonly IUserService userService;
3532
private readonly ISupervisorService supervisorService;
3633

3734
public RegisterController(
38-
ICentresDataService centresDataService,
39-
IJobGroupsDataService jobGroupsDataService,
35+
ICentresService centresService,
36+
IJobGroupsService jobGroupsService,
4037
IRegistrationService registrationService,
4138
ICryptoService cryptoService,
4239
PromptsService promptsService,
4340
IFeatureManager featureManager,
4441
ISupervisorDelegateService supervisorDelegateService,
45-
IUserDataService userDataService,
4642
IUserService userService,
4743
ISupervisorService supervisorService
4844
)
4945
{
50-
this.centresDataService = centresDataService;
51-
this.jobGroupsDataService = jobGroupsDataService;
46+
this.centresService = centresService;
47+
this.jobGroupsService = jobGroupsService;
5248
this.registrationService = registrationService;
5349
this.cryptoService = cryptoService;
5450
this.promptsService = promptsService;
5551
this.featureManager = featureManager;
5652
this.supervisorDelegateService = supervisorDelegateService;
57-
this.userDataService = userDataService;
5853
this.supervisorService = supervisorService;
5954
this.userService = userService;
6055
}
@@ -348,7 +343,7 @@ public IActionResult Confirmation(
348343

349344
private string? GetCentreName(int? centreId)
350345
{
351-
return centreId == null ? null : centresDataService.GetCentreName(centreId.Value);
346+
return centreId == null ? null : centresService.GetCentreName(centreId.Value);
352347
}
353348

354349
private IEnumerable<EditDelegateRegistrationPromptViewModel>
@@ -385,9 +380,9 @@ DelegateRegistrationData data
385380

386381
private void PopulatePersonalInformationExtraFields(PersonalInformationViewModel model)
387382
{
388-
model.CentreName = model.Centre.HasValue ? centresDataService.GetCentreName(model.Centre.Value) : null;
383+
model.CentreName = model.Centre.HasValue ? centresService.GetCentreName(model.Centre.Value) : null;
389384
model.CentreOptions = SelectListHelper.MapOptionsToSelectListItems(
390-
centresDataService.GetCentresForDelegateSelfRegistrationAlphabetical(),
385+
centresService.GetCentresForDelegateSelfRegistrationAlphabetical(),
391386
model.Centre
392387
);
393388
}
@@ -400,15 +395,15 @@ DelegateRegistrationData data
400395
model.DelegateRegistrationPrompts =
401396
GetEditDelegateRegistrationPromptViewModelsFromModel(model, data.Centre!.Value);
402397
model.JobGroupOptions = SelectListHelper.MapOptionsToSelectListItems(
403-
jobGroupsDataService.GetJobGroupsAlphabetical(),
398+
jobGroupsService.GetJobGroupsAlphabetical(),
404399
model.JobGroup
405400
);
406401
}
407402

408403
private void PopulateSummaryExtraFields(SummaryViewModel model, DelegateRegistrationData data)
409404
{
410-
model.Centre = centresDataService.GetCentreName((int)data.Centre!);
411-
model.JobGroup = jobGroupsDataService.GetJobGroupName((int)data.JobGroup!);
405+
model.Centre = centresService.GetCentreName((int)data.Centre!);
406+
model.JobGroup = jobGroupsService.GetJobGroupName((int)data.JobGroup!);
412407
model.DelegateRegistrationPrompts = GetDelegateRegistrationPromptsFromData(data);
413408
}
414409

DigitalLearningSolutions.Web/Services/UserService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ string registrationConfirmationHash
142142
int? GetUserIdByAdminId(int adminId);
143143
AdminUser? GetAdminUserByEmailAddress(string emailAddress);
144144
DelegateAccount? GetDelegateAccountById(int id);
145+
int? GetUserIdFromUsername(string username);
145146
}
146147

147148
public class UserService : IUserService
@@ -785,5 +786,10 @@ public bool CentreSpecificEmailIsInUseAtCentre(string email, int centreId)
785786
{
786787
return userDataService.GetDelegateAccountById(id);
787788
}
789+
790+
public int? GetUserIdFromUsername(string username)
791+
{
792+
return userDataService.GetUserIdFromUsername(username);
793+
}
788794
}
789795
}

0 commit comments

Comments
 (0)