Skip to content

Commit f4bb6d4

Browse files
committed
TD-3909 - Contrller refactor -DataService reference removed from the controller
1 parent 5e6e468 commit f4bb6d4

File tree

4 files changed

+266
-201
lines changed

4 files changed

+266
-201
lines changed

DigitalLearningSolutions.Data/DataServices/RoleProfileService.cs renamed to DigitalLearningSolutions.Data/DataServices/RoleProfileDataService.cs

Lines changed: 197 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -1,197 +1,197 @@
1-
namespace DigitalLearningSolutions.Data.DataServices
2-
{
3-
using System.Collections.Generic;
4-
using System.Data;
5-
using System.Linq;
6-
using Dapper;
7-
using DigitalLearningSolutions.Data.Models.RoleProfiles;
8-
using Microsoft.Extensions.Logging;
9-
10-
public interface IRoleProfileService
11-
{
12-
//GET DATA
13-
IEnumerable<RoleProfile> GetAllRoleProfiles(int adminId);
14-
15-
IEnumerable<RoleProfile> GetRoleProfilesForAdminId(int adminId);
16-
17-
RoleProfileBase? GetRoleProfileBaseById(int roleProfileId, int adminId);
18-
19-
RoleProfileBase? GetRoleProfileByName(string roleProfileName, int adminId);
20-
21-
IEnumerable<NRPProfessionalGroups> GetNRPProfessionalGroups();
22-
23-
//UPDATE DATA
24-
bool UpdateRoleProfileName(int roleProfileId, int adminId, string roleProfileName);
25-
26-
bool UpdateRoleProfileProfessionalGroup(int roleProfileId, int adminId, int? nrpProfessionalGroupID);
27-
//INSERT DATA
28-
29-
//DELETE DATA
30-
}
31-
32-
public class RoleProfileService : IRoleProfileService
33-
{
34-
private const string SelfAssessmentBaseFields = @"rp.ID, rp.Name AS RoleProfileName, rp.Description, rp.BrandID,
35-
rp.ParentSelfAssessmentID,
36-
rp.[National], rp.[Public], rp.CreatedByAdminID AS OwnerAdminID,
37-
rp.NRPProfessionalGroupID,
38-
rp.NRPSubGroupID,
39-
rp.NRPRoleID,
40-
rp.PublishStatusID, CASE WHEN rp.CreatedByAdminID = @adminId THEN 3 WHEN rpc.CanModify = 1 THEN 2 WHEN rpc.CanModify = 0 THEN 1 ELSE 0 END AS UserRole";
41-
42-
private const string SelfAssessmentFields =
43-
@", rp.CreatedDate,
44-
(SELECT BrandName
45-
FROM Brands
46-
WHERE (BrandID = rp.BrandID)) AS Brand,
47-
(SELECT [Name]
48-
FROM SelfAssessments AS rp2
49-
WHERE (ID = rp.ParentSelfAssessmentID)) AS ParentSelfAssessment,
50-
(SELECT Forename + ' ' + Surname + (CASE WHEN Active = 1 THEN '' ELSE ' (Inactive)' END) AS Expr1
51-
FROM AdminUsers
52-
WHERE (AdminID = rp.CreatedByAdminID)) AS Owner,
53-
rp.Archived,
54-
rp.LastEdit,
55-
(SELECT ProfessionalGroup
56-
FROM NRPProfessionalGroups
57-
WHERE (ID = rp.NRPProfessionalGroupID)) AS NRPProfessionalGroup,
58-
(SELECT SubGroup
59-
FROM NRPSubGroups
60-
WHERE (ID = rp.NRPSubGroupID)) AS NRPSubGroup,
61-
(SELECT RoleProfile
62-
FROM NRPRoles
63-
WHERE (ID = rp.NRPRoleID)) AS NRPRole, rpr.ID AS SelfAssessmentReviewID";
64-
65-
private const string SelfAssessmentBaseTables =
66-
@"SelfAssessments AS rp LEFT OUTER JOIN
67-
SelfAssessmentCollaborators AS rpc ON rpc.SelfAssessmentID = rp.ID AND rpc.AdminID = @adminId";
68-
69-
private const string SelfAssessmentTables =
70-
@" LEFT OUTER JOIN
71-
SelfAssessmentReviews AS rpr ON rpc.ID = rpr.SelfAssessmentCollaboratorID AND rpr.Archived IS NULL AND rpr.ReviewComplete IS NULL";
72-
73-
private readonly IDbConnection connection;
74-
private readonly ILogger<RoleProfileService> logger;
75-
76-
public RoleProfileService(IDbConnection connection, ILogger<RoleProfileService> logger)
77-
{
78-
this.connection = connection;
79-
this.logger = logger;
80-
}
81-
82-
public IEnumerable<RoleProfile> GetAllRoleProfiles(int adminId)
83-
{
84-
return connection.Query<RoleProfile>(
85-
$@"SELECT {SelfAssessmentBaseFields} {SelfAssessmentFields}
86-
FROM {SelfAssessmentBaseTables} {SelfAssessmentTables}",
87-
new { adminId }
88-
);
89-
}
90-
91-
public IEnumerable<RoleProfile> GetRoleProfilesForAdminId(int adminId)
92-
{
93-
return connection.Query<RoleProfile>(
94-
$@"SELECT {SelfAssessmentBaseFields} {SelfAssessmentFields}
95-
FROM {SelfAssessmentBaseTables} {SelfAssessmentTables}
96-
WHERE (rp.CreatedByAdminID = @adminId) OR
97-
(@adminId IN
98-
(SELECT AdminID
99-
FROM SelfAssessmentCollaborators
100-
WHERE (SelfAssessmentID = rp.ID)))",
101-
new { adminId }
102-
);
103-
}
104-
105-
public RoleProfileBase? GetRoleProfileBaseById(int roleProfileId, int adminId)
106-
{
107-
return connection.Query<RoleProfileBase>(
108-
$@"SELECT {SelfAssessmentBaseFields}
109-
FROM {SelfAssessmentBaseTables}
110-
WHERE (rp.ID = @roleProfileId)",
111-
new { roleProfileId, adminId }
112-
).FirstOrDefault();
113-
}
114-
115-
public bool UpdateRoleProfileName(int roleProfileId, int adminId, string roleProfileName)
116-
{
117-
if ((roleProfileName.Length == 0) | (adminId < 1) | (roleProfileId < 1))
118-
{
119-
logger.LogWarning(
120-
$"Not updating role profile name as it failed server side validation. AdminId: {adminId}, roleProfileName: {roleProfileName}, roleProfileId: {roleProfileId}"
121-
);
122-
return false;
123-
}
124-
125-
var existingSelfAssessments = (int)connection.ExecuteScalar(
126-
@"SELECT COUNT(*) FROM SelfAssessments WHERE [Name] = @roleProfileName AND ID <> @roleProfileId",
127-
new { roleProfileName, roleProfileId }
128-
);
129-
if (existingSelfAssessments > 0)
130-
{
131-
return false;
132-
}
133-
134-
var numberOfAffectedRows = connection.Execute(
135-
@"UPDATE SelfAssessments SET [Name] = @roleProfileName, UpdatedByAdminID = @adminId
136-
WHERE ID = @roleProfileId",
137-
new { roleProfileName, adminId, roleProfileId }
138-
);
139-
if (numberOfAffectedRows < 1)
140-
{
141-
logger.LogWarning(
142-
"Not updating role profile name as db update failed. " +
143-
$"SelfAssessmentName: {roleProfileName}, admin id: {adminId}, roleProfileId: {roleProfileId}"
144-
);
145-
return false;
146-
}
147-
148-
return true;
149-
}
150-
151-
public IEnumerable<NRPProfessionalGroups> GetNRPProfessionalGroups()
152-
{
153-
return connection.Query<NRPProfessionalGroups>(
154-
@"SELECT ID, ProfessionalGroup, Active
155-
FROM NRPProfessionalGroups
156-
WHERE (Active = 1)
157-
ORDER BY ProfessionalGroup"
158-
);
159-
}
160-
161-
public RoleProfileBase? GetRoleProfileByName(string roleProfileName, int adminId)
162-
{
163-
return connection.Query<RoleProfileBase>(
164-
$@"SELECT {SelfAssessmentBaseFields}
165-
FROM {SelfAssessmentBaseTables}
166-
WHERE (rp.Name = @roleProfileName)",
167-
new { roleProfileName, adminId }
168-
).FirstOrDefault();
169-
}
170-
171-
public bool UpdateRoleProfileProfessionalGroup(int roleProfileId, int adminId, int? nrpProfessionalGroupID)
172-
{
173-
var sameCount = (int)connection.ExecuteScalar(
174-
@"SELECT COUNT(*) FROM RoleProfiles WHERE ID = @roleProfileId AND NRPProfessionalGroupID = @nrpProfessionalGroupID",
175-
new { roleProfileId, nrpProfessionalGroupID }
176-
);
177-
if (sameCount > 0)
178-
{
179-
//same so don't update:
180-
return false;
181-
}
182-
183-
//needs updating:
184-
var numberOfAffectedRows = connection.Execute(
185-
@"UPDATE SelfAssessments SET NRPProfessionalGroupID = @nrpProfessionalGroupID, NRPSubGroupID = NULL, NRPRoleID = NULL, UpdatedByAdminID = @adminId
186-
WHERE ID = @roleProfileId",
187-
new { nrpProfessionalGroupID, adminId, roleProfileId }
188-
);
189-
if (numberOfAffectedRows > 0)
190-
{
191-
return true;
192-
}
193-
194-
return false;
195-
}
196-
}
197-
}
1+
namespace DigitalLearningSolutions.Data.DataServices
2+
{
3+
using System.Collections.Generic;
4+
using System.Data;
5+
using System.Linq;
6+
using Dapper;
7+
using DigitalLearningSolutions.Data.Models.RoleProfiles;
8+
using Microsoft.Extensions.Logging;
9+
10+
public interface IRoleProfileDataService
11+
{
12+
//GET DATA
13+
IEnumerable<RoleProfile> GetAllRoleProfiles(int adminId);
14+
15+
IEnumerable<RoleProfile> GetRoleProfilesForAdminId(int adminId);
16+
17+
RoleProfileBase? GetRoleProfileBaseById(int roleProfileId, int adminId);
18+
19+
RoleProfileBase? GetRoleProfileByName(string roleProfileName, int adminId);
20+
21+
IEnumerable<NRPProfessionalGroups> GetNRPProfessionalGroups();
22+
23+
//UPDATE DATA
24+
bool UpdateRoleProfileName(int roleProfileId, int adminId, string roleProfileName);
25+
26+
bool UpdateRoleProfileProfessionalGroup(int roleProfileId, int adminId, int? nrpProfessionalGroupID);
27+
//INSERT DATA
28+
29+
//DELETE DATA
30+
}
31+
32+
public class RoleProfileDataService : IRoleProfileDataService
33+
{
34+
private const string SelfAssessmentBaseFields = @"rp.ID, rp.Name AS RoleProfileName, rp.Description, rp.BrandID,
35+
rp.ParentSelfAssessmentID,
36+
rp.[National], rp.[Public], rp.CreatedByAdminID AS OwnerAdminID,
37+
rp.NRPProfessionalGroupID,
38+
rp.NRPSubGroupID,
39+
rp.NRPRoleID,
40+
rp.PublishStatusID, CASE WHEN rp.CreatedByAdminID = @adminId THEN 3 WHEN rpc.CanModify = 1 THEN 2 WHEN rpc.CanModify = 0 THEN 1 ELSE 0 END AS UserRole";
41+
42+
private const string SelfAssessmentFields =
43+
@", rp.CreatedDate,
44+
(SELECT BrandName
45+
FROM Brands
46+
WHERE (BrandID = rp.BrandID)) AS Brand,
47+
(SELECT [Name]
48+
FROM SelfAssessments AS rp2
49+
WHERE (ID = rp.ParentSelfAssessmentID)) AS ParentSelfAssessment,
50+
(SELECT Forename + ' ' + Surname + (CASE WHEN Active = 1 THEN '' ELSE ' (Inactive)' END) AS Expr1
51+
FROM AdminUsers
52+
WHERE (AdminID = rp.CreatedByAdminID)) AS Owner,
53+
rp.Archived,
54+
rp.LastEdit,
55+
(SELECT ProfessionalGroup
56+
FROM NRPProfessionalGroups
57+
WHERE (ID = rp.NRPProfessionalGroupID)) AS NRPProfessionalGroup,
58+
(SELECT SubGroup
59+
FROM NRPSubGroups
60+
WHERE (ID = rp.NRPSubGroupID)) AS NRPSubGroup,
61+
(SELECT RoleProfile
62+
FROM NRPRoles
63+
WHERE (ID = rp.NRPRoleID)) AS NRPRole, rpr.ID AS SelfAssessmentReviewID";
64+
65+
private const string SelfAssessmentBaseTables =
66+
@"SelfAssessments AS rp LEFT OUTER JOIN
67+
SelfAssessmentCollaborators AS rpc ON rpc.SelfAssessmentID = rp.ID AND rpc.AdminID = @adminId";
68+
69+
private const string SelfAssessmentTables =
70+
@" LEFT OUTER JOIN
71+
SelfAssessmentReviews AS rpr ON rpc.ID = rpr.SelfAssessmentCollaboratorID AND rpr.Archived IS NULL AND rpr.ReviewComplete IS NULL";
72+
73+
private readonly IDbConnection connection;
74+
private readonly ILogger<RoleProfileDataService> logger;
75+
76+
public RoleProfileDataService(IDbConnection connection, ILogger<RoleProfileDataService> logger)
77+
{
78+
this.connection = connection;
79+
this.logger = logger;
80+
}
81+
82+
public IEnumerable<RoleProfile> GetAllRoleProfiles(int adminId)
83+
{
84+
return connection.Query<RoleProfile>(
85+
$@"SELECT {SelfAssessmentBaseFields} {SelfAssessmentFields}
86+
FROM {SelfAssessmentBaseTables} {SelfAssessmentTables}",
87+
new { adminId }
88+
);
89+
}
90+
91+
public IEnumerable<RoleProfile> GetRoleProfilesForAdminId(int adminId)
92+
{
93+
return connection.Query<RoleProfile>(
94+
$@"SELECT {SelfAssessmentBaseFields} {SelfAssessmentFields}
95+
FROM {SelfAssessmentBaseTables} {SelfAssessmentTables}
96+
WHERE (rp.CreatedByAdminID = @adminId) OR
97+
(@adminId IN
98+
(SELECT AdminID
99+
FROM SelfAssessmentCollaborators
100+
WHERE (SelfAssessmentID = rp.ID)))",
101+
new { adminId }
102+
);
103+
}
104+
105+
public RoleProfileBase? GetRoleProfileBaseById(int roleProfileId, int adminId)
106+
{
107+
return connection.Query<RoleProfileBase>(
108+
$@"SELECT {SelfAssessmentBaseFields}
109+
FROM {SelfAssessmentBaseTables}
110+
WHERE (rp.ID = @roleProfileId)",
111+
new { roleProfileId, adminId }
112+
).FirstOrDefault();
113+
}
114+
115+
public bool UpdateRoleProfileName(int roleProfileId, int adminId, string roleProfileName)
116+
{
117+
if ((roleProfileName.Length == 0) | (adminId < 1) | (roleProfileId < 1))
118+
{
119+
logger.LogWarning(
120+
$"Not updating role profile name as it failed server side validation. AdminId: {adminId}, roleProfileName: {roleProfileName}, roleProfileId: {roleProfileId}"
121+
);
122+
return false;
123+
}
124+
125+
var existingSelfAssessments = (int)connection.ExecuteScalar(
126+
@"SELECT COUNT(*) FROM SelfAssessments WHERE [Name] = @roleProfileName AND ID <> @roleProfileId",
127+
new { roleProfileName, roleProfileId }
128+
);
129+
if (existingSelfAssessments > 0)
130+
{
131+
return false;
132+
}
133+
134+
var numberOfAffectedRows = connection.Execute(
135+
@"UPDATE SelfAssessments SET [Name] = @roleProfileName, UpdatedByAdminID = @adminId
136+
WHERE ID = @roleProfileId",
137+
new { roleProfileName, adminId, roleProfileId }
138+
);
139+
if (numberOfAffectedRows < 1)
140+
{
141+
logger.LogWarning(
142+
"Not updating role profile name as db update failed. " +
143+
$"SelfAssessmentName: {roleProfileName}, admin id: {adminId}, roleProfileId: {roleProfileId}"
144+
);
145+
return false;
146+
}
147+
148+
return true;
149+
}
150+
151+
public IEnumerable<NRPProfessionalGroups> GetNRPProfessionalGroups()
152+
{
153+
return connection.Query<NRPProfessionalGroups>(
154+
@"SELECT ID, ProfessionalGroup, Active
155+
FROM NRPProfessionalGroups
156+
WHERE (Active = 1)
157+
ORDER BY ProfessionalGroup"
158+
);
159+
}
160+
161+
public RoleProfileBase? GetRoleProfileByName(string roleProfileName, int adminId)
162+
{
163+
return connection.Query<RoleProfileBase>(
164+
$@"SELECT {SelfAssessmentBaseFields}
165+
FROM {SelfAssessmentBaseTables}
166+
WHERE (rp.Name = @roleProfileName)",
167+
new { roleProfileName, adminId }
168+
).FirstOrDefault();
169+
}
170+
171+
public bool UpdateRoleProfileProfessionalGroup(int roleProfileId, int adminId, int? nrpProfessionalGroupID)
172+
{
173+
var sameCount = (int)connection.ExecuteScalar(
174+
@"SELECT COUNT(*) FROM RoleProfiles WHERE ID = @roleProfileId AND NRPProfessionalGroupID = @nrpProfessionalGroupID",
175+
new { roleProfileId, nrpProfessionalGroupID }
176+
);
177+
if (sameCount > 0)
178+
{
179+
//same so don't update:
180+
return false;
181+
}
182+
183+
//needs updating:
184+
var numberOfAffectedRows = connection.Execute(
185+
@"UPDATE SelfAssessments SET NRPProfessionalGroupID = @nrpProfessionalGroupID, NRPSubGroupID = NULL, NRPRoleID = NULL, UpdatedByAdminID = @adminId
186+
WHERE ID = @roleProfileId",
187+
new { nrpProfessionalGroupID, adminId, roleProfileId }
188+
);
189+
if (numberOfAffectedRows > 0)
190+
{
191+
return true;
192+
}
193+
194+
return false;
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)