/// <summary>
        /// อัพเดทหรือเพิ่มข้อมูลกิจกรรม
        /// </summary>
        /// <param name="data">ข้อมูลกิจกรรมที่ต้องการดำเนินการ</param>
        public void UpsertUserActivity(UserActivity data)
        {
            var update = Builders<UserActivity>.Update
              .Set(it => it.IsTeacher, data.IsTeacher)
              .Set(it => it.IsPrivateAccount, data.IsPrivateAccount)
              .Set(it => it.UserProfileName, data.UserProfileName)
              .Set(it => it.UserProfileImageUrl, data.UserProfileImageUrl)
              .Set(it => it.HideClassRoomMessageDate, data.HideClassRoomMessageDate)
              .Set(it => it.UserProfileId, data.UserProfileId)
              .Set(it => it.ClassRoomId, data.ClassRoomId)
              .Set(it => it.CreatedDate, data.CreatedDate)
              .Set(it => it.DeletedDate, data.DeletedDate)
              .Set(it => it.LessonActivities, data.LessonActivities);

            var updateOption = new UpdateOptions { IsUpsert = true };
            _mongoUtil.GetCollection<UserActivity>(TableName)
               .UpdateOne(it => it.id == data.id, update, updateOption);
        }
Пример #2
0
        public AddCourseRespond AddCourse(AddCourseRequest body)
        {
            var addCourseFailRespond = new AddCourseRespond
            {
                Code = body.Code,
                Grade = body.Grade,
            };
            var areArgumentsValid = body != null
                && !string.IsNullOrEmpty(body.UserProfileId)
                && !string.IsNullOrEmpty(body.Code)
                && !string.IsNullOrEmpty(body.Grade);
            if (!areArgumentsValid) return addCourseFailRespond;

            var selectedStudentKey = _studentKeyRepo.GetStudentKeyByCodeAndGrade(body.Code, body.Grade);
            if (selectedStudentKey == null) return addCourseFailRespond;

            var selectedClassRoom = _classRoomRepo.GetClassRoomById(selectedStudentKey.ClassRoomId);
            if (selectedClassRoom == null) return addCourseFailRespond;

            var selectedClassCalendar = _classCalendarRepo.GetClassCalendarByClassRoomId(selectedStudentKey.ClassRoomId);
            if (selectedClassCalendar == null) return addCourseFailRespond;

            var selectedUserProfile = _userprofileRepo.GetUserProfileById(body.UserProfileId);
            var canUseTheCode = selectedUserProfile != null
                && selectedUserProfile.Subscriptions != null
                && selectedUserProfile.Subscriptions.All(it => it.ClassRoomId != selectedStudentKey.ClassRoomId);
            if (!canUseTheCode) return addCourseFailRespond;

            var lessonCatalogs = selectedClassRoom.Lessons
                .Select(it => _lessonCatalogRepo.GetLessonCatalogById(it.LessonCatalogId))
                .ToList();
            if (lessonCatalogs.Any(it => it == null)) return addCourseFailRespond;

            var now = _dateTime.GetCurrentTime();
            var subscriptions = selectedUserProfile.Subscriptions.ToList();
            subscriptions.Add(new UserProfile.Subscription
            {
                id = Guid.NewGuid().ToString(),
                Role = UserProfile.AccountRole.Student,
                LastActiveDate = now,
                ClassRoomId = selectedClassRoom.id,
                ClassCalendarId = selectedClassCalendar.id,
                CreatedDate = now,
                ClassRoomName = selectedClassRoom.Name,
                CourseCatalogId = selectedClassRoom.CourseCatalogId
            });
            selectedUserProfile.Subscriptions = subscriptions;
            _userprofileRepo.UpsertUserProfile(selectedUserProfile);

            const int PrimaryContent = 1;
            var lessonActivities = selectedClassRoom.Lessons.Select(lesson =>
            {
                var selectedLessonCalendar = selectedClassCalendar.LessonCalendars
                    .Where(it => !it.DeletedDate.HasValue)
                    .FirstOrDefault(lc => lc.LessonId == lesson.id);

                var selectedLessonCatalog = lessonCatalogs
                    .FirstOrDefault(it => it.id == lesson.LessonCatalogId);

                return new UserActivity.LessonActivity
                {
                    id = Guid.NewGuid().ToString(),
                    BeginDate = selectedLessonCalendar.BeginDate,
                    TotalContentsAmount = selectedLessonCatalog.ExtraContentUrls.Count() + PrimaryContent,
                    LessonId = lesson.id,
                    SawContentIds = Enumerable.Empty<string>()
                };
            }).ToList();

            var userActivity = new UserActivity
            {
                id = Guid.NewGuid().ToString(),
                UserProfileName = selectedUserProfile.Name,
                UserProfileImageUrl = selectedUserProfile.ImageProfileUrl,
                UserProfileId = selectedUserProfile.id,
                ClassRoomId = selectedClassRoom.id,
                CreatedDate = now,
                LessonActivities = lessonActivities
            };
            _userActivityRepo.UpsertUserActivity(userActivity);

            return new AddCourseRespond
            {
                Code = body.Code,
                Grade = body.Grade,
                IsSuccess = true
            };
        }
Пример #3
0
        public static UserActivity CreateNewUserActivity(this UserProfile selectedUserProfile, ClassRoom selectedClassRoom, ClassCalendar selectedClassCalendar, List<LessonCatalog> lessonCatalogs, DateTime now)
        {
            var lessonActivities = selectedClassRoom.Lessons.Select(lessonInClassRoom =>
            {
                var selectedLessonCalendar = selectedClassCalendar.LessonCalendars
                    .Where(it => !it.DeletedDate.HasValue)
                    .FirstOrDefault(lessonCalendar => lessonCalendar.LessonId == lessonInClassRoom.id);

                var selectedLessonCatalog = lessonCatalogs.FirstOrDefault(it => it.id == lessonInClassRoom.LessonCatalogId);

                var totalContentsAmount = selectedLessonCatalog.PostAssessments.Count()
                    + selectedLessonCatalog.PreAssessments.Count()
                    + selectedLessonCatalog.StudentItems.Count()
                    + selectedLessonCatalog.TeacherItems.Count();
                return new UserActivity.LessonActivity
                {
                    id = Guid.NewGuid().ToString(),
                    BeginDate = selectedLessonCalendar.BeginDate,
                    TotalContentsAmount = totalContentsAmount,
                    LessonId = lessonInClassRoom.id,
                    SawContentIds = Enumerable.Empty<string>()
                };
            }).ToList();

            var userActivity = new UserActivity
            {
                id = Guid.NewGuid().ToString(),
                UserProfileName = selectedUserProfile.Name,
                UserProfileImageUrl = selectedUserProfile.ImageProfileUrl,
                UserProfileId = selectedUserProfile.id,
                ClassRoomId = selectedClassRoom.id,
                CreatedDate = now,
                LessonActivities = lessonActivities
            };

            return userActivity;
        }