Added HttpException class for missing HttpException for net8 Wrapped HttpContext.Session, GetJsonResult, IsAjaxRequest and GetUserIdentityName in controllers for net8 Added AuthenticationService to test Fab2ApprovalMKLink code for net8 Compile conditionally flags to debug in dotnet core
583 lines
18 KiB
C#
583 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
|
|
using Dapper;
|
|
|
|
using Fab2ApprovalSystem.Misc;
|
|
using Fab2ApprovalSystem.Models;
|
|
|
|
namespace Fab2ApprovalSystem.DMO;
|
|
|
|
public class TrainingDMO {
|
|
private readonly IDbConnection db = new SqlConnection(GlobalVars.DB_CONNECTION_STRING);
|
|
#if !NET8
|
|
private static FabApprovalTrainingEntities FabApprovalDB = new FabApprovalTrainingEntities();
|
|
#endif
|
|
|
|
public int Create(int issueId) {
|
|
DynamicParameters parameters = new();
|
|
parameters.Add("@TrainingId", dbType: DbType.Int32, direction: ParameterDirection.InputOutput);
|
|
parameters.Add("@ECNNumber", issueId);
|
|
|
|
db.Execute("InsertTrainingItem", parameters, commandType: CommandType.StoredProcedure);
|
|
int trainingId = parameters.Get<int>("@TrainingId");
|
|
|
|
return trainingId;
|
|
}
|
|
|
|
public int CreateAssignment(int trainingId, int userId) {
|
|
DynamicParameters parameters = new();
|
|
parameters.Add("@TrainingID", trainingId);
|
|
parameters.Add("@UserID", userId);
|
|
parameters.Add("@AssignmentID", dbType: DbType.Int32, direction: ParameterDirection.InputOutput);
|
|
|
|
db.Execute("InsertTrainingAssignment", parameters, commandType: CommandType.StoredProcedure);
|
|
int assignmentId = parameters.Get<int>("@AssignmentID");
|
|
|
|
return assignmentId;
|
|
}
|
|
|
|
public IEnumerable<Training> GetAllTrainings() {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
IEnumerable<Training> allTrainings = (from a in db.Trainings select a).ToList();
|
|
|
|
return allTrainings;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public Training GetTraining(int trainingId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
Training trainingRecord = new Training();
|
|
trainingRecord = (from a in db.Trainings where a.TrainingID == trainingId select a).FirstOrDefault();
|
|
return trainingRecord;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public TrainingAssignment GetAssignment(int assignmentId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
TrainingAssignment assignmentRecord = (from a in db.TrainingAssignments where a.ID == assignmentId select a).FirstOrDefault();
|
|
return assignmentRecord;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public int GetTrainingId(int issueId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
int ECNId = issueId;
|
|
int trainingId = (from a in db.Trainings where a.ECN == ECNId select a.TrainingID).FirstOrDefault();
|
|
|
|
return trainingId;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<int> GetTrainees(int groupId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var users = (from a in db.TrainingGroupMembers where a.TrainingGroupID == groupId select a.UserID).ToList();
|
|
|
|
return users;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public bool isUserTrainingMember(int groupId, int userId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var users = (from a in db.TrainingGroupMembers where a.TrainingGroupID == groupId && a.UserID == userId select a).FirstOrDefault();
|
|
|
|
if (users == null) {
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<TrainingGroup> GetTrainingGroups() {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
return (from a in db.TrainingGroups select a).ToList();
|
|
#endif
|
|
|
|
#if NET8
|
|
System.Text.StringBuilder query = new("SELECT TrainingGroupID, TrainingGroupName FROM TrainingGroups ORDER BY TrainingGroupID");
|
|
return db.Query<TrainingGroup>(query.ToString()).ToList();
|
|
#endif
|
|
}
|
|
|
|
public TrainingGroup GetTrainingGroupByID(int groupId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
TrainingGroup groups = (from a in db.TrainingGroups where a.TrainingGroupID == groupId select a).FirstOrDefault();
|
|
return groups;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<int> GetECNAssignedTrainingGroups(int ECNNumber) {
|
|
#if !NET8
|
|
List<int> trainingGroups = (from a in FabApprovalDB.ECNTrainingBies where a.ECNNumber == ECNNumber select a.AcknowledgementTrainingByID).ToList();
|
|
|
|
return trainingGroups;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public void AddTrainingGroupToECN(int ECNNumber, int groupId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
|
|
var parameters = new DynamicParameters();
|
|
parameters.Add("@ECNNumber", ECNNumber);
|
|
parameters.Add("@TrainingByID", groupId);
|
|
|
|
this.db.Execute("ECNInsertTrainingBy", parameters, commandType: CommandType.StoredProcedure);
|
|
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public void SetTrainingFlag(int ECNNumber) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var parameters = new DynamicParameters();
|
|
parameters.Add("@ECNNumber", ECNNumber);
|
|
|
|
this.db.Execute("ECNSetTrainingFlag", parameters, commandType: CommandType.StoredProcedure);
|
|
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<TrainingAssignment> GetAllTrainingAssignments(int TrainingID) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var TrainingData = from a in db.TrainingAssignments where a.TrainingID == TrainingID select a;
|
|
|
|
RefreshTrainingData(TrainingID, TrainingData);
|
|
|
|
TrainingData = from a in db.TrainingAssignments where a.TrainingID == TrainingID select a;
|
|
|
|
return TrainingData.ToList();
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<TrainingAssignment> GetTrainingAssignments(int TrainingID) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var TrainingData = from a in db.TrainingAssignments where a.TrainingID == TrainingID && a.Deleted != true select a;
|
|
|
|
RefreshTrainingData(TrainingID, TrainingData);
|
|
|
|
TrainingData = from a in db.TrainingAssignments where a.TrainingID == TrainingID select a;
|
|
|
|
return TrainingData.ToList();
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<TrainingAssignment> GetTrainingAssignmentsByUser(int TrainingID, int userID) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var TrainingData = from a in db.TrainingAssignments where a.TrainingID == TrainingID && a.UserID == userID select a;
|
|
|
|
return TrainingData.ToList();
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<TrainingDocAck> GetAssignedDocs(int trainingAssignmentId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var docs = (from a in db.TrainingDocAcks where a.TrainingAssignmentID == trainingAssignmentId && a.Deleted != true select a).ToList();
|
|
|
|
return docs;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public void AcknowledgeDocument(int trainingDocAckID) {
|
|
DynamicParameters parameters = new();
|
|
parameters.Add("@TrainingDocAckID", trainingDocAckID);
|
|
|
|
db.Execute("TrainingAcknowledgeDocument", parameters, commandType: CommandType.StoredProcedure);
|
|
}
|
|
|
|
public void UpdateAssignmentStatus(int trainingAssignmentID) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var RecordToUpdate = (from a in db.TrainingAssignments where a.ID == trainingAssignmentID select a).SingleOrDefault();
|
|
if (RecordToUpdate.status != true) {
|
|
RecordToUpdate.DateCompleted = DateTime.Now;
|
|
RecordToUpdate.status = true;
|
|
try {
|
|
db.SaveChanges();
|
|
} catch (InvalidOperationException e) {
|
|
string exception = e.ToString();
|
|
Console.WriteLine(e);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public int GetTrainingIdByAssignment(int trainingAssignmentID) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
int trainingID = (from a in db.TrainingAssignments where a.ID == trainingAssignmentID select a.TrainingID).SingleOrDefault();
|
|
|
|
return trainingID;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public void UpdateTrainingStatus(int trainingId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
Training training = (from a in db.Trainings where a.TrainingID == trainingId select a).SingleOrDefault();
|
|
|
|
if (training != null) {
|
|
training.CompletedDate = DateTime.Now;
|
|
training.Status = true;
|
|
|
|
try {
|
|
db.SaveChanges();
|
|
} catch (InvalidOperationException e) {
|
|
string exception = e.ToString();
|
|
Console.WriteLine(e);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public void reOpenTraining(int trainingId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
Training training = (from a in db.Trainings where a.TrainingID == trainingId select a).SingleOrDefault();
|
|
|
|
if (training != null) {
|
|
training.CompletedDate = null;
|
|
training.Status = false;
|
|
|
|
try {
|
|
db.SaveChanges();
|
|
} catch (InvalidOperationException e) {
|
|
string exception = e.ToString();
|
|
Console.WriteLine(e);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public bool CheckTrainingStatus(int trainingAssignmentID) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
bool isFinished = true;
|
|
int trainingID = (from a in db.TrainingAssignments where a.ID == trainingAssignmentID select a.TrainingID).SingleOrDefault();
|
|
var trainingAssignments = from a in db.TrainingAssignments where a.TrainingID == trainingID && a.Deleted != true select a;
|
|
|
|
foreach (var training in trainingAssignments) {
|
|
if (training.status == false) {
|
|
isFinished = false;
|
|
}
|
|
}
|
|
return isFinished;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public bool CheckTrainingAssignmentStatus(int trainingAssignmentID) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
bool isFinished = true;
|
|
// TrainingDocAck docsAssigned = null;
|
|
|
|
var docsAssigned = from a in db.TrainingDocAcks where a.TrainingAssignmentID == trainingAssignmentID && a.Deleted != true select a;
|
|
|
|
foreach (var doc in docsAssigned) {
|
|
if (doc.Reviewed == false) {
|
|
isFinished = false;
|
|
}
|
|
}
|
|
|
|
return isFinished;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public bool IsUserAssigned(int userId, int trainingId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
bool userHasAssignment = false;
|
|
|
|
var assignments = (from a in db.TrainingAssignments where a.TrainingID == trainingId && a.UserID == userId && a.Deleted != true select a).ToList();
|
|
|
|
if (assignments.Count() > 0) {
|
|
userHasAssignment = true;
|
|
}
|
|
|
|
return userHasAssignment;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<Training> GetTrainings() {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var trainings = from a in db.Trainings select a;
|
|
|
|
return trainings.ToList();
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<TrainingAssignment> GetTrainingAssignmentsByUserID(int userID) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var trainingAssignments = from a in db.TrainingAssignments where a.UserID == userID && a.Deleted != true select a;
|
|
|
|
return trainingAssignments.ToList();
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public void DeleteTrainingAssignment(int trainingAssignmentId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var trainingAssignments = from a in db.TrainingAssignments where a.ID == trainingAssignmentId select a;
|
|
|
|
foreach (var item in trainingAssignments) {
|
|
item.Deleted = true;
|
|
item.DeletedDate = DateTime.Now;
|
|
}
|
|
try {
|
|
db.SaveChanges();
|
|
} catch (InvalidOperationException e) {
|
|
string exception = e.ToString();
|
|
Console.WriteLine(e);
|
|
}
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public void DeleteTrainingDocAck(int trainingAssignmentId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
var trainingDocAcks = from a in db.TrainingDocAcks where a.TrainingAssignmentID == trainingAssignmentId select a;
|
|
|
|
foreach (var item in trainingDocAcks) {
|
|
item.Deleted = true;
|
|
item.DeletedDate = DateTime.Now;
|
|
}
|
|
try {
|
|
db.SaveChanges();
|
|
} catch (InvalidOperationException e) {
|
|
string exception = e.ToString();
|
|
Console.WriteLine(e);
|
|
}
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public void DeleteTraining(int trainingId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
Training training = (from a in db.Trainings where a.TrainingID == trainingId select a).FirstOrDefault();
|
|
|
|
if (training != null) {
|
|
training.Deleted = true;
|
|
training.DeletedDate = DateTime.Now;
|
|
}
|
|
|
|
List<TrainingAssignment> trainingAssignments = (from a in db.TrainingAssignments where a.TrainingID == trainingId select a).ToList();
|
|
db.SaveChanges();
|
|
foreach (TrainingAssignment trainingAssignment in trainingAssignments) {
|
|
DeleteTrainingAssignment(trainingAssignment.ID);
|
|
DeleteTrainingDocAck(trainingAssignment.ID);
|
|
}
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public void DeleteAssignmentByUserId(int userId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
|
|
var userAssignments = (from a in db.TrainingAssignments where a.UserID == userId select a).ToList();
|
|
|
|
foreach (var item in userAssignments) {
|
|
// get document assignments
|
|
var docAssignments = (from a in db.TrainingDocAcks where a.TrainingAssignmentID == item.TrainingID select a).ToList();
|
|
// delete each docAssignment
|
|
foreach (var docAssignment in docAssignments) {
|
|
DeleteTrainingDocAck(docAssignment.ID);
|
|
}
|
|
DeleteTrainingAssignment(item.ID);
|
|
}
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public bool CheckValidDocAck(int docAckId) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
TrainingDocAck ack = (from a in db.TrainingDocAcks where a.ID == docAckId select a).FirstOrDefault();
|
|
|
|
// TODO Check the user is valid
|
|
|
|
// Check that the assignment exists
|
|
if (ack != null) {
|
|
// Check that the assignment isn't deleted
|
|
if (ack.Deleted == true) {
|
|
return false;
|
|
}
|
|
// Check that the assignment isn't completed
|
|
else if (ack.Reviewed == true) {
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<Training> GetAllOpenTrainings() {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
List<Training> openTrainings = (from a in db.Trainings where a.Status != true && a.Deleted != true select a).ToList();
|
|
return openTrainings;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
public List<TrainingAssignment> GetOpenAssignmentsByTrainingID(int trainingID) {
|
|
#if !NET8
|
|
FabApprovalTrainingEntities db = new FabApprovalTrainingEntities();
|
|
List<TrainingAssignment> openAssignments = (from a in FabApprovalDB.TrainingAssignments where a.TrainingID == trainingID && a.status != true && a.Deleted != true select a).ToList();
|
|
return openAssignments;
|
|
#endif
|
|
|
|
#if NET8
|
|
throw new NotImplementedException();
|
|
#endif
|
|
}
|
|
|
|
private void RefreshTrainingData(int TrainingID, IQueryable<TrainingAssignment> TrainingData) {
|
|
bool assignmentIsIncomplete = false;
|
|
UserAccountDMO userAccountDMO = new();
|
|
foreach (TrainingAssignment assignment in TrainingData) {
|
|
LoginModel userModel = userAccountDMO.GetUserByID(assignment.UserID);
|
|
if (!userModel.IsActive)
|
|
UpdateAssignmentStatus(assignment.ID);
|
|
if (assignment.Deleted != true && (assignment.DateCompleted is null || assignment.DateCompleted > DateTime.Now))
|
|
assignmentIsIncomplete = true;
|
|
}
|
|
|
|
if (!assignmentIsIncomplete)
|
|
UpdateTrainingStatus(TrainingID);
|
|
}
|
|
} |