示例#1
0
        public async Task <CommentViewModel> Add(AddCommentViewModel model)
        {
            var hashIds = new Hashids(minHashLength: 5);

            var comment = await StoriesDbContext.Comments.AddAsync(new Comment {
                ParentCommentId = string.IsNullOrEmpty(model.ParentCommentHashId) ? null : hashIds.Decode(model.ParentCommentHashId)?.First(),
                ContentMarkdown = model.CommentMarkdown,
                Content         = CommonMarkConverter.Convert(model.CommentMarkdown),
                StoryId         = hashIds.Decode(model.StoryHashId).First(),
                UserId          = model.UserId
            });

            await StoriesDbContext.SaveChangesAsync();

            VoteQueueService.QueueCommentVote(comment.Entity.Id);

            return(await MapCommentToCommentViewModel(comment.Entity));
        }
示例#2
0
        public async Task <bool> ToggleCommentVote(string hashId, Guid userId)
        {
            var commentId = new Hashids(minHashLength: 5).Decode(hashId)?.FirstOrDefault();

            if (commentId == 0)
            {
                return(false);
            }

            var comment = await StoriesDbContext.Comments.Include(v => v.Votes).Where(o => o.Id == commentId).FirstOrDefaultAsync();

            if (comment == null)
            {
                return(false);
            }

            var vote = comment.Votes.FirstOrDefault(v => v.UserId == userId);

            if (vote != null)
            {
                comment.Upvotes--;
                return(await DeleteVote(vote) > 0);
            }

            vote = new Vote
            {
                CommentId = commentId,
                UserId    = userId,
            };

            comment.Upvotes++;

            VoteQueueService.QueueCommentVote(comment.Id);

            return(await AddVote(vote) > 0);
        }