312 lines
10 KiB
C#
312 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using Dapper;
|
|
using Fab2ApprovalSystem.Models;
|
|
using System.Text;
|
|
using Fab2ApprovalSystem.Misc;
|
|
|
|
namespace Fab2ApprovalSystem.DMO
|
|
{
|
|
public class AdminDMO
|
|
{
|
|
private IDbConnection db = new SqlConnection(GlobalVars.DB_CONNECTION_STRING);
|
|
private static FabApprovalTrainingEntities FabApprovalDB = new FabApprovalTrainingEntities();
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<Role> GetSubRoles()
|
|
{
|
|
StringBuilder sql = new StringBuilder();
|
|
sql.Append(
|
|
"SELECT R.RoleID, R.RoleName, SubRoleID, SubRoleCategoryItem, SR.RoleID, SR.Inactive " +
|
|
"FROM vSubRoles SR " +
|
|
"INNER JOIN Role R ON R.RoleID = SR.RoleID " +
|
|
"ORDER BY R.RoleID, SubRoleCategoryItem ");
|
|
|
|
db.Open();
|
|
var lookup = new Dictionary<int, Role>();
|
|
List<Role> data = this.db.Query<Role, SubRole, Role>(sql.ToString(),
|
|
(parent, child) =>
|
|
{
|
|
Role role;
|
|
if (!lookup.TryGetValue(parent.RoleID, out role))
|
|
{
|
|
lookup.Add(parent.RoleID, role = parent);
|
|
}
|
|
//if (role.RoleID == null)
|
|
// role.SubRoles = new List<SubRole>();
|
|
role.SubRoles.Add(child);
|
|
return role;
|
|
},
|
|
splitOn: "SubRoleID").Distinct().ToList();
|
|
|
|
|
|
return data;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// /// <param name="userId"></param>
|
|
/// <returns></returns>
|
|
public List<UserSubRoles> GetUserSubRoles(int userId)
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
parameters.Add("@UserId", userId);
|
|
var userSubRoleList = this.db.Query<UserSubRoles>("GetSubRolesByUserId", parameters, commandType: CommandType.StoredProcedure).ToList();
|
|
return userSubRoleList;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="subRole"></param>
|
|
/// <returns></returns>
|
|
public IEnumerable<LoginModel> GetAllUsersBySubRole(int subRole)
|
|
{
|
|
StringBuilder sql = new StringBuilder();
|
|
sql.Append("SELECT FirstName + ' ' + LastName AS FullName , LoginID, FirstName, LastName, U.UserID, SubRoleID ");
|
|
sql.Append("FROM UserSubRole UR ");
|
|
sql.Append("INNER JOIN Users U ON UR.UserID = U.UserID ");
|
|
sql.Append("WHERE UR.SubRoleID = " + subRole.ToString() + " ");
|
|
sql.Append("ORDER BY FirstName");
|
|
|
|
return this.db.Query<LoginModel>(sql.ToString()).ToList();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="subRole"></param>
|
|
/// <param name="userids"></param>
|
|
public void AddUserRoles(int subRole, string userids)
|
|
{
|
|
string sql;
|
|
|
|
string[] arrayOfUsers = userids.Split(new char[] { '~' });
|
|
|
|
for (int i = 0; i < arrayOfUsers.Length; i++)
|
|
{
|
|
sql = "INSERT INTO UserSubRole (UserID, SubRoleID) VALUES (" + arrayOfUsers[i] + ", " + subRole + " )";
|
|
this.db.Execute(sql);
|
|
}
|
|
|
|
}
|
|
|
|
public void DeleteUserFromAllTrainingGroups(int userId)
|
|
{
|
|
string sql = "DELETE FROM TrainingGroupMembers WHERE UserId = " + userId;
|
|
|
|
|
|
this.db.Open();
|
|
this.db.Execute(sql);
|
|
return;
|
|
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="subRole"></param>
|
|
/// <param name="userids"></param>
|
|
public void DeleteUserRoles(int subRole, string userids)
|
|
{
|
|
string sql;
|
|
|
|
string[] arrayOfUsers = userids.Split(new char[] { '~' });
|
|
|
|
for (int i = 0; i < arrayOfUsers.Length; i++)
|
|
{
|
|
sql = "DELETE FROM UserSubRole WHERE UserID = " + arrayOfUsers[i] + " AND SubRoleID = " + subRole;
|
|
this.db.Execute(sql);
|
|
}
|
|
|
|
}
|
|
public List<TrainingReportUser> GetTrainingReportUsers()
|
|
{
|
|
List<TrainingReportUser> CurrentReportUsers = (from a in FabApprovalDB.TrainingReportUsers select a).ToList();
|
|
return CurrentReportUsers;
|
|
}
|
|
public List<TECNNotificationsUser> GetTECNNotificationUsers()
|
|
{
|
|
List<TECNNotificationsUser> currentTECNNotificationUsers = (from a in FabApprovalDB.TECNNotificationsUsers select a).ToList();
|
|
return currentTECNNotificationUsers;
|
|
}
|
|
|
|
public void TrainingReportAddUser(int userId)
|
|
{
|
|
string sql = "INSERT INTO TrainingReportUsers (UserId) " + "VALUES ('" + userId + "') ";
|
|
|
|
|
|
this.db.Open();
|
|
this.db.Execute(sql);
|
|
return;
|
|
}
|
|
public void TECNExpirationAddUser(int userId)
|
|
{
|
|
string sql = "INSERT INTO TECNNotificationsUsers (UserId) " + "VALUES ('" + userId + "') ";
|
|
|
|
this.db.Open();
|
|
this.db.Execute(sql);
|
|
|
|
|
|
|
|
return;
|
|
}
|
|
public void TrainingReportDeleteUser(int userId)
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
|
|
parameters = new DynamicParameters();
|
|
parameters.Add("@UserID", userId);
|
|
|
|
this.db.Execute("DeleteUserFromTrainingReport", parameters, commandType: CommandType.StoredProcedure);
|
|
return;
|
|
}
|
|
public void TECNExpirationDeleteUser(int userId)
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
|
|
parameters = new DynamicParameters();
|
|
parameters.Add("@UserID", userId);
|
|
|
|
this.db.Execute("DeleteUserFromTECNReport", parameters, commandType: CommandType.StoredProcedure);
|
|
return;
|
|
}
|
|
public List<TrainingGroup> GetTrainingGroups()
|
|
{
|
|
//StringBuilder sql = new StringBuilder();
|
|
//sql.Append(
|
|
// "SELECT 'TrainingGroupID', TrainingGroupName " +
|
|
// "FROM TrainingGroups " +
|
|
// "ORDER BY TrainingGroupID ");
|
|
|
|
//db.Open();
|
|
//var lookup = new Dictionary<int, TrainingGroup>();
|
|
////List<TrainingGroup> data = this.db.Query<TrainingGroup>(sql.ToString()
|
|
//return this.db.Query<TrainingGroup>(sql.ToString()).ToList();
|
|
var TrainingGroups = from a in FabApprovalDB.TrainingGroups select a;
|
|
List<TrainingGroup> GroupsToReturn = TrainingGroups.ToList();
|
|
|
|
return GroupsToReturn;
|
|
}
|
|
public void AddNewTrainingGroup(string groupName)
|
|
{
|
|
TrainingGroup existing = null;
|
|
//Check to see that the group name doesn't exist.
|
|
try
|
|
{
|
|
existing = (from a in FabApprovalDB.TrainingGroups where a.TrainingGroupName == groupName select a).FirstOrDefault();
|
|
}
|
|
catch
|
|
{
|
|
// string test = "";
|
|
}
|
|
|
|
|
|
if (existing == null)
|
|
{
|
|
//string sql = new StringBuilder();
|
|
string sql = "INSERT INTO TrainingGroups (TrainingGroupName) " + "VALUES ('" + groupName + "') ";
|
|
|
|
|
|
this.db.Open();
|
|
this.db.Execute(sql);
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
public void DeleteTrainingGroup(int groupID)
|
|
{
|
|
try
|
|
{
|
|
string sql = "DELETE FROM TrainingGroups WHERE TrainingGroupID = " + groupID;
|
|
this.db.Open();
|
|
this.db.Execute(sql);
|
|
|
|
sql = "DELETE FROM TrainingGroupMembers WHERE TrainingGroupID = " + groupID;
|
|
this.db.Execute(sql);
|
|
return;
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
}
|
|
public List<TrainingGroupMember> GetTrainingGroupMembers(int GroupID)
|
|
{
|
|
return (from a in FabApprovalDB.TrainingGroupMembers where a.TrainingGroupID == GroupID select a).ToList();
|
|
}
|
|
public void AddUserToGroup(int userId, int groupId)
|
|
{
|
|
UserAccountDMO userDB = new UserAccountDMO();
|
|
string userFullName = userDB.GetUserByID(userId).FullName;
|
|
|
|
TrainingGroupMember existing = null;
|
|
|
|
existing = (from a in FabApprovalDB.TrainingGroupMembers where a.TrainingGroupID == groupId && a.UserID == userId select a).FirstOrDefault();
|
|
if (existing == null)
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
|
|
parameters = new DynamicParameters();
|
|
parameters.Add("@GroupID", groupId);
|
|
parameters.Add("@UserID", userId);
|
|
parameters.Add("@UserFullName", userFullName);
|
|
|
|
this.db.Execute("AddUserToTrainingGroup", parameters, commandType: CommandType.StoredProcedure);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("The user already exists in this training group.");
|
|
}
|
|
|
|
|
|
//if (existing == null)
|
|
//{
|
|
// //string sql = new StringBuilder();
|
|
|
|
// string sql = "INSERT INTO TrainingGroupMembers (TrainingGroupID, UserID, FullName) " + "VALUES ('" + groupId + "','" + userId + "','" + userFullName + "') ";
|
|
|
|
// try
|
|
// {
|
|
// this.db.Open();
|
|
// this.db.Execute(sql);
|
|
// }
|
|
// catch(Exception e)
|
|
// {
|
|
// return;
|
|
// }
|
|
// return;
|
|
//}
|
|
//else
|
|
//{
|
|
// return;
|
|
|
|
}
|
|
public void DeleteFromGroup(int userId, int groupId)
|
|
{
|
|
var parameters = new DynamicParameters();
|
|
|
|
parameters = new DynamicParameters();
|
|
parameters.Add("@GroupID", groupId);
|
|
parameters.Add("@UserID", userId);
|
|
|
|
this.db.Execute("DeleteUserFromTrainingGroup", parameters, commandType: CommandType.StoredProcedure);
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|