Esempio n. 1
0
        public async Task <Link> GetUrl(string shortUrl)
        {
            var link = await _linksRepository.GetByShortUrl(shortUrl)
                       ?? throw new ArgumentException("Ссылка не найдена");

            link.Count++;
            await _linksRepository.Update(link);

            return(link);
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        public IActionResult Create([FromBody] CreateLink command)
        {
            if (!(command.Link.IsValidHttpLink() || command.Link.IsValidHttpsLink()))
            {
                return(BadRequest());
            }

            Link linkInformation = new Link {
                OriginalLink = command.Link, Visitors = 0
            };

            _repository.Add(linkInformation);

            linkInformation.Hash = _hashAlgorithm.Hash(linkInformation.Id);
            _repository.Update(linkInformation);

            return(Ok(new { message = "Created" }));
        }
Esempio n. 4
0
        public IActionResult Update(Link link)
        {
            if (!ModelState.IsValid)
            {
                TempData["msg"] = "<script>alert('Link url format is not correct. Use full link started from http:// or https://');</script>";
                return(Redirect("Index"));
            }

            _repository.Update(link);

            return(Redirect("Index"));
        }
Esempio n. 5
0
        public IActionResult Update(Link link)
        {
            if (!ModelState.IsValid)
            {
                TempData["msg"] = "<script>alert('Ссылка  имеет не корректный вид, используйте http:// или https://');</script>";
                return(Redirect("Index"));
            }

            _repository.Update(link);

            return(Redirect("Index"));
        }
Esempio n. 6
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());
        }
Esempio n. 7
0
        public IActionResult RedirectLink(string hash)
        {
            Link linkToRedirect = _repository.GetLinkByHash(hash);

            if (!(Request.Cookies[hash] is null))
            {
                return(Redirect(linkToRedirect.OriginalLink));
            }

            linkToRedirect.Visitors += 1;
            _repository.Update(linkToRedirect);

            AddCookie(hash);

            return(Redirect(linkToRedirect.OriginalLink));
        }
 public IActionResult Put([FromBody] Link link)
 {
     _repository.Update(link);
     return(Ok());
 }
 public IActionResult Update(Link link)
 {
     _repository.Update(link);
     return(Redirect("Index"));
 }