ICollection <CommentGetDto> ICommentBusinessLogic.GetAll(Guid postId, UserInfoModel userInfo) { ICollection <Comment> comments = commentRepository.GetAllByFilter(c => c.PostId == postId, "Reacts"); ICollection <CommentGetDto> returnedComemnts = new List <CommentGetDto>(); foreach (Comment comment in comments) { CommentGetDto commentGetDto = mapper.Map <CommentGetDto>(comment); if (userInfo != null && comment.Reacts != null) { CommentReact react = comment.Reacts.FirstOrDefault(cr => cr.UserId == userInfo.CreatorId); if (react != null) { if (react.Type == ReactType.Like) { commentGetDto.Liked = true; } else if (react.Type == ReactType.Dislike) { commentGetDto.Disliked = true; } } } returnedComemnts.Add(commentGetDto); } return(returnedComemnts); }
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(); } }