public ConsensusContext( IApplicationLifetime applicationLifetime, HttpClient httpClient, ILogger <IConsensusContext> logger) { _applicationLifetime = applicationLifetime; _httpClient = httpClient; _logger = logger; _applicationLifetime.ApplicationStopping.Register(DisposeResources); _nodeVote = new NodeVote(); }
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)); }
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); }