public ResponseMessageDTO CompleteTraining(CompleteTrainingInputDTO completeTrainingInput) { try { if (completeTrainingInput.TrainingId <= 0) { return(new ResponseMessageDTO(false, L("CompleteTraining_Fail"))); } else { var training = _trainingRepository.Get(completeTrainingInput.TrainingId); if (training.StatusId == _trainingStatusRepository.GetTrainingStatusIdByName(TrainingStatus.CANCELED) || training.StatusId == _trainingStatusRepository.GetTrainingStatusIdByName(TrainingStatus.PLANNED)) { //training already canceled or planed -> cant completed return(new ResponseMessageDTO(false, L(""))); } else { //Update status of training to completed training.StatusId = _trainingStatusRepository.GetTrainingStatusIdByName(TrainingStatus.COMPLETED); var updateTraining = _trainingRepository.Update(training); if (updateTraining.Id == 0) { return(new ResponseMessageDTO(false, L("CreatTraining"))); } #region Update Competencies of trainees assigned //Get list of trainees selected by user to update Competencies TraineeTrainingOutcomeDTO[] trainees = JsonConvert.DeserializeObject <TraineeTrainingOutcomeDTO[]>(completeTrainingInput.TraineeTrainingOutcome); //Update Competencies of trainees above foreach (TraineeTrainingOutcomeDTO trainee in trainees.ToList()) { //Get the list of Competencies that the trainee used to update List <int> competenciesUpdate = new List <int>(); var traineeEntity = _traineeRepository.Get(trainee.TraineeId); //Get the list of Competencies that the trainee already has int[] sourceCompetencies = traineeEntity.ArrayOfCompetence; //Compare the sourceCompetencies with the selectedCompetencies to determine the //Competencies which used to add to the trainee int[] missingCompetencies = _compareAppService.missingCompetencies(sourceCompetencies, trainee.SelectedCompetencies); //Add there missing Competencies to the list source Competencies List <int> listSourceCompetencies = sourceCompetencies.ToList(); List <int> listMissingCompetencies = missingCompetencies.ToList(); foreach (int competence in listMissingCompetencies) { listSourceCompetencies.Add(competence); } //Use the list source Competencies to update competenciesUpdate = listSourceCompetencies.Distinct().ToList(); //Update trainee's Competencies traineeEntity.ArrayOfCompetence = competenciesUpdate.ToArray(); var udTrainee = _traineeRepository.Update(traineeEntity); if (udTrainee.Id == 0) { return(new ResponseMessageDTO(false, L("UpdateTraining_FailError"))); } } #endregion #region Update trainings targeted and training attended of trainees assigned List <int> listTraineesIDAssigned = updateTraining.ArrayOfAssignedTrainees.ToList(); foreach (int traineeIDAssigned in listTraineesIDAssigned) { var traineeAssigned = _traineeRepository.Get(traineeIDAssigned); List <int> listTrainingsTargeted = traineeAssigned.ArrayOfTargetedTraining.ToList(); List <int> listTrainingsAttended = traineeAssigned.ArrayOfAttendedTraining.ToList(); var itemToRemove = listTrainingsTargeted.SingleOrDefault(r => r == completeTrainingInput.TrainingId); if (itemToRemove != 0) { //Remove training from training targeted listTrainingsTargeted.Remove(itemToRemove); } //Add training to training attended listTrainingsAttended.Add(completeTrainingInput.TrainingId); listTrainingsAttended = listTrainingsAttended.Distinct().ToList(); //update trainee traineeAssigned.ArrayOfTargetedTraining = listTrainingsTargeted.ToArray(); traineeAssigned.ArrayOfAttendedTraining = listTrainingsAttended.ToArray(); var updateOldTrainee = _traineeRepository.Update(traineeAssigned); if (updateOldTrainee.Id == 0) { return(new ResponseMessageDTO(false, L("UpdateTraining_FailError"))); } } #endregion return(new ResponseMessageDTO(true, L("UpdateTraining_Success"))); } } } catch { return(new ResponseMessageDTO(false, L("UpdateTraining_FailError"))); } }
public ResponseMessageDTO CompleteTraining(CompleteTrainingInputDTO completeTrainingInput) { return(_trainingAppService.CompleteTraining(completeTrainingInput)); }