예제 #1
0
        public async Task <string> Handle(AddUrlCommand request, CancellationToken cancellationToken)
        {
            var result = Uri.TryCreate(request.UrlAddress, UriKind.Absolute, out var uriResult) &&
                         (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

            if (!result)
            {
                throw new Exception("Invalid Uri");
            }
            if (!string.IsNullOrWhiteSpace(request.Code) && _urlRepository.GetEntities().Any(x => x.UrlCode?.Code == request.Code))
            {
                throw new Exception("Used Code");
            }
            var url = _mapper.Map <Url>(request);

            _urlRepository.Add(url);
            var code = !string.IsNullOrWhiteSpace(request.Code) ? request.Code : Helper.GetCode(url.Id); // uretilen kod unique oldugu icin var mi yokmu diye bakmaya gerek yok.

            url.UrlCode = new UrlCode {
                UrlId = url.Id, Code = code
            };
            _urlRepository.Update(url);
            // var code2 = Helper.GenerateCode(6);
            return(await Task.FromResult(new Uri(ClientUrl.BaseUri, url.UrlCode.Code).ToString()));
        }
        /// <summary>
        /// Adds the url elements.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="filePath"/> is null
        /// </exception>
        public void AddElements(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException($"{nameof(filePath)} is null or empty.");
            }

            if (File.Exists(filePath))
            {
                using (var streamReader = new StreamReader(filePath))
                {
                    string url = string.Empty;

                    while (streamReader.EndOfStream == false)
                    {
                        url = streamReader.ReadLine();

                        if (urlParserService.IsUrl(url))
                        {
                            urlRepository.Add(urlParserService.Url);
                        }
                        else
                        {
                            logger.LogInformation("{0} isn't url.", url);
                        }
                    }
                }
            }
            else
            {
                logger.LogInformation("{0} doesn't exist.", filePath);
            }
        }
        public async Task <UrlDto> Handle(ShortenUrlCommand request, CancellationToken cancellationToken)
        {
            var longUrl = ValidateUrl(request);

            var shortUrl = _shortUrlService.GetShortUrl(longUrl);

            // check cache
            UrlDto urlDto = null;

            if (_database != null)
            {
                var data = await _database.StringGetAsync(shortUrl);

                if (!data.IsNullOrEmpty)
                {
                    urlDto = JsonConvert.DeserializeObject <UrlDto>(data);
                    if (urlDto != null)
                    {
                        urlDto.LongUrl = GetResultLongUrl(urlDto.LongUrl);
                        return(urlDto);
                    }
                }
            }

            var dbUrl = await _mediator.Send(new GetUrlFromLongUrlQuery(longUrl));

            if (dbUrl != null)
            {
                var dbUrlDto = _mapper.Map <UrlDto>(dbUrl);
                dbUrlDto.LongUrl = GetResultLongUrl(dbUrl.LongUrl);
                return(dbUrlDto);
            }

            var url = new Url(longUrl);

            _urlRepository.Add(url);

            url.SetShortUrl(shortUrl);

            await _urlRepository.UnitOfWork.SaveEntitiesAsync();

            urlDto = _mapper.Map <UrlDto>(url);

            if (_database != null)
            {
                var created = await _database.StringSetAsync(urlDto.ShortUrl, JsonConvert.SerializeObject(urlDto));

                if (!created)
                {
                    _logger.LogInformation("Error when setting cache for url!");
                    return(null);
                }

                _logger.LogInformation("Url cache updated successfully.");
            }

            urlDto.LongUrl = GetResultLongUrl(urlDto.LongUrl);
            return(urlDto);
        }
예제 #4
0
        public Url Shorten(string original, string userId, LinkExpiration duration)
        {
            Url    url       = new Url(userId, original, duration);
            string generated = _parameterService.GenerateNextValue(ParameterConstants.LastCreated).Value;

            while (!_repository.CheckAvailable(generated))
            {
                generated = _parameterService.GenerateNextValue(ParameterConstants.LastCreated).Value;
            }
            url.ChangeShortened(generated);
            _repository.Add(url);
            return(url);
        }
 public IHttpActionResult Add([FromBody] Url url)
 {
     _repo.Add(url);
     return(Created(Request.RequestUri, url));
 }
예제 #6
0
 public Url Add(Url url)
 {
     return(urlRepo.Add(url));
 }