/// <summary> /// อัพเดทข้อมูล Class room /// </summary> /// <param name="data">ข้อมูลที่จะทำการอัพเดท</param> public void UpsertClassRoom(ClassRoom data) { var update = Builders<ClassRoom>.Update .Set(it => it.Name, data.Name) .Set(it => it.CourseCatalogId, data.CourseCatalogId) .Set(it => it.DeletedDate, data.DeletedDate) .Set(it => it.Message, data.Message) .Set(it => it.Lessons, data.Lessons) .Set(it => it.LastUpdatedMessageDate, data.LastUpdatedMessageDate); var updateOption = new UpdateOptions { IsUpsert = true }; MongoAccess.MongoUtil.Instance.GetCollection<ClassRoom>(TableName) .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; }
/// <summary> /// สร้าง Class room ใหม่ /// </summary> /// <param name="data">ข้อมูล Class room ที่ต้องการสร้าง</param> public async Task CreateNewClassRoom(ClassRoom data) { await _mongoUtil.GetCollection<ClassRoom>(TableName).InsertOneAsync(data); }
private ClassCalendar createClassCalendar(ClassRoom classRoom, IEnumerable<LessonCatalog> lessonCatalogs, DateTime currentTime) { var lessonOrderRunner = 1; var lessonCalendars = classRoom.Lessons.Select(it => { var lessonCatalog = lessonCatalogs.FirstOrDefault(lc => lc.id == it.LessonCatalogId); if (lessonCatalog == null) return null; var topicOfTheDays = lessonCatalog.TopicOfTheDays.Select(totd => new ClassCalendar.TopicOfTheDay { id = Guid.NewGuid().ToString(), CreatedDate = currentTime, Message = totd.Message, SendOnDay = totd.SendOnDay, RequiredSendTopicOfTheDayDate = currentTime }).ToList(); var result = new ClassCalendar.LessonCalendar { id = Guid.NewGuid().ToString(), Order = lessonOrderRunner++, BeginDate = currentTime.Date, CreatedDate = currentTime, LessonId = it.id, SemesterGroupName = lessonCatalog.SemesterName, TopicOfTheDays = topicOfTheDays }; return result; }).Where(it => it != null).ToList(); var classCalendar = new ClassCalendar { id = Guid.NewGuid().ToString(), BeginDate = currentTime.Date, ClassRoomId = classRoom.id, CreatedDate = currentTime, Holidays = Enumerable.Empty<DateTime>(), ShiftDays = Enumerable.Empty<DateTime>(), LessonCalendars = lessonCalendars, }; return classCalendar; }
private IEnumerable<UserProfile.Subscription> addNewSelfPurchaseSubscription(IEnumerable<UserProfile.Subscription> subscriptions, ClassRoom selectedClassRoom, string classCalendarId, string courseCatalogId, DateTime currentTime, string newSubscriptionId) { var newSubscriptions = subscriptions.ToList(); var newSubscription = new UserProfile.Subscription { id = newSubscriptionId, Role = UserProfile.AccountRole.SelfPurchaser, LastActiveDate = currentTime, ClassRoomId = selectedClassRoom.id, ClassCalendarId = classCalendarId, CreatedDate = currentTime, ClassRoomName = selectedClassRoom.Name, CourseCatalogId = courseCatalogId }; newSubscriptions.Add(newSubscription); return newSubscriptions; }
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; }