public static bool CheckAccessPermissionToSelectedClassRoom(this IUserProfileRepository userprofileRepo, string userprofileId, string classRoomId, out UserProfile userprofile) { userprofile = null; var areArgumentsValid = !string.IsNullOrEmpty(userprofileId) && !string.IsNullOrEmpty(classRoomId); if (!areArgumentsValid) return false; var selectedUserProfile = userprofileRepo.GetUserProfileById(userprofileId); if (selectedUserProfile == null) return false; userprofile = selectedUserProfile; var canAccessToTheClass = selectedUserProfile .Subscriptions .Where(it => it.ClassRoomId.Equals(classRoomId, StringComparison.CurrentCultureIgnoreCase)) .Any(); return canAccessToTheClass; }
/// <summary> /// อัพเดทหรือเพิ่มข้อมูลผู้ใช้ /// </summary> /// <param name="data">ข้อมูลที่ต้องการดำเนินการ</param> public void UpsertUserProfile(UserProfile data) { var update = Builders<UserProfile>.Update .Set(it => it.Name, data.Name) .Set(it => it.SchoolName, data.SchoolName) .Set(it => it.ImageProfileUrl, data.ImageProfileUrl) .Set(it => it.IsPrivateAccount, data.IsPrivateAccount) .Set(it => it.IsEnableNotification, data.IsEnableNotification) .Set(it => it.CourseReminder, data.CourseReminder) .Set(it => it.CreatedDate, data.CreatedDate) .Set(it => it.DeletedDate, data.DeletedDate) .Set(it => it.Subscriptions, data.Subscriptions); var updateOption = new UpdateOptions { IsUpsert = true }; MongoAccess.MongoUtil.Instance.GetCollection<UserProfile>(UserProfileTableName) .UpdateOne(it => it.id == data.id, update, updateOption); }
private IEnumerable<UserProfile.Subscription> createNewSubscription(IEnumerable<UserProfile.Subscription> subscriptions, UserProfile.AccountRole role, ClassRoom classRoom, string classCalendarId, string licenseId, DateTime currentTime) { var result = subscriptions.ToList(); result.Add(new UserProfile.Subscription { id = Guid.NewGuid().ToString(), Role = role, LastActiveDate = currentTime, ClassRoomId = classRoom.id, ClassCalendarId = classCalendarId, CreatedDate = currentTime, LicenseId = licenseId, ClassRoomName = classRoom.Name, CourseCatalogId = classRoom.CourseCatalogId }); return result; }
private async Task<bool> addNewCourseByTeacherCode(string code, string grade, UserProfile selectedUserProfile, DateTime currentTime) { var selectedContract = await _contractRepo.GetContractsByTeacherCode(code, grade); var isTeacherKey = selectedContract != null && !selectedContract.DeletedDate.HasValue && selectedContract.Licenses.Any(); if (!isTeacherKey) return false; var selectedLicense = selectedContract.Licenses .Where(it => !it.DeletedDate.HasValue) .FirstOrDefault(it => it.TeacherKeys.Any(t => !t.DeletedDate.HasValue && t.Code == code && t.Grade == grade)); if (selectedLicense == null) return false; var selectedTeacherKey = selectedLicense.TeacherKeys .Where(it => !it.DeletedDate.HasValue) .OrderBy(it => it.CreatedDate) .LastOrDefault(); if (selectedTeacherKey == null) return false; // Create new ClassRoom var selectedCourseCatalog = _courseCatalogRepo.GetCourseCatalogById(selectedLicense.CourseCatalogId); if (selectedCourseCatalog == null) return false; var lessonCatalogs = _lessonCatalogRepo.GetLessonCatalogByCourseCatalogId(selectedCourseCatalog.id).ToList(); if (lessonCatalogs == null || !lessonCatalogs.Any()) return false; var lessons = lessonCatalogs .Where(it => !it.DeletedDate.HasValue) .Select(it => new ClassRoom.Lesson { id = Guid.NewGuid().ToString(), LessonCatalogId = it.id }).ToList(); var newClassRoom = new ClassRoom { id = Guid.NewGuid().ToString(), Name = selectedCourseCatalog.SideName, CourseCatalogId = selectedCourseCatalog.id, CreatedDate = currentTime, LastUpdatedMessageDate = currentTime, Lessons = lessons }; await _classRoomRepo.CreateNewClassRoom(newClassRoom); // Create new ClassCalendar var lessonCalendars = lessonCatalogs .Where(it => !it.DeletedDate.HasValue) .Select(lesson => { var selectedLesson = lessons.FirstOrDefault(it => it.LessonCatalogId == lesson.id); return new ClassCalendar.LessonCalendar { id = Guid.NewGuid().ToString(), Order = lesson.Order, SemesterGroupName = lesson.SemesterName, BeginDate = currentTime, CreatedDate = currentTime, LessonId = selectedLesson.id, TopicOfTheDays = lesson.TopicOfTheDays .Where(it => !it.DeletedDate.HasValue) .Select(it => new ClassCalendar.TopicOfTheDay { id = it.id, Message = it.Message, SendOnDay = it.SendOnDay, CreatedDate = currentTime, }) }; }).ToList(); var newClassCalendar = new ClassCalendar { id = Guid.NewGuid().ToString(), ClassRoomId = newClassRoom.id, CreatedDate = currentTime, Holidays = Enumerable.Empty<DateTime>(), ShiftDays = Enumerable.Empty<DateTime>(), LessonCalendars = lessonCalendars }; await _classCalendarRepo.CreateNewClassCalendar(newClassCalendar); // Create new UserActivity var userActivity = selectedUserProfile.CreateNewUserActivity(newClassRoom, newClassCalendar, lessonCatalogs, currentTime); _userActivityRepo.UpsertUserActivity(userActivity); // Create new subscription var subscriptions = createNewSubscription(selectedUserProfile.Subscriptions, UserProfile.AccountRole.Teacher, newClassRoom, newClassCalendar.id, selectedLicense.id, currentTime); selectedUserProfile.Subscriptions = subscriptions; _userprofileRepo.UpsertUserProfile(selectedUserProfile); // Create new student key var newStudentKey = new StudentKey { id = Guid.NewGuid().ToString(), Grade = grade, Code = generateStudentCode(grade), CourseCatalogId = selectedCourseCatalog.id, ClassRoomId = newClassRoom.id, CreatedDate = currentTime }; await _studentKeyRepo.CreateNewStudentKey(newStudentKey); return true; }
private bool addNewCourseByStudentCode(UserProfile selectedUserProfile, StudentKey selectedStudentKey, DateTime currentTime) { var canUserprofileAccessToTheClassRoom = selectedUserProfile.Subscriptions .Where(it => !it.DeletedDate.HasValue) .Any(it => it.ClassRoomId == selectedStudentKey.ClassRoomId); if (canUserprofileAccessToTheClassRoom) return false; var selectedClassRoom = _classRoomRepo.GetClassRoomById(selectedStudentKey.ClassRoomId); if (selectedClassRoom == null) return false; var selectedClassCalendar = _classCalendarRepo.GetClassCalendarByClassRoomId(selectedStudentKey.ClassRoomId); if (selectedClassCalendar == null) return false; var lessonCatalogs = selectedClassRoom.Lessons .Select(it => _lessonCatalogRepo.GetLessonCatalogById(it.LessonCatalogId)) .ToList(); if (lessonCatalogs.Any(it => it == null)) return false; var teacherSubscription = _userprofileRepo.GetUserProfilesByClassRoomId(selectedClassRoom.id) .Where(it => !it.DeletedDate.HasValue) .SelectMany(it => it.Subscriptions) .Where(it => !it.DeletedDate.HasValue) .Where(it => it.Role == UserProfile.AccountRole.Teacher) .Where(it => it.ClassRoomId == selectedClassRoom.id) .FirstOrDefault(); if (teacherSubscription == null) { _logger.LogWarning($"Teacher account in the private ClassRoomId: { selectedStudentKey.ClassRoomId } not found."); return false; } selectedUserProfile.Subscriptions = createNewSubscription(selectedUserProfile.Subscriptions, UserProfile.AccountRole.Student, selectedClassRoom, selectedClassCalendar.id, teacherSubscription.LicenseId, currentTime); _userprofileRepo.UpsertUserProfile(selectedUserProfile); var userActivity = selectedUserProfile.CreateNewUserActivity(selectedClassRoom, selectedClassCalendar, lessonCatalogs, currentTime); _userActivityRepo.UpsertUserActivity(userActivity); return true; }
private bool validateAccessToCourseScheduleManagement(string userprofileId, string classRoomId, out UserProfile userprofile) { var canAccessToTheClassRoom = _userprofileRepo .CheckAccessPermissionToSelectedClassRoom(userprofileId, classRoomId, out userprofile); if (!canAccessToTheClassRoom) return false; var isTeacher = userprofile.Subscriptions .Where(it => !it.DeletedDate.HasValue && it.ClassRoomId == classRoomId) .Any(it => it.Role == UserProfile.AccountRole.Teacher); return isTeacher; }