Skip to content

Commit 2e722eb

Browse files
authored
Merge pull request #2714 from TechnologyEnhancedLearning/Develop/Features/TD-3946-Helper/Servicefilter/Viewcomponent/Viewmodel-refactor-(Service-Repositorypattern)
TD-3946 helper/servicefilter/viewcomponent/viewmodel refactor (service repositorypattern)
2 parents 7364819 + 971facc commit 2e722eb

File tree

11 files changed

+62
-59
lines changed

11 files changed

+62
-59
lines changed

DigitalLearningSolutions.Web.Tests/ServiceFilter/VerifyAdminUserCanAccessAdminUserTests.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace DigitalLearningSolutions.Web.Tests.ServiceFilter
22
{
3-
using System.Collections.Generic;
4-
using DigitalLearningSolutions.Data.DataServices.UserDataService;
3+
using System.Collections.Generic;
54
using DigitalLearningSolutions.Web.Controllers;
65
using DigitalLearningSolutions.Web.ServiceFilter;
76
using DigitalLearningSolutions.Web.Services;
@@ -19,7 +18,7 @@
1918

2019
internal class VerifyAdminUserCanAccessAdminUserTests
2120
{
22-
private readonly IUserDataService userDataService = A.Fake<IUserDataService>();
21+
private readonly IUserService userService = A.Fake<IUserService>();
2322
private ActionExecutingContext context = null!;
2423
private const int AdminId = 2;
2524

@@ -45,11 +44,11 @@ public void Returns_NotFound_if_service_returns_null()
4544
{
4645
// Given
4746
context.RouteData.Values["adminId"] = AdminId;
48-
A.CallTo(() => userDataService.GetAdminUserById(AdminId))
47+
A.CallTo(() => userService.GetAdminUserById(AdminId))
4948
.Returns(null);
5049

5150
// When
52-
new VerifyAdminUserCanAccessAdminUser(userDataService).OnActionExecuting(context);
51+
new VerifyAdminUserCanAccessAdminUser(userService).OnActionExecuting(context);
5352

5453
// Then
5554
context.Result.Should().BeStatusCodeResult().WithStatusCode(410);
@@ -60,11 +59,11 @@ public void Returns_AccessDenied_if_service_returns_admin_user_at_different_cent
6059
{
6160
// Given
6261
context.RouteData.Values["adminId"] = AdminId;
63-
A.CallTo(() => userDataService.GetAdminUserById(AdminId))
62+
A.CallTo(() => userService.GetAdminUserById(AdminId))
6463
.Returns(UserTestHelper.GetDefaultAdminUser(centreId: 100));
6564

6665
// When
67-
new VerifyAdminUserCanAccessAdminUser(userDataService).OnActionExecuting(context);
66+
new VerifyAdminUserCanAccessAdminUser(userService).OnActionExecuting(context);
6867

6968
// Then
7069
context.Result.Should().BeRedirectToActionResult().WithControllerName("LearningSolutions")
@@ -76,11 +75,11 @@ public void Does_not_return_any_redirect_page_if_service_returns_admin_user_at_s
7675
{
7776
// Given
7877
context.RouteData.Values["adminId"] = AdminId;
79-
A.CallTo(() => userDataService.GetAdminUserById(AdminId))
78+
A.CallTo(() => userService.GetAdminUserById(AdminId))
8079
.Returns(UserTestHelper.GetDefaultAdminUser(centreId: 101));
8180

8281
// When
83-
new VerifyAdminUserCanAccessAdminUser(userDataService).OnActionExecuting(context);
82+
new VerifyAdminUserCanAccessAdminUser(userService).OnActionExecuting(context);
8483

8584
// Then
8685
context.Result.Should().BeNull();

DigitalLearningSolutions.Web.Tests/ServiceFilter/VerifyAdminUserCanAccessDelegateUserTests.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace DigitalLearningSolutions.Web.Tests.ServiceFilter
22
{
3-
using System.Collections.Generic;
4-
using DigitalLearningSolutions.Data.DataServices.UserDataService;
3+
using System.Collections.Generic;
54
using DigitalLearningSolutions.Web.Controllers;
65
using DigitalLearningSolutions.Web.ServiceFilter;
76
using DigitalLearningSolutions.Web.Services;
@@ -19,7 +18,7 @@
1918

2019
internal class VerifyAdminUserCanAccessDelegateUserTests
2120
{
22-
private readonly IUserDataService userDataService = A.Fake<IUserDataService>();
21+
private readonly IUserService userService = A.Fake<IUserService>();
2322
private ActionExecutingContext context = null!;
2423
private const int DelegateId = 2;
2524

@@ -45,11 +44,11 @@ public void Returns_NotFound_if_service_returns_null()
4544
{
4645
// Given
4746
context.RouteData.Values["delegateId"] = DelegateId;
48-
A.CallTo(() => userDataService.GetDelegateUserById(DelegateId))
47+
A.CallTo(() => userService.GetDelegateUserById(DelegateId))
4948
.Returns(null);
5049

5150
// When
52-
new VerifyAdminUserCanAccessDelegateUser(userDataService).OnActionExecuting(context);
51+
new VerifyAdminUserCanAccessDelegateUser(userService).OnActionExecuting(context);
5352

5453
// Then
5554
context.Result.Should().BeNotFoundResult();
@@ -60,11 +59,11 @@ public void Returns_AccessDenied_if_service_returns_delegate_user_at_different_c
6059
{
6160
// Given
6261
context.RouteData.Values["delegateId"] = DelegateId;
63-
A.CallTo(() => userDataService.GetDelegateUserById(DelegateId))
62+
A.CallTo(() => userService.GetDelegateUserById(DelegateId))
6463
.Returns(UserTestHelper.GetDefaultDelegateUser(centreId: 100));
6564

6665
// When
67-
new VerifyAdminUserCanAccessDelegateUser(userDataService).OnActionExecuting(context);
66+
new VerifyAdminUserCanAccessDelegateUser(userService).OnActionExecuting(context);
6867

6968
// Then
7069
context.Result.Should().BeRedirectToActionResult().WithControllerName("LearningSolutions")
@@ -76,11 +75,11 @@ public void Does_not_return_any_redirect_page_if_service_returns_delegate_user_a
7675
{
7776
// Given
7877
context.RouteData.Values["delegateId"] = DelegateId;
79-
A.CallTo(() => userDataService.GetDelegateUserById(DelegateId))
78+
A.CallTo(() => userService.GetDelegateUserById(DelegateId))
8079
.Returns(UserTestHelper.GetDefaultDelegateUser(centreId: 101));
8180

8281
// When
83-
new VerifyAdminUserCanAccessDelegateUser(userDataService).OnActionExecuting(context);
82+
new VerifyAdminUserCanAccessDelegateUser(userService).OnActionExecuting(context);
8483

8584
// Then
8685
context.Result.Should().BeNull();

DigitalLearningSolutions.Web.Tests/Services/GroupServiceTests/GroupsServiceSynchroniseGroupsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ public void AddNewDelegateToAppropriateGroups_adds_delegate_to_appropriate_group
444444
);
445445
A.CallTo(() => groupsDataService.GetGroupsForCentre(registrationModel.Centre))
446446
.Returns(new List<Group> { synchronisedGroup, unsynchronisedGroup });
447-
A.CallTo(() => jobGroupsDataService.GetJobGroupName(0)).Returns(null);
448-
A.CallTo(() => jobGroupsDataService.GetJobGroupName(1)).Returns(null);
447+
A.CallTo(() => jobGroupsService.GetJobGroupName(0)).Returns(null);
448+
A.CallTo(() => jobGroupsService.GetJobGroupName(1)).Returns(null);
449449

450450
// When
451451
groupsService.AddNewDelegateToAppropriateGroups(
@@ -489,9 +489,9 @@ public void SynchroniseJobGroupsOnOtherCentres_synchronises_correct_job_groups()
489489
A.CallTo(() => groupsDataService.GetGroupsForCentre(delegateAccount.CentreId))
490490
.Returns(groups);
491491

492-
A.CallTo(() => jobGroupsDataService.GetJobGroupName(oldJobGroupId))
492+
A.CallTo(() => jobGroupsService.GetJobGroupName(oldJobGroupId))
493493
.Returns(oldJobGroupGroup.GroupLabel);
494-
A.CallTo(() => jobGroupsDataService.GetJobGroupName(newJobGroupId))
494+
A.CallTo(() => jobGroupsService.GetJobGroupName(newJobGroupId))
495495
.Returns(newJobGroupGroup.GroupLabel);
496496

497497
// When

DigitalLearningSolutions.Web.Tests/Services/GroupServiceTests/GroupsServiceTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public partial class GroupsServiceTests
5252
private IEmailSchedulerService emailSchedulerService = null!;
5353
private IGroupsDataService groupsDataService = null!;
5454
private IGroupsService groupsService = null!;
55-
private IJobGroupsDataService jobGroupsDataService = null!;
55+
private IJobGroupsService jobGroupsService = null!;
5656
private IEnrolService enrolService = null!;
5757
private ILogger<IGroupsService> logger = null!;
5858
private IProgressDataService progressDataService = null!;
@@ -73,11 +73,11 @@ public void Setup()
7373
configuration = A.Fake<IConfiguration>();
7474
centreRegistrationPromptsService = A.Fake<ICentreRegistrationPromptsService>();
7575
logger = A.Fake<ILogger<IGroupsService>>();
76-
jobGroupsDataService = A.Fake<IJobGroupsDataService>(x => x.Strict());
76+
jobGroupsService = A.Fake<IJobGroupsService>(x => x.Strict());
7777
userDataService = A.Fake<IUserDataService>();
7878
notificationPreferencesDataService = A.Fake<INotificationPreferencesDataService>();
7979

80-
A.CallTo(() => jobGroupsDataService.GetJobGroupsAlphabetical()).Returns(
80+
A.CallTo(() => jobGroupsService.GetJobGroupsAlphabetical()).Returns(
8181
JobGroupsTestHelper.GetDefaultJobGroupsAlphabetical()
8282
);
8383
A.CallTo(() => configuration["AppRootPath"]).Returns("baseUrl");
@@ -102,7 +102,7 @@ public void Setup()
102102
clockUtility,
103103
tutorialContentDataService,
104104
emailService,
105-
jobGroupsDataService,
105+
jobGroupsService,
106106
progressDataService,
107107
configuration,
108108
centreRegistrationPromptsService,
@@ -111,7 +111,7 @@ public void Setup()
111111
notificationPreferencesDataService
112112
);
113113

114-
A.CallTo(() => jobGroupsDataService.GetJobGroupsAlphabetical()).Returns(
114+
A.CallTo(() => jobGroupsService.GetJobGroupsAlphabetical()).Returns(
115115
JobGroupsTestHelper.GetDefaultJobGroupsAlphabetical()
116116
);
117117
A.CallTo(() => configuration["AppRootPath"]).Returns("baseUrl");
@@ -554,7 +554,7 @@ public void
554554
true
555555
);
556556

557-
A.CallTo(() => jobGroupsDataService.GetJobGroupsAlphabetical()).Returns(jobGroups);
557+
A.CallTo(() => jobGroupsService.GetJobGroupsAlphabetical()).Returns(jobGroups);
558558

559559
SetUpGenerateGroupFakes(timeNow);
560560

DigitalLearningSolutions.Web/Helpers/LinkedFieldHelper.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using System.Collections.Generic;
44
using System.Linq;
5-
using DigitalLearningSolutions.Data.DataServices;
65
using DigitalLearningSolutions.Data.Enums;
76
using DigitalLearningSolutions.Data.Models;
87
using DigitalLearningSolutions.Data.Models.User;
@@ -13,16 +12,16 @@ public static class LinkedFieldHelper
1312
public static List<LinkedFieldChange> GetLinkedFieldChanges(
1413
RegistrationFieldAnswers oldAnswers,
1514
RegistrationFieldAnswers newAnswers,
16-
IJobGroupsDataService jobGroupsDataService,
15+
IJobGroupsService jobGroupsService,
1716
ICentreRegistrationPromptsService centreRegistrationPromptsService
1817
)
1918
{
2019
var changedLinkedFieldsWithAnswers = new List<LinkedFieldChange>();
2120

2221
if (newAnswers.JobGroupId != oldAnswers.JobGroupId)
2322
{
24-
var oldJobGroup = jobGroupsDataService.GetJobGroupName(oldAnswers.JobGroupId);
25-
var newJobGroup = jobGroupsDataService.GetJobGroupName(newAnswers.JobGroupId);
23+
var oldJobGroup = jobGroupsService.GetJobGroupName(oldAnswers.JobGroupId);
24+
var newJobGroup = jobGroupsService.GetJobGroupName(newAnswers.JobGroupId);
2625
changedLinkedFieldsWithAnswers.Add(
2726
new LinkedFieldChange(
2827
RegistrationField.JobGroup.LinkedToFieldId,

DigitalLearningSolutions.Web/ServiceFilter/VerifyAdminAndDelegateUserCentre.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
namespace DigitalLearningSolutions.Web.ServiceFilter
22
{
3-
using DigitalLearningSolutions.Data.DataServices.UserDataService;
43
using DigitalLearningSolutions.Web.Helpers;
4+
using DigitalLearningSolutions.Web.Services;
55
using Microsoft.AspNetCore.Mvc;
66
using Microsoft.AspNetCore.Mvc.Filters;
77

88
public class VerifyAdminAndDelegateUserCentre : IActionFilter
99
{
10-
private readonly IUserDataService userDataService;
10+
private readonly IUserService userService;
1111

12-
public VerifyAdminAndDelegateUserCentre(IUserDataService userDataService)
12+
public VerifyAdminAndDelegateUserCentre(IUserService userService)
1313
{
14-
this.userDataService = userDataService;
14+
this.userService = userService;
1515
}
1616

1717
public void OnActionExecuted(ActionExecutedContext context) { }
@@ -25,7 +25,7 @@ public void OnActionExecuting(ActionExecutingContext context)
2525

2626
var centreId = controller.User.GetCentreIdKnownNotNull();
2727
var delegateUserId = int.Parse(context.RouteData.Values["delegateId"].ToString()!);
28-
var delegateAccount = userDataService.GetDelegateUserById(delegateUserId);
28+
var delegateAccount = userService.GetDelegateUserById(delegateUserId);
2929

3030
if (delegateAccount == null)
3131
{

DigitalLearningSolutions.Web/ServiceFilter/VerifyAdminUserCanAccessAdminUser.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
using System.Net;
44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.AspNetCore.Mvc.Filters;
6-
using DigitalLearningSolutions.Data.DataServices.UserDataService;
76
using DigitalLearningSolutions.Web.Helpers;
7+
using DigitalLearningSolutions.Web.Services;
88

99
public class VerifyAdminUserCanAccessAdminUser : IActionFilter
1010
{
11-
private readonly IUserDataService userDataService;
11+
private readonly IUserService userService;
1212

13-
public VerifyAdminUserCanAccessAdminUser(IUserDataService userDataService)
13+
public VerifyAdminUserCanAccessAdminUser(IUserService userService)
1414
{
15-
this.userDataService = userDataService;
15+
this.userService = userService;
1616
}
1717

1818
public void OnActionExecuted(ActionExecutedContext context) { }
@@ -26,7 +26,7 @@ public void OnActionExecuting(ActionExecutingContext context)
2626

2727
var centreId = controller.User.GetCentreIdKnownNotNull();
2828
var adminUserId = int.Parse(context.RouteData.Values["adminId"].ToString()!);
29-
var adminAccount = userDataService.GetAdminUserById(adminUserId);
29+
var adminAccount = userService.GetAdminUserById(adminUserId);
3030

3131
if (adminAccount == null)
3232
{

DigitalLearningSolutions.Web/ServiceFilter/VerifyAdminUserCanAccessDelegateUser.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
namespace DigitalLearningSolutions.Web.ServiceFilter
22
{
3-
using DigitalLearningSolutions.Data.DataServices.UserDataService;
43
using DigitalLearningSolutions.Web.Helpers;
4+
using DigitalLearningSolutions.Web.Services;
55
using Microsoft.AspNetCore.Mvc;
66
using Microsoft.AspNetCore.Mvc.Filters;
77

88
public class VerifyAdminUserCanAccessDelegateUser : IActionFilter
99
{
10-
private readonly IUserDataService userDataService;
10+
private readonly IUserService userService;
1111

12-
public VerifyAdminUserCanAccessDelegateUser(IUserDataService userDataService)
12+
public VerifyAdminUserCanAccessDelegateUser(IUserService userService)
1313
{
14-
this.userDataService = userDataService;
14+
this.userService = userService;
1515
}
1616

1717
public void OnActionExecuted(ActionExecutedContext context) { }
@@ -25,7 +25,7 @@ public void OnActionExecuting(ActionExecutingContext context)
2525

2626
var centreId = controller.User.GetCentreIdKnownNotNull();
2727
var delegateUserId = int.Parse(context.RouteData.Values["delegateId"].ToString()!);
28-
var delegateAccount = userDataService.GetDelegateUserById(delegateUserId);
28+
var delegateAccount = userService.GetDelegateUserById(delegateUserId);
2929

3030
if (delegateAccount == null)
3131
{

DigitalLearningSolutions.Web/Services/GroupsService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public class GroupsService : IGroupsService
154154
private readonly IConfiguration configuration;
155155
private readonly IEmailService emailService;
156156
private readonly IGroupsDataService groupsDataService;
157-
private readonly IJobGroupsDataService jobGroupsDataService;
157+
private readonly IJobGroupsService jobGroupsService;
158158
private readonly ILogger<IGroupsService> logger;
159159
private readonly IProgressDataService progressDataService;
160160
private readonly ITutorialContentDataService tutorialContentDataService;
@@ -166,7 +166,7 @@ public GroupsService(
166166
IClockUtility clockUtility,
167167
ITutorialContentDataService tutorialContentDataService,
168168
IEmailService emailService,
169-
IJobGroupsDataService jobGroupsDataService,
169+
IJobGroupsService jobGroupsService,
170170
IProgressDataService progressDataService,
171171
IConfiguration configuration,
172172
ICentreRegistrationPromptsService centreRegistrationPromptsService,
@@ -179,7 +179,7 @@ INotificationPreferencesDataService notificationPreferencesDataService
179179
this.clockUtility = clockUtility;
180180
this.tutorialContentDataService = tutorialContentDataService;
181181
this.emailService = emailService;
182-
this.jobGroupsDataService = jobGroupsDataService;
182+
this.jobGroupsService = jobGroupsService;
183183
this.progressDataService = progressDataService;
184184
this.configuration = configuration;
185185
this.centreRegistrationPromptsService = centreRegistrationPromptsService;
@@ -294,7 +294,7 @@ List<Group> groupsForSynchronisation
294294
var changedLinkedFields = LinkedFieldHelper.GetLinkedFieldChanges(
295295
oldRegistrationFieldAnswers,
296296
registrationFieldAnswers,
297-
jobGroupsDataService,
297+
jobGroupsService,
298298
centreRegistrationPromptsService
299299
);
300300

@@ -344,8 +344,8 @@ AccountDetailsData accountDetailsData
344344
{
345345
var groupsLinkedToJobGroup = GetGroupsWhichShouldUpdateWhenUserDetailsChangeForCentre(account.CentreId)
346346
.Where(g => g.LinkedToField == JobGroupLinkedFieldNumber).ToList();
347-
var oldJobGroupName = jobGroupsDataService.GetJobGroupName(oldJobGroupId);
348-
var newJobGroupName = jobGroupsDataService.GetJobGroupName(newJobGroupId);
347+
var oldJobGroupName = jobGroupsService.GetJobGroupName(oldJobGroupId);
348+
var newJobGroupName = jobGroupsService.GetJobGroupName(newJobGroupId);
349349

350350
var groupsToRemoveDelegateFrom = groupsLinkedToJobGroup.Where(
351351
g =>
@@ -885,7 +885,7 @@ by the system because a previous course completion has expired.</p>
885885

886886
private (List<(int id, string name)>, string groupNamePrefix) GetJobGroupsAndPrefix()
887887
{
888-
var jobGroups = jobGroupsDataService.GetJobGroupsAlphabetical().ToList();
888+
var jobGroups = jobGroupsService.GetJobGroupsAlphabetical().ToList();
889889
const string groupNamePrefix = "Job group";
890890
return (jobGroups, groupNamePrefix);
891891
}

DigitalLearningSolutions.Web/Services/UserService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ void SetCentreEmail(
154154
);
155155
int GetDelegateCountWithAnswerForPrompt(int centreId, int promptNumber);
156156
List<AdminUser> GetAdminUsersByCentreId(int centreId);
157+
AdminUser? GetAdminUserById(int id);
157158
}
158159

159160
public class UserService : IUserService
@@ -826,5 +827,10 @@ public List<AdminUser> GetAdminUsersByCentreId(int centreId)
826827
{
827828
return userDataService.GetAdminUsersByCentreId(centreId);
828829
}
830+
831+
public AdminUser? GetAdminUserById(int id)
832+
{
833+
return userDataService.GetAdminUserById(id);
834+
}
829835
}
830836
}

0 commit comments

Comments
 (0)