示例#1
0
        public async Task <IActionResult> Update(
            int id,
            [FromBody] ShortUrlUpdateDto shortUrlDto
            )
        {
            var shortUrl = await _shortUrlRepository.FindById(id);

            if (shortUrl == null)
            {
                return(BadRequest());
            }

            // Patch only supplied fields
            _mapper.Map(shortUrlDto, shortUrl);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var success = await _shortUrlRepository.Update(shortUrl);

            if (!success)
            {
                return(BadRequest());
            }

            var response = _mapper.Map <ShortUrlDto>(shortUrl);

            return(Ok(response));
        }
示例#2
0
        /// <summary>
        /// 更新短网址
        /// </summary>
        /// <param name="entity">需要更新的短网址信息</param>
        public void Update(ShortUrlEntity entity)
        {
            EventBus <ShortUrlEntity> .Instance().OnBefore(entity, new CommonEventArgs(EventOperationType.Instance().Update()));

            shortUrlRepository.Update(entity);
            EventBus <ShortUrlEntity> .Instance().OnAfter(entity, new CommonEventArgs(EventOperationType.Instance().Update()));
        }
        /// <summary>
        /// get the actual url base the short url of it
        /// </summary>
        /// <param name="shortUrl">short url for redirect to actual path</param>
        /// <returns></returns>
        public string GetUrl(string shortUrl)
        {
            var q = _shortUrlRepository.GetUrl(shortUrl);

            if (q != null)
            {
                q.TimesOfView++;
                _shortUrlRepository.Update(q);
                return(q.ActualURL);
            }
            return(null);
        }
示例#4
0
        public ShortUrl GenerateNew(NewShortUrl newShortUrl)
        {
            _loggerservice.LogInformation("Generate URL request called : " + newShortUrl.OriginalUrl);
            var shorturl     = _mapper.Map <ShortUrl>(newShortUrl);
            var reposhorturl = _shortUrlRepository.Add(_mapper.Map <Domain.Models.ShortUrl>(shorturl));

            reposhorturl.ShortenedUrl = _generator.Encode(reposhorturl.Id);
            _shortUrlRepository.Update(reposhorturl);
            _loggerservice.LogInformation("Caching new short URL into cache service : ");
            shorturl = _mapper.Map <ShortUrl>(reposhorturl);
            _shortUrlCacheService.Set(shorturl.ShortenedUrl, shorturl);
            return(shorturl);
        }
        private async Task <ShortUrl> CheckAndGetShortUrl(Guid slugId, bool boost, bool isRouter, CancellationToken cancellationToken)
        {
            //Get shorturl entity
            var shortUrlEntity = await _shortUrlRepository.GetBySlug(slugId, cancellationToken);

            if (!isRouter)
            {
                //Check owner
                Check.That <ApplicationException>(!CurrentUser.IsAdmin() &&
                                                  !shortUrlEntity.CreatedBy.Equals(CurrentUser.Id), $"This slug not found for the user");
            }


            //Set Hits
            if (boost)
            {
                shortUrlEntity.Boost();
            }

            //Update the shorturl
            await _shortUrlRepository.Update(shortUrlEntity, cancellationToken);

            return(shortUrlEntity);
        }