void IPostBusinessLogic.PatchReact(PostPatchReactDto postPatchReact)
        {
            PostReact postReact = postReactRepository.GetByFilter(
                pr => pr.UserId == postPatchReact.CreatorId && pr.PostId == postPatchReact.Id);
            ReactType type = (ReactType)System.Enum.Parse(typeof(ReactType), postPatchReact.Type);

            int deltaUpvotes = 0, deltaDownvotes = 0;

            if (postReact == null)
            {
                postReact = new PostReact
                {
                    PostId = postPatchReact.Id,
                    UserId = postPatchReact.CreatorId,
                    Type   = type
                };
                postReactRepository.Insert(postReact);
                postReactRepository.SaveChanges();
                ReactsUtils.UpdateDeltas(ref deltaUpvotes, ref deltaDownvotes, type, ReactType.None);
            }
            else
            {
                if (type == ReactType.None)
                {
                    ReactsUtils.UpdateDeltas(ref deltaUpvotes, ref deltaDownvotes, type, postReact.Type);
                    postReactRepository.Delete(postReact.Id);
                    postReactRepository.SaveChanges();
                }
                else if (type != postReact.Type)
                {
                    ReactsUtils.UpdateDeltas(ref deltaUpvotes, ref deltaDownvotes, type, postReact.Type);
                    postReact.Type = type;
                    postReactRepository.Update(postReact);
                    postReactRepository.SaveChanges();
                }
            }
            if (deltaUpvotes != 0 || deltaDownvotes != 0)
            {
                Post post = postRepository.GetById(postPatchReact.Id);
                post.Upvotes   += deltaUpvotes;
                post.Downvotes += deltaDownvotes;
                postRepository.Update(post);
                postRepository.SaveChanges();
            }
        }
Example #2
0
        void ICommentBusinessLogic.PatchReact(CommentPatchReactDto commentPatchReact)
        {
            CommentReact commentReact = commentReactRepository.GetByFilter(
                cr => cr.UserId == commentPatchReact.CreatorId && cr.CommentId == commentPatchReact.Id);
            ReactType type = (ReactType)Enum.Parse(typeof(ReactType), commentPatchReact.Type);

            int deltaUpvotes = 0, deltaDownvotes = 0;

            if (commentReact == null)
            {
                commentReact = new CommentReact
                {
                    CommentId = commentPatchReact.Id,
                    UserId    = commentPatchReact.CreatorId,
                    Type      = type
                };
                commentReactRepository.Insert(commentReact);
                commentRepository.SaveChanges();
                ReactsUtils.UpdateDeltas(ref deltaUpvotes, ref deltaDownvotes, type, ReactType.None);
            }
            else
            {
                if (type == ReactType.None)
                {
                    ReactsUtils.UpdateDeltas(ref deltaUpvotes, ref deltaDownvotes, type, commentReact.Type);
                    commentReactRepository.Delete(commentReact.Id);
                    commentReactRepository.SaveChanges();
                }
                else if (type != commentReact.Type)
                {
                    ReactsUtils.UpdateDeltas(ref deltaUpvotes, ref deltaDownvotes, type, commentReact.Type);
                    commentReact.Type = type;
                    commentReactRepository.Update(commentReact);
                    commentReactRepository.SaveChanges();
                }
            }
            if (deltaUpvotes != 0 || deltaDownvotes != 0)
            {
                Comment comment = commentRepository.GetById(commentPatchReact.Id);
                comment.Upvotes   += deltaUpvotes;
                comment.Downvotes += deltaDownvotes;
                commentRepository.Update(comment);
                commentRepository.SaveChanges();
            }
        }