Exemplo n.º 1
0
        public async Task <string> Upvote(string postId)
        {
            try
            {
                var currentuser = Feature.CurrentUser(httpContextAccessor, userRepository);
                var currentPost = await postRepository.GetByIdAsync(ObjectId.Parse(postId));

                var builderUpvote     = Builders <UpVote> .Filter;
                var filterExistUpvote = builderUpvote.Eq(ObjectVoteId, postId)
                                        & builderUpvote.Eq(UpVoteBy, currentuser.OId)
                                        & builderUpvote.Eq(IsDeleted, false);

                var existUpvote = await upVoteRepository.FindAsync(filterExistUpvote);

                if (existUpvote != null)
                {
                    return(UserUpvoteAlready);
                }
                else if (existUpvote == null)
                {
                    var builderDownVote     = Builders <DownVote> .Filter;
                    var filterExistDownVote = builderDownVote.Eq(ObjectVoteId, postId)
                                              & builderDownVote.Eq(DownVoteBy, currentuser.OId)
                                              & builderDownVote.Eq(IsDeleted, false);
                    var existDownVote = await downVoteRepository.FindAsync(filterExistDownVote);

                    if (existDownVote != null)
                    {
                        existDownVote.IsDeleted = true;
                        await downVoteRepository.DeleteAsync(existDownVote.Id);
                    }

                    var upvote = new UpVote()
                    {
                        ObjectVoteId = postId,
                        UpVoteBy     = currentuser.OId
                    };
                    await upVoteRepository.AddAsync(upvote);
                }

                var notificationDetail = new Noftication()
                {
                    AuthorId        = currentuser.OId,
                    OwnerId         = currentPost.AuthorId,
                    ObjectId        = currentPost.OId,
                    ObjectThumbnail = currentPost.Title
                };

                await fcmRepository.PushNotify(currentPost.AuthorId, notificationDetail, NotificationContent.UpvotePostNotification);

                await userService.AddPoint(currentPost.AuthorId, currentPost.OId, PointAdded.Upvote);

                return(UpvoteSuccess);
            }
            catch (Exception)
            {
                return("Có lỗi xảy ra. ");
            }
        }