/// <summary> /// อัพเดทหรือเพิ่มข้อมูล like lesson /// </summary> /// <param name="data">ข้อมูลที่ต้องการดำเนินการ</param> public void UpsertLikeComment(LikeComment data) { var update = Builders<LikeComment>.Update .Set(it => it.ClassRoomId, data.ClassRoomId) .Set(it => it.CommentId, data.CommentId) .Set(it => it.LessonId, data.LessonId) .Set(it => it.LikedByUserProfileId, data.LikedByUserProfileId) .Set(it => it.LastNotifyRequest, data.LastNotifyRequest) .Set(it => it.LastNotifyComplete, data.LastNotifyComplete) .Set(it => it.CreatedDate, data.CreatedDate) .Set(it => it.DeletedDate, data.DeletedDate); var updateOption = new UpdateOptions { IsUpsert = true }; MongoAccess.MongoUtil.Instance.GetCollection<LikeComment>(TableName) .UpdateOne(it => it.id == data.id, update, updateOption); }
public void Like(LikeCommentRequest body) { var areArgumentsValid = body != null && !string.IsNullOrEmpty(body.ClassRoomId) && !string.IsNullOrEmpty(body.CommentId) && !string.IsNullOrEmpty(body.LessonId) && !string.IsNullOrEmpty(body.UserProfileId); if (!areArgumentsValid) 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 selectedComment = _commentRepo.GetCommentById(body.CommentId); if (selectedComment == null) return; var likeComments = _likeCommentRepo.GetLikeCommentByCommentId(body.CommentId) .Where(it => !it.DeletedDate.HasValue) .ToList(); var likedCommentsByThisUser = likeComments .Where(it => it.LikedByUserProfileId.Equals(body.UserProfileId, StringComparison.CurrentCultureIgnoreCase)); var isUnlike = likedCommentsByThisUser.Any(); if (isUnlike) { foreach (var item in likedCommentsByThisUser) { item.DeletedDate = now; _likeCommentRepo.UpsertLikeComment(item); } } else { var isCommentOwner = selectedComment.CreatedByUserProfileId.Equals(body.UserProfileId, StringComparison.CurrentCultureIgnoreCase); if (!isCommentOwner) { var canLikeThisComment = _userprofileRepo.CheckAccessPermissionToUserProfile(selectedComment.CreatedByUserProfileId); if (!canLikeThisComment) return; } 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 LikeComment { id = Guid.NewGuid().ToString(), ClassRoomId = body.ClassRoomId, LessonId = body.LessonId, CommentId = body.CommentId, LikedByUserProfileId = body.UserProfileId, CreatedDate = now }; likeComments.Add(newLike); _likeCommentRepo.UpsertLikeComment(newLike); } selectedComment.TotalLikes = likeComments.Where(it => !it.DeletedDate.HasValue).Count(); _commentRepo.UpsertComment(selectedComment); _notificationCtrl.SendNotification(); }