/// <summary> /// Add Learning Map data along with the mapping details of all courses and trainees included in the Learning Map(if any). /// </summary> /// <param name="data">Learning Map object which contain Learning map data along with the Course list(if any) and Trainee /// list(if any) included in the Learning map</param> /// <returns>Id of the Learning map if successfully added otherwise zero</returns> public int AddLearningMap(LearningMap data) { try { using (var context = new TrainingTrackerEntities()) { var newLearningMapEntity = new EntityFramework.LearningMap { Title = data.Title, Notes = data.Notes, Duration = data.Duration, DateCreated = DateTime.Now, IsCourseRestricted = data.IsCourseRestricted, TeamId = data.TeamId, CreatedBy = data.CreatedBy, IsDeleted = data.IsDeleted, LearningMapCourseMappings = (data.Courses != null) ? data.Courses.Select(x => new LearningMapCourseMapping { //LearningMapId = newLearningMapEntity.Id, CourseId = x.Id, SortOrder = x.SortOrder, IsDeleted = false, DateInserted = DateTime.Now }).ToList() : null, LearningMapUserMappings = (data.Trainees != null) ? data.Trainees.Select(x => new LearningMapUserMapping { //LearningMapId = newLearningMapEntity.Id, UserId = x.UserId, DateInserted = DateTime.Now }).ToList() : null }; context.LearningMaps.Add(newLearningMapEntity); context.SaveChanges(); return(newLearningMapEntity.Id); } } catch (Exception ex) { LogUtility.ErrorRoutine(ex); return(0); } }
/// <summary> /// Updates Learning Map data along with the mapping details of all courses included in the Learning Map(if any) and /// Adds mapping details of new Trainees added in the Learning Map(if any) /// </summary> /// <param name="data">Learning Map object contains Learning map data with Course list(if any) and newly added Trainee list(if any). /// Trainee list should contains only those trainees which is newly added </param> /// <returns>True if Learning Map data along with the Course and User mapping data is updated successfully otherwise false</returns> public bool UpdateLearningMap(LearningMap data) { try { using (var context = new TrainingTrackerEntities()) { var learningMapEntity = context.LearningMaps.First(x => x.Id == data.Id); if (learningMapEntity == null) { return(false); } learningMapEntity.Title = data.Title; learningMapEntity.Notes = data.Notes; learningMapEntity.Duration = data.Duration; learningMapEntity.IsCourseRestricted = data.IsCourseRestricted; //removing the courses(data.Course) which is already in the learning map and make the IsDeleted feild true //in the entity(LearningMapCourseMappings) for the courses which is not present in the data.Courses learningMapEntity.LearningMapCourseMappings.Where(x => !x.IsDeleted && x.LearningMapId == data.Id) .ToList() .ForEach( x => { x.IsDeleted = true; if (data.Courses != null) { var course = data.Courses.Where(y => y.Id == x.CourseId) .FirstOrDefault(); if (course != null) { x.IsDeleted = false; data.Courses.Remove(course); } } } ); if (data.Courses != null) { var courseMappingEntity = data.Courses.Select(x => new LearningMapCourseMapping { LearningMapId = data.Id, CourseId = x.Id, SortOrder = x.SortOrder, IsDeleted = false, DateInserted = DateTime.Now }).ToList(); learningMapEntity.LearningMapCourseMappings = learningMapEntity.LearningMapCourseMappings.Concat(courseMappingEntity).ToList(); } // Trainees can only be added if (data.Trainees != null) { // remove the trainees(data.Trainee) which is already assigned for the current learning map learningMapEntity.LearningMapUserMappings.Where(x => x.LearningMapId == data.Id) .ToList() .ForEach( x => { var trainee = data.Trainees.Where(y => y.UserId == x.UserId) .FirstOrDefault(); if (trainee != null) { data.Trainees.Remove(trainee); } } ); var newTraineesMapping = data.Trainees.Select(x => new LearningMapUserMapping { LearningMapId = data.Id, UserId = x.UserId, DateInserted = DateTime.Now }).ToList(); learningMapEntity.LearningMapUserMappings = learningMapEntity.LearningMapUserMappings.Concat(newTraineesMapping).ToList(); } context.SaveChanges(); return(true); } } catch (Exception ex) { LogUtility.ErrorRoutine(ex); return(false); } }