Пример #1
0
        public IHttpActionResult CreatePost([FromBody] Post NewPost, [FromUri] string AccessToken)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var UserId = TokenValidator.VerifyToken(AccessToken);

            NewPost.UserId = UserId;

            NewPost = PostAuxiliar.UpdateOwner(NewPost, SQLContext);

            NewPost = TreatPostAddons(NewPost);

            NoSQLContext.PostCollection.InsertOne(NewPost);

            Post Parent = default;

            if (NewPost.Parent != null)
            {
                Parent = FindParent(NewPost);

                if (Parent != default)
                {
                    UpdateParent(Parent, NewPost.Id);
                }
            }

            GraphAuxiliar.IncreaseFollowAffinity(NewPost.UserId, NewPost.Mentions, SQLContext);
            GraphAuxiliar.IncreaseTopicAffinity(NewPost.UserId, NewPost.Hashtags, SQLContext);

            if (Parent != default)
            {
                GraphAuxiliar.IncreaseFollowAffinity(NewPost.UserId, Parent.UserId, SQLContext);
                GraphAuxiliar.IncreaseFollowAffinity(NewPost.UserId, Parent.Mentions, SQLContext);
                GraphAuxiliar.IncreaseTopicAffinity(NewPost.UserId, Parent.Hashtags, SQLContext);
            }

            return(Ok(NewPost));
        }
Пример #2
0
        public IHttpActionResult Like([FromUri] string AccessToken, [FromUri] string PostId)
        {
            var UserId = TokenValidator.VerifyToken(AccessToken);

            var PostLiked = PostAuxiliar.PostById(PostId);

            if (PostLiked.Likes.Contains(UserId))
            {
                PostLiked.Likes.Remove(UserId);
            }
            else
            {
                PostLiked.Likes.Add(UserId);
            }

            if (PostLiked.Dislikes.Contains(UserId))
            {
                PostLiked.Dislikes.Remove(UserId);
            }

            var Update = Builders <Post> .Update
                         .Set("Likes", PostLiked.Likes)
                         .Set("Dislikes", PostLiked.Dislikes);

            PostAuxiliar.UpdatePostById(PostLiked.Id, Update);

            int TotalLikes    = PostLiked.Likes.Count;
            int TotalDeslikes = PostLiked.Dislikes.Count;

            dynamic Response = new { TotalLikes, TotalDeslikes };

            GraphAuxiliar.IncreaseFollowAffinity(UserId, PostLiked.UserId, SQLContext);
            GraphAuxiliar.IncreaseTopicAffinity(UserId, PostLiked.Hashtags, SQLContext);

            return(Ok(Response));
        }