Esempio n. 1
0
        public async Task <OperationResult <bool> > SetCommentReaction(User user, Guid id, CommentReaction reaction)
        {
            var sw      = Stopwatch.StartNew();
            var result  = false;
            var errors  = new List <Exception>();
            var comment = DbContext.Comments.FirstOrDefault(x => x.RoadieId == id);

            if (comment == null)
            {
                return(new OperationResult <bool>(true, string.Format("Comment Not Found [{0}]", id)));
            }
            var userCommentReaction =
                DbContext.CommentReactions.FirstOrDefault(x => x.CommentId == comment.Id && x.UserId == user.Id);

            if (userCommentReaction == null)
            {
                userCommentReaction = new data.CommentReaction
                {
                    CommentId = comment.Id,
                    UserId    = user.Id.Value
                };
                DbContext.CommentReactions.Add(userCommentReaction);
            }

            userCommentReaction.Reaction = reaction == CommentReaction.Unknown ? null : reaction.ToString();
            await DbContext.SaveChangesAsync();

            ClearCaches(comment);
            var userCommentReactions = (from cr in DbContext.CommentReactions
                                        where cr.CommentId == comment.Id
                                        select cr).ToArray();
            var additionalData = new Dictionary <string, object>();

            additionalData.Add("likedCount",
                               userCommentReactions.Where(x => x.ReactionValue == CommentReaction.Like).Count());
            additionalData.Add("dislikedCount",
                               userCommentReactions.Where(x => x.ReactionValue == CommentReaction.Dislike).Count());
            sw.Stop();
            result = true;
            return(new OperationResult <bool>
            {
                AdditionalClientData = additionalData,
                Data = result,
                IsSuccess = result,
                Errors = errors,
                OperationTime = sw.ElapsedMilliseconds
            });
        }