Ejemplo n.º 1
0
        /// <summary>
        /// This method gives like if the post is not liked previously.
        /// gives unlike if the post was liked before.
        /// </summary>
        /// <param name="context">CurrentUserId.</param>
        /// <param name="postId">PostId.</param>
        /// <returns>ReactResponse.</returns>
        public async Task <ReactResponse> AddReact(string context, string postId)
        {
            var post = await _cacheProcessor.FindPostStringAsync(postId);

            if (post == null)
            {
                post = await _postManager.GetPostById(postId);

                if (post == null)
                {
                    return(new ReactResponse()
                    {
                        StatusCode = StatusCode.NotFound, ErrorMessage = "Post not found",
                    });
                }
                // await _cacheProcessor.UpdatePostStringAsync(post.Id, post);
            }
            var user = await _userManager.GetUser(context);

            var result = await _postManager.AddReact(post.Id, user);

            post = await _postManager.GetPostById(post.Id);

            var cached = true;

            if (post.Author.Id == user.Id)
            {
                cached = await _cacheProcessor.UpdateUserPostStringAsync(user.Id, post);
            }
            var cachedSingle = await _cacheProcessor.UpdatePostStringAsync(post.Id, post);

            if (!cached || !cachedSingle)
            {
                return(new ReactResponse()
                {
                    StatusCode = StatusCode.Internal, ErrorMessage = "Internal error! Couldn't cache react",
                });
            }
            if (result)
            {
                return(new ReactResponse()
                {
                    StatusCode = StatusCode.Ok, LikeOrUnlike = "Like", Post = ConvertToPostResponse(post),
                });
            }
            return(new ReactResponse()
            {
                StatusCode = StatusCode.Ok, LikeOrUnlike = "Unlike", Post = ConvertToPostResponse(post),
            });
        }