Ejemplo n.º 1
0
        public async Task<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 selectedUserProfile = _userprofileRepo.GetUserProfileById(body.UserProfileId);
            var isValidUserProfile = selectedUserProfile != null && selectedUserProfile.Subscriptions != null;
            if (!isValidUserProfile) return addCourseFailRespond;

            var now = _dateTime.GetCurrentTime();
            var selectedStudentKey = _studentKeyRepo.GetStudentKeyByCodeAndGrade(body.Code, body.Grade);
            var isStudentKey = selectedStudentKey != null && !selectedStudentKey.DeletedDate.HasValue;
            var isSuccessed = isStudentKey ?
                addNewCourseByStudentCode(selectedUserProfile, selectedStudentKey, now) :
                await addNewCourseByTeacherCode(body.Code, body.Grade, selectedUserProfile, now);
            return new AddCourseRespond
            {
                Code = body.Code,
                Grade = body.Grade,
                IsSuccess = isSuccessed
            };
        }
Ejemplo n.º 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
            };
        }