Exemple #1
0
        public async Task DownVote(int linkId, string userId)
        {
            var link = await _linksRepository.GetById(linkId);

            if (await _votesRepository.AlreadyVoted(linkId, userId))
            {
                var vote = await _votesRepository.GetByLinkId(linkId);

                switch (vote.VoteType)
                {
                // Down - return
                case VoteType.Down: return;

                // Up - none vote
                case VoteType.Up:
                {
                    vote.VoteType = VoteType.NonVote;
                    link.Ups--;
                    break;
                }

                // none vote - down
                case VoteType.NonVote:
                {
                    vote.VoteType = VoteType.Down;
                    link.Downs++;
                    break;
                }
                }
                // TODO: Save vote and link to database
                await _linksRepository.Update(link);

                await _votesRepository.Update(vote);
            }
            else
            {
                // create new down vote
                var user = await _userRepository.GetById(userId);

                var newVote = new Vote(link, user, VoteType.Down);

                link.Downs++;

                await _votesRepository.Add(newVote);

                await _linksRepository.Update(link);
            }
        }