コード例 #1
0
        public void WhenUserProfileIdClickTheUnlikeButtonDiscussionForCommentInTheLessonOfClassRoom(string userprofileId, string discussionId, string commentId, string lessonId, string classRoomId)
        {
            var mockLikeDiscussionRepo = ScenarioContext.Current.Get<Moq.Mock<ILikeDiscussionRepository>>();
            mockLikeDiscussionRepo.Setup(it => it.UpsertLikeDiscussion(It.IsAny<LikeDiscussion>()));

            var mockCommentRepo = ScenarioContext.Current.Get<Moq.Mock<ICommentRepository>>();
            mockCommentRepo.Setup(it => it.UpsertComment(It.IsAny<Comment>()));

            var discussionCtrl = ScenarioContext.Current.Get<DiscussionController>();
            var body = new LikeDiscussionRequest
            {
                ClassRoomId = classRoomId,
                LessonId = lessonId,
                CommentId = commentId,
                DiscussionId = discussionId,
                UserProfileId = userprofileId
            };
            discussionCtrl.Like(body);
        }
コード例 #2
0
        public void Like(LikeDiscussionRequest body)
        {
            var areArgumentsValid = body != null
                && !string.IsNullOrEmpty(body.ClassRoomId)
                && !string.IsNullOrEmpty(body.CommentId)
                && !string.IsNullOrEmpty(body.DiscussionId)
                && !string.IsNullOrEmpty(body.UserProfileId)
                && !string.IsNullOrEmpty(body.LessonId);
            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 selectedDiscussion = selectedComment.Discussions.FirstOrDefault(it => it.id.Equals(body.DiscussionId, StringComparison.CurrentCultureIgnoreCase));
            if (selectedDiscussion == null) return;

            var likeDiscussions = _likeDiscussionRepo.GetLikeDiscussionByDiscusionId(body.DiscussionId)
                .Where(it => !it.DeletedDate.HasValue)
                .ToList();

            var likedDiscussionsByThisUser = likeDiscussions
                .Where(it => it.LikedByUserProfileId.Equals(body.UserProfileId, StringComparison.CurrentCultureIgnoreCase));

            var isUnlike = likedDiscussionsByThisUser.Any();
            if (isUnlike)
            {
                foreach (var item in likedDiscussionsByThisUser)
                {
                    item.DeletedDate = now;
                    _likeDiscussionRepo.UpsertLikeDiscussion(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 LikeDiscussion
                {
                    id = Guid.NewGuid().ToString(),
                    ClassRoomId = body.ClassRoomId,
                    LessonId = body.LessonId,
                    CommentId = body.CommentId,
                    DiscussionId = body.DiscussionId,
                    LikedByUserProfileId = body.UserProfileId,
                    CreatedDate = now
                };
                likeDiscussions.Add(newLike);
                _likeDiscussionRepo.UpsertLikeDiscussion(newLike);
            }

            selectedDiscussion.TotalLikes = likeDiscussions.Where(it => !it.DeletedDate.HasValue).Count();
            _commentRepo.UpsertComment(selectedComment);
            _notificationCtrl.SendNotification();
        }