public void Post(PostNewCourseMessageRequest body) { var areArgumentsValid = body != null && !string.IsNullOrEmpty(body.ClassRoomId) && !string.IsNullOrEmpty(body.Message) && !string.IsNullOrEmpty(body.UserProfileId); if (!areArgumentsValid) { return; } UserProfile userprofile; var canAccessToTheClassRoom = _userprofileRepo.CheckAccessPermissionToSelectedClassRoom(body.UserProfileId, body.ClassRoomId, out userprofile); if (!canAccessToTheClassRoom) { return; } var isTeacherAccount = userprofile.Subscriptions.First(it => it.ClassRoomId == body.ClassRoomId).Role == UserProfile.AccountRole.Teacher; if (!isTeacherAccount) { return; } var selectedClassRoom = _classRoomRepo.GetClassRoomById(body.ClassRoomId); if (selectedClassRoom == null) { return; } selectedClassRoom.Message = body.Message; selectedClassRoom.LastUpdatedMessageDate = _dateTime.GetCurrentTime(); _classRoomRepo.UpsertClassRoom(selectedClassRoom); }
public void Post(LikeLessonRequest body) { var isArgumentValid = body != null && !string.IsNullOrEmpty(body.ClassRoomId) && !string.IsNullOrEmpty(body.LessonId) && !string.IsNullOrEmpty(body.UserProfileId); if (!isArgumentValid) { return; } var canAccessToTheClassRoom = _userprofileRepo.CheckAccessPermissionToSelectedClassRoom(body.UserProfileId, body.ClassRoomId); if (!canAccessToTheClassRoom) { return; } var now = _dateTime.GetCurrentTime(); var canAccessToTheClassLesson = _classCalendarRepo.CheckAccessPermissionToSelectedClassLesson(body.ClassRoomId, body.LessonId, now); if (!canAccessToTheClassLesson) { return; } var selectedClassRoom = _classRoomRepo.GetClassRoomById(body.ClassRoomId); var isLikeConditionValid = selectedClassRoom != null && selectedClassRoom.Lessons.Any(it => it.id == body.LessonId); if (!isLikeConditionValid) { return; } var likeLessons = _likeLessonRepo.GetLikeLessonsByLessonId(body.LessonId) .Where(it => !it.DeletedDate.HasValue) .ToList(); if (likeLessons == null) { return; } var likedLessonsByThisUser = likeLessons .Where(it => it.LikedByUserProfileId.Equals(body.UserProfileId, StringComparison.CurrentCultureIgnoreCase)); var isUnlike = likedLessonsByThisUser.Any(); if (isUnlike) { foreach (var item in likedLessonsByThisUser) { item.DeletedDate = now; _likeLessonRepo.UpsertLikeLesson(item); } } else { var selectedUserActivity = _userActivityRepo.GetUserActivityByUserProfileIdAndClassRoomId(body.UserProfileId, body.ClassRoomId); if (selectedUserActivity == null) { return; } var selectedLessonActivity = selectedUserActivity.LessonActivities.FirstOrDefault(it => it.LessonId.Equals(body.LessonId)); if (selectedLessonActivity == null) { return; } selectedLessonActivity.ParticipationAmount++; _userActivityRepo.UpsertUserActivity(selectedUserActivity); var newLike = new LikeLesson { id = Guid.NewGuid().ToString(), ClassRoomId = body.ClassRoomId, LessonId = body.LessonId, LikedByUserProfileId = body.UserProfileId, CreatedDate = now }; likeLessons.Add(newLike); _likeLessonRepo.UpsertLikeLesson(newLike); } var selectedLesson = selectedClassRoom.Lessons.First(it => it.id == body.LessonId); selectedLesson.TotalLikes = likeLessons.Where(it => !it.DeletedDate.HasValue).Count(); _classRoomRepo.UpsertClassRoom(selectedClassRoom); _notificationCtrl.SendNotification(); }