public CourseModuleAchievementViewModel(UserCourseModuleAchievement moduleAchievement)
 {
     Key = moduleAchievement.Key;
     CourseAchievementKey = moduleAchievement.UserCourseAchievementKey;
     CourseModuleKey      = moduleAchievement.CourseModuleKey;
     CompletedDate        = moduleAchievement.CompletedDate;
 }
        private void CompareAndPrepareCourseAchievement(
            UserCourseAchievement courseAchievementCandidate,
            int courseModuleKey,
            User user,
            IQueryable <UserCourseAchievement> existingAchievements)
        {
            var courseKey         = courseAchievementCandidate.CourseKey;
            var courseAchievement = existingAchievements.FirstOrDefault(x => x.CourseKey == courseKey);

            if (courseAchievement == null)
            {
                courseAchievement = new UserCourseAchievement
                {
                    CourseKey = courseKey,
                    UserKey   = user.Key
                };
                _courseCourseAchievementRepository.InsertOnCommit(courseAchievement);
            }


            if (courseAchievement.CourseModuleAchievements == null)
            {
                courseAchievement.CourseModuleAchievements = new List <UserCourseModuleAchievement>();
            }

            UserCourseModuleAchievement moduleAchievement = courseAchievement.CourseModuleAchievements.OrEmptyListIfNull().FirstOrDefault(a => a.CourseModuleKey == courseModuleKey);

            if (moduleAchievement == null)
            {
                moduleAchievement = new UserCourseModuleAchievement
                {
                    UserCourseAchievement = courseAchievement,
                    CourseModuleKey       = courseModuleKey
                };

                courseAchievement.CourseModuleAchievements.Add(moduleAchievement);
            }

            moduleAchievement.CompletedDate = DateTime.UtcNow;

            var course = _courseRepository.GetAll().Include(c => c.CourseModules).FirstOrDefault(x => x.Key == courseKey);

            // should not be null
            if (course != null)
            {
                if (courseAchievement.CourseModuleAchievements.Count >= course.CourseModules.Count)
                {
                    courseAchievement.Completed     = true;
                    courseAchievement.CompletedDate = DateTime.UtcNow;
                }
            }
        }