Example #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);
            }
        }
Example #2
0
        public async Task <Unit> Handle(DeleteLinkCommand request, CancellationToken cancellationToken)
        {
            var link = await _linksRepository.GetById(request.LinkId);

            await _linksRepository.Delete(link);

            return(new Unit());
        }
Example #3
0
        public async Task <Unit> Handle(EditLinkCommand request, CancellationToken cancellationToken)
        {
            var link = await _linksRepository.GetById(request.LinkId);

            link.UpdatedAt   = DateTime.Now;
            link.Description = request.Description;
            link.Url         = request.Url;

            await _linksRepository.Update(link);

            return(new Unit());
        }
Example #4
0
        public async Task <int> Handle(CreateCommentCommand request, CancellationToken cancellationToken)
        {
            if (request.User == null)
            {
                throw new ArgumentNullException(nameof(request.User));
            }

            var link = await _linksRepository.GetById(request.LinkId);

            var comment = new Comment(request.Content, request.User, link);

            await _commentsRepository.Add(comment);

            return(comment.Id);
        }
Example #5
0
        public async Task <LinkDto> Handle(GetLinkById request, CancellationToken cancellationToken)
        {
            var link = await _linksRepository.GetById(request.LinkId);

            return(_mapper.Map <LinkDto>(link));
        }