Exemplo n.º 1
0
        public ConsensusContext(
            IApplicationLifetime applicationLifetime,
            HttpClient httpClient,
            ILogger <IConsensusContext> logger)
        {
            _applicationLifetime = applicationLifetime;
            _httpClient          = httpClient;
            _logger = logger;

            _applicationLifetime.ApplicationStopping.Register(DisposeResources);
            _nodeVote = new NodeVote();
        }
Exemplo n.º 2
0
        private async Task <int> AddVoteAsync(string userId, string nodeId, bool isUpVote)
        {
            var weight = await GetWeightAsync(userId);

            var score = (short)(isUpVote ? weight : -weight);
            var vote  = new NodeVote()
            {
                Id     = Guid.NewGuid().ToString(),
                NodeId = nodeId,
                UserId = userId,
                Score  = score
            };

            _nodeRepository.AddVote(vote);
            return(await UpdateHotAsync(vote, false));
        }
Exemplo n.º 3
0
        private async Task <int> UpdateHotAsync(NodeVote vote, bool isUndo)
        {
            var node = await _nodeRepository.GetAsync(vote.NodeId);

            if (vote.Score > 0)
            {
                node.UpVotes = node.UpVotes + (isUndo? -1 : 1) * vote.Score;
            }
            if (vote.Score < 0)
            {
                node.DownVotes = node.DownVotes + (isUndo ? 1 : -1) * vote.Score;
            }
            var hot = Hot(node.UpVotes, node.DownVotes, node.CreatedDate);

            node.Hot = hot;

            await _nodeRepository.SaveChangesAsync();

            var diff = node.UpVotes - node.DownVotes;

            return(diff > 0 ? diff : 0);
        }
 public void RemoveVote(NodeVote vote)
 {
     _dbContext.NodeVotes.Remove(vote);
 }
 public void AddVote(NodeVote vote)
 {
     _dbContext.NodeVotes.Add(vote);
 }